Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
 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
C#
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
Posted
Updated 28-Oct-13 2:50am
v2

1 solution

Try:
C#
private void button1_Click(object sender, EventArgs e)
       {
          if (GridViewImprestsPays.CurrentRow != null)
          {
          double val = ImprestsPaysMgr.PaymentsvalueSum(int.Parse(GridViewImprestsPays.CurrentRow.Cells["Imprest_ID"].Value.ToString()));
               textBox1.Text = val.ToString();
          }
          else
          {
               // CurrentRow is null: you can't get the value of the Imprest_ID cell
          }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900