firas shaar

firas shaar

  • NA
  • 2
  • 2.7k

problem in sum function

Oct 29 2013 5:59 AM
please someone help me
I have two tables the first is imprests table and the second is payments_imprests
I have method that return payments values sum in payments_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 I have imprest_ID in imprests table and I have payments values for this impres_ID in imprests_payments table
but my problem is when I have imprest_ID in imprests tbale
but no rows is found in imprests_payments table where worker hasn't paid any payment value
where the following error is rising
Object reference not set to an instance of an object.
I need the solution for this problem
         


Answers (3)