Problem populating TextBox with DataRow from DataTable: NullReferenceException

M

Mico

I would be very grateful for any help with the following problem:

I use a DataAdapter to fill a DataSet in the Page_Load method.

Later, I use this DataSet to construct a DataTable, then create a
DataRow and search through the DataTable for a specific row. Having
found the row, I intend to display the various fields in some text
boxes. This is all triggered from a dropdownlist. The code for this
is shown below:

private void ddlTerms_SelectedIndexChanged(object sender,
System.EventArgs e)
{
DataTable tblTerm = dsAllTerm.Tables["tblTerm"];
DataRow drCurrent = tblTerm.Rows.Find(ddlTerms.SelectedValue);
txtName.Text = drCurrent["Term Name"].ToString();
}

However, when I load the page and trigger the relevant event, I get
the following message:

"Object reference not set to an instance of an object.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object."

The stack trace indicates this is a problem from the following line:

txtName.Text = drCurrent["Term Name"].ToString();

I think I've narrowed it down to the fact that drCurrent is giving a
null reference, but I don't know why or how to fix it.

Can anyone help?

Thanks,

Mark.
 
N

Nicholas Paldino [.NET/C# MVP]

Mico,

Well, if the value is null in the table, then it might give you null
when you look for it. Since you want a string (assuming that the column is
of type string), use a cast, like so:

// Set the text.
txtName.Text = (string) drCurrent["Term Name"];

This should work for you.

Hope this helps.
 
K

Kai Brinkmann [MSFT]

If drCurrent is null then your method call to
tblTerm.Rows.Find(ddlTerms.SelectedValue) doesn't seem to be returning a
value. Are you sure that ddlTerms.SelectedValue is the primary key in your
datatable? You need to set a primary key in the DataTable for the Find
method to work.

It's also possible that ddlTerms.Selected Value is actually not found in the
table, which would also cause drCurrent to be null. Did you try running the
debugger and verifying the state of your Dataset and DataTable objects at
runtime?

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mico

There was nothing in the DataSet!! Bah! Thanks for your help.

Mark.

Kai Brinkmann said:
If drCurrent is null then your method call to
tblTerm.Rows.Find(ddlTerms.SelectedValue) doesn't seem to be returning a
value. Are you sure that ddlTerms.SelectedValue is the primary key in your
datatable? You need to set a primary key in the DataTable for the Find
method to work.

It's also possible that ddlTerms.Selected Value is actually not found in the
table, which would also cause drCurrent to be null. Did you try running the
debugger and verifying the state of your Dataset and DataTable objects at
runtime?

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.


Mico said:
I would be very grateful for any help with the following problem:

I use a DataAdapter to fill a DataSet in the Page_Load method.

Later, I use this DataSet to construct a DataTable, then create a
DataRow and search through the DataTable for a specific row. Having
found the row, I intend to display the various fields in some text
boxes. This is all triggered from a dropdownlist. The code for this
is shown below:

private void ddlTerms_SelectedIndexChanged(object sender,
System.EventArgs e)
{
DataTable tblTerm = dsAllTerm.Tables["tblTerm"];
DataRow drCurrent = tblTerm.Rows.Find(ddlTerms.SelectedValue);
txtName.Text = drCurrent["Term Name"].ToString();
}

However, when I load the page and trigger the relevant event, I get
the following message:

"Object reference not set to an instance of an object.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object."

The stack trace indicates this is a problem from the following line:

txtName.Text = drCurrent["Term Name"].ToString();

I think I've narrowed it down to the fact that drCurrent is giving a
null reference, but I don't know why or how to fix it.

Can anyone help?

Thanks,

Mark.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top