Brent Oliver
Brent Oliver

Reputation: 171

Getting a null exception error, but the variable this is thrown on is not null

I am working on a form to add a record to a database, that is prepopulated with the values from an existing record. When I click the save button however a null exception error is being thrown on the first variable used, which I know has a value. I have even tried typing over the pre-populated value and still get the null exception error. Why would this happen?

Here is the item from the aspx page:

<asp:tablecell>
   <asp:textbox ID="txt_author" runat="server" Text='<%#Item.Author %>' ></asp:textbox>
</asp:tablecell>

Here is the line throwing the error on the code behind:

protected void ButtonUpload_Click(object sender, EventArgs e)
{
      string vAuthor = txt_author.Text;

Upvotes: 1

Views: 117

Answers (1)

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

Assuming that ASP Table ID will be Table1 and having one row and TextBox is present in first cell, You can modify As per your code.

 string vAuthor= (Table1.Rows[0].Cells[0].FindControl("txt_author") as TextBox).Text

Since your Text Box is Present in Table Structure, and we can not able access it directly, we need to find Control first from TableCell.

Upvotes: 1

Related Questions