付款方式存在问题 [英] problem in sum payments method

查看:127
本文介绍了付款方式存在问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人帮助我

我有两个表,第一个是imprests表,第二个是payments_imprests

我有方法返回payment_imprests中的付款金额

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();
}



当我在imprests表中有imprest_ID并且我在imprests_payments表中有impres_ID的付款值时,此代码工作正常
但我的问题是当我在imprest_ID中获得imprest_ID时

但在imprests_payments表中找不到行,其中worker没有支付任何付款额

where以下错误正在上升

对象引用未设置为对象的实例。

我需要解决此问题的方法


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

推荐答案

尝试:

Try:
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
          }
}


这篇关于付款方式存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆