0

I have a method called NewBatch_GetAndSetBatchID.

When I call this methode in the Page_Load methode it works perfectly.

But when I call this methode from a button (on the same page), I get:

Null reference exeption Object reference not set to an instance of an object.

It throws an error at:

BatchNoTextBox.Text = (batchID += 1).ToString();

When I debug, I can see that my batchID is filled with a value.

This is my code:

public void NewBatch_GetAndSetBatchID()
    {
        FormView1.ChangeMode(FormViewMode.Insert);


        string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
        try
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                SqlCommand sqlCommando = new SqlCommand("SELECT MAX(BatchNo) FROM BM_BrewBatchHeader", conn);

                try
                {
                    conn.Open();
                    batchID = Convert.ToInt32(sqlCommando.ExecuteScalar());
                }
                catch (Exception)
                {

                }
                finally
                {
                    conn.Close();
                }
            }
        }
        catch (SqlException error)
        {

        }

        try
        {
            TextBox BatchNoTextBox = (TextBox)FormView1.FindControl("BatchNoTextBox");
            BatchNoTextBox.Text = (batchID += 1).ToString();
        }
        catch (Exception)
        {
            throw;
        }
    }

What can be the problem here?

I found the solution on another forum: I have to use the PreRender trigger from the FormView. With the code below it works fine. When I now set the Formview to insert or edit mode the code executes perfectly.

protected void FormView1_PreRender(object sender, EventArgs e)
        {
            // if the mode is Edit or Insert
            if (this.FormView1.CurrentMode == FormViewMode.Edit || this.FormView1.CurrentMode == FormViewMode.Insert)
            {
                TextBox BatchNoTextBox = (TextBox)FormView1.FindControl("BatchNoTextBox");
                BatchNoTextBox.Text = (batchID += 1).ToString();
            }
        }
Mart Apon
  • 69
  • 1
  • 1
  • 8
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Uwe Keim Sep 13 '18 at 16:47
  • If the right side of an assignment is not the problem, most likely the left side is the problem (i.e. `BatchNoTextBox` is null). – Uwe Keim Sep 13 '18 at 16:49
  • Ok, I understand but How can I fix that? I'm a bit lost now. Sorry, new at programming.. Whyy is my object null when I call the methode from a button and not when I call it from the PageLoad – Mart Apon Sep 13 '18 at 16:52
  • can you try this ` Int32 ID = Convert.ToInt32(sqlCommando.ExecuteScalar());` and see it works first. ` – Saif Sep 13 '18 at 16:55
  • That doesn't work. I stil get the same error. – Mart Apon Sep 13 '18 at 17:01
  • 1
    What is a NullReferenceException, and how do I fix it? Good article, I can find my problem there but not the solution. Think it has something to do with the Page lifecycle but I still don't now how to fix it @UweKeim – Mart Apon Sep 13 '18 at 17:37
  • If you think it has something to do with the page lifecycle, then you should explain when this code is running with respect to the lifecycle. If you create a proper [MCVE] then this wouldn't be an issue. – mason Sep 13 '18 at 18:59
  • I found the solution on another forum. I edited my question and added the answer found there. Thanks for the help people! – Mart Apon Sep 13 '18 at 19:09

0 Answers0