-2

I have a page which updates the details of lender in the database but when I click the update button it is throwing this error.

I have put the breakpoints and inspected the code it shows the values when I hover but also it is throwing this error

This is the place where I get the values from the textboxes and when I hover on the enttity.lender_name(same for all the textboxes) it shows the value in textbox correctly

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

The error is thrown in this line int update = bal.lenderupdate(enttity, Convert.ToInt32(ViewState["lender_id"].ToString())); and goes to throw ex saying the error System.NullReferenceException: 'Object reference not set to an instance of an object.'

This my code where it goes to Stored procedure

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

The values are coming here to but it is throwing the error

data should go to database and update the details of particular lender

Dilli Prasad
  • 5
  • 1
  • 7
  • The easiest way is to check `bal` or `ViewState["lender_id"]` in the debugger against null values. – Markus Feb 15 '19 at 08:55
  • @Markus How can I check it pls help – Dilli Prasad Feb 15 '19 at 08:56
  • As a sidenote, you should not use `throw ex` to re-throw an exception. Just use `throw` in order to preserve the stack trace. Maybe your production code contains something else in the catch-block, if not, you can just remove it. – Markus Feb 15 '19 at 08:57
  • I have checked the same but not solved https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Dilli Prasad Feb 15 '19 at 08:58
  • should I remove the `throw ex` – Dilli Prasad Feb 15 '19 at 08:59
  • set a breakpoint at the line (key F9) and run the program in Visual Studio. When the breakpoint is hit, move the mouse over `bal` to check whether it is set. If yes, use the `Watch` tool window and enter `ViewState["lender_id"]`. Make sure that the values are set or check against null. – Markus Feb 15 '19 at 08:59
  • if sample has the same catch block as your production code, you can remove the whole try-catch-block as it does not do anything useful. If your production code has some more code (e.g. for logging) in the catch-block, only remove the `ex` from the `throw ex`. Be awar that this will not solve the NullReferenceException. – Markus Feb 15 '19 at 09:12
  • You should remove the try...catch; there is no point catching an exception if all you do is throw it again. You should only catch an exception if: a) the catch has some code that can recover from the exception, or b) the catch has code to log/trace/report the exception (and the catch may then also rethrow the exception, using "throw", and not "throw ex"). – Polyfun Feb 15 '19 at 09:32

1 Answers1

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

I dont know what for variables are null. But if any is null then the statement return 0 and not a exception

Jamez Fatout
  • 402
  • 4
  • 13