收到此错误** System.NullReferenceException:“对象引用未设置为对象的实例。” **

时间:2019-02-15 08:54:14

标签: c# asp.net sql-server visual-studio stored-procedures

我有一个页面可以更新数据库中贷方的详细信息,但是当我单击“更新”按钮时,它将引发此错误。

我已经放置了断点并检查了当我将鼠标悬停时会显示值的代码,而且还会抛出此错误

这是我从文本框中获取值的地方,当我将鼠标悬停在enttity.lender_name(所有文本框都相同)上时,它将在文本框中正确显示值

protected void btn_update_Click(object sender, EventArgs e)
    {
        try
        {

            enttity.lender_name = lender_name.Text;
            enttity.lender_code = lender_code.Text;
            enttity.manager_name = manager_name.Text;
            enttity.manager_number = manager_number.Text;
            enttity.lc_number = lc_number.Text;
            enttity.manager_email = manager_email.Text;
            enttity.lc_email = lc_email.Text;
            enttity.contact_name = contact_name.Text;
            enttity.designation = designation.Text;
            enttity.branch_name = branch_name.Text;
            enttity.branch_add = branch_add.Text;
            enttity.branch_add2 = branch_add2.Text;
            enttity.branch_city = branch_city.Text;
            enttity.branch_state = branch_state.Text;
            enttity.branch_zip = branch_zip.Text;
            enttity.branch_country = branch_country.Text;
            int update = bal.lenderupdate(enttity, Convert.ToInt32(ViewState["lender_id"].ToString()));

            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert(' Successfully updated!');window.location.href = 'Lender_registration.aspx'", true);

        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

错误在此行int update = bal.lenderupdate(enttity, Convert.ToInt32(ViewState["lender_id"].ToString()));中引发,并转到throw ex,并说错误 System.NullReferenceException:“对象引用未设置为对象的实例。”

这是我在存储过程中使用的代码

public int lenderupdate(lender_entity enttity, int lender_id)
    {
        try
        {
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@lender_id ", lender_id);
            cmd.Parameters.AddWithValue("@lender_name ", enttity.lender_name);
            cmd.Parameters.AddWithValue("@lender_code", enttity.lender_code);
            cmd.Parameters.AddWithValue("@manager_name", enttity.manager_name);
            cmd.Parameters.AddWithValue("@manager_number ", enttity.manager_number);
            cmd.Parameters.AddWithValue("@lc_number", enttity.lc_number);
            cmd.Parameters.AddWithValue("@manager_email", enttity.manager_email);
            cmd.Parameters.AddWithValue("@lc_email ", enttity.lc_email);
            cmd.Parameters.AddWithValue("@contact_name", enttity.contact_name);
            cmd.Parameters.AddWithValue("@designation", enttity.designation);
            cmd.Parameters.AddWithValue("@branch_name ", enttity.branch_name);
            cmd.Parameters.AddWithValue("@branch_add", enttity.branch_add);
            cmd.Parameters.AddWithValue("@branch_add2", enttity.branch_add2);
            cmd.Parameters.AddWithValue("@branch_city", enttity.branch_city);
            cmd.Parameters.AddWithValue("@branch_state", enttity.branch_state);
            cmd.Parameters.AddWithValue("@branch_zip", enttity.branch_zip);
            cmd.Parameters.AddWithValue("@branch_country", enttity.branch_country);
            int updatelender = dbmngr.ExecuteNonQuery(cmd, CommandType.StoredProcedure, "updatelenders");
            return updatelender;


        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

这些值到这里了,但是会引发错误

数据应进入数据库并更新特定贷方的详细信息

1 个答案:

答案 0 :(得分:0)

int update = bal?.lenderupdate(enttity,Convert.ToInt32(ViewState["lender_id"]?.ToString())) ?? 0;

我不知道什么是变量为null。但是,如果其中任何一个为null,则该语句返回0而不是异常