Answered by:
Null Reference error when calling a Methode

Question
-
User1600235305 posted
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, somehow my BatchNoTextBox = null but I can't understand why.
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; } }
Thursday, September 13, 2018 5:47 PM
Answers
-
User1600235305 posted
I Found the answer here: https://forums.asp.net/t/1227594.aspx?Accessing+the+TextBoxes+in+a+FormView+from+C+code+behind
Thanks for the help people.
When I use the PreRender event it works fine:
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(); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 13, 2018 7:08 PM
All replies
-
User409696431 posted
Can you clarify, which is null, the value of BatchNoTextBox.Text, or the result of the FindControl?
Thursday, September 13, 2018 6:12 PM -
User1600235305 posted
TextBox BatchNoTextBox = (TextBox)FormView1.FindControl("BatchNoTextBox");
batchNoTextBox.Text = (batchID += 1).ToString();
The TextBox BatchNoTextBox is null.
When I call the methode from the Page_Load the TextBox BatchNoTextBox is {System.Web.UI.WebControls.TextBox}Thursday, September 13, 2018 6:23 PM -
User1120430333 posted
Martapon
TextBox BatchNoTextBox = (TextBox)FormView1.FindControl("BatchNoTextBox");
batchNoTextBox.Text = (batchID += 1).ToString();
The TextBox BatchNoTextBox is null.
When I call the methode from the Page_Load the TextBox BatchNoTextBox is {System.Web.UI.WebControls.TextBox}It didn't find the control. The control can have a different name at the time you are trying to find the control. You should use the F12 Developer tool with the DOM Element browser and click on the control to show the actual name that it has at the time your program is trying to find it, if it's even there..
Thursday, September 13, 2018 6:36 PM -
User1600235305 posted
I Found the answer here: https://forums.asp.net/t/1227594.aspx?Accessing+the+TextBoxes+in+a+FormView+from+C+code+behind
Thanks for the help people.
When I use the PreRender event it works fine:
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(); } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 13, 2018 7:08 PM