1
Answer

problem in sum payments method

firas shaar

firas shaar

11y
902
1
please someone help me
I have method that return payments values sum for workers imprests
 public static double PaymentsvalueSum(int id)
        {
            string strconn = myproject.Properties.Settings.Default.SalariesConnectionString;
            OleDbConnection conn = new OleDbConnection(strconn);

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
OleDbCommand command = new OleDbCommand("select SUM(Payment_Value) from imprests_payments  WHERE Imprest_ID=@id;", conn);
            command.Parameters.Add(new OleDbParameter("@id", id));
            double sumvalue = (double)command.ExecuteScalar();
            try
            { }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                conn.Close();
            }
            return sumvalue;
        }

when button_click event I write

 private void button1_Click(object sender, EventArgs e)
        {
           double val = ImprestsPaysMgr.PaymentsvalueSum(int.Parse(GridViewImprestsPays.CurrentRow.Cells["Imprest_ID"].Value.ToString()));
                textBox1.Text = val.ToString();
 }

this code is working perfectly when payments values in imprests_payments table is available but my problem is when no payments values is available in imprests_payments table
where the following error is rising
Object reference not set to an instance of an object.
I need the solution for this problem


Answers (1)