Answered by:
ASP.net C# Object reference not set to an instance of an object

Question
-
I am getting the error:
Object reference not set to an instance of an object.The line it is getting the error on is this one:
valueoftextbox = txtInstance.Text;publicvoidButton1_Click(objectsender, EventArgse)
{
stringIDNumber = Request.QueryString["ID"];
stringvalueoftextbox = "";
stringstrSQLStatement = "";
TextBoxtxtInstance = (TextBox)Page.FindControl("ctlfldAnswer01");
valueoftextbox = txtInstance.Text;
strSQLStatement =
"fldQ01='"+ valueoftextbox + "' ";
srcChartReview.UpdateCommand =
"UPDATE tblAssignment SET "+ strSQLStatement + " WHERE fldID="+ IDNumber;
srcChartReview.Update();
}
- Moved by Caillen Wednesday, September 10, 2014 5:41 AM
Tuesday, September 9, 2014 3:31 PM
Answers
-
Where did you get "txtInstance" from?According to your code, it should be the nameID of the textbox.
Please check it.
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
- Marked as answer by Eugene Weast Wednesday, September 10, 2014 2:06 PM
Tuesday, September 9, 2014 5:36 PM
All replies
-
Your issue is likely being caused by this line:
TextBox txtInstance = (TextBox)Page.FindControl("ctlfldAnswer01");
which is returning null, because it is returning null, because it cannot find the control. Because it is null, this line:
valueoftextbox = txtInstance.Text;
is invalid because txtInstance is null.
It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
- Edited by TSoftware-Old Tuesday, September 9, 2014 5:49 PM corrected space
Tuesday, September 9, 2014 4:13 PM -
Where did you get "txtInstance" from?According to your code, it should be the nameID of the textbox.
Please check it.
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
- Marked as answer by Eugene Weast Wednesday, September 10, 2014 2:06 PM
Tuesday, September 9, 2014 5:36 PM -
Noam,
That is right, I intend "txtInstance" to be the name of the textbox using this line:
TextBox txtInstance = (TextBox)Page.FindControl("ctlfldAnswer01");
So I thought this would assign the text of the textbox to the variable "valueoftextbox":
valueoftextbox = txtInstance.Text;
or this:
valueoftextbox = txtInstance.Text.ToString();But I think the page is clearing out before I FindControl and there is no control to find. So it may be an event issue.
Any help is appreciated.
Wednesday, September 10, 2014 2:17 AM -
- Proposed as answer by Caillen Wednesday, September 10, 2014 5:39 AM
Wednesday, September 10, 2014 3:24 AM -
Oh, I was building the form dynamically. So that was the issue with how the page was loading. thanks for your help.
Wednesday, September 10, 2014 2:05 PM