Object reference not set to an instance of an object

  • Thread starter Max Gay via .NET 247
  • Start date
M

Max Gay via .NET 247

I am having a problem with a page that I am trying to develop. Iam new at asp.net.
The page I have created makes a datagrid that has two links aedit and a cancel link. When you click either of the links itthrows the following error "Object reference noet set to aninstance of an object. The following lines appear along withthat error message
Line 69: Dim CurrentTextBox AsSystem.Web.UI.WebControls.Textbox
Line 70: CurrentTextBox=E.Item.FindControl("edit_" &Cols(I))
Line 71: Dim ColValue As String = CurrentTextBox.Text
Line 72: MyCommand.Parameters("@" &Cols(I)).Value=ColValue
Line 73: Next

I forgot to mention that this is part of a For Next Loop
What could be causing this--I can post more code if needed
Thank you in advance
Max
 
K

Kevin Spencer

Okay, Max, let's walk through this, and see what the possible candidates
are. In case you're not sure, this exception means that you've tried to
treat some variable that is null (Nothing) as an object of the type that you
thought it was.

Line 69: Dim CurrentTextBox As System.Web.UI.WebControls.Textbox

This line declares a variable. There is no object reference here, only a
type reference.

Line 70: CurrentTextBox=E.Item.FindControl("edit_" & Cols(I))

This line assigns the result of the FindControl method to your variable. It
could not throw an exception, because if the object is not found, the method
returns null (Nothing). That is a valid value for any object variable.

Line 71: Dim ColValue As String = CurrentTextBox.Text

This is a very likely candidate. You can't treat Nothing like a TextBox. It
has no TextBox properties. In fact, it has no properties at all. However,
there is one other possibility:

Line 72: MyCommand.Parameters("@" & Cols(I)).Value=ColValue

If MyCommand is Nothing, or MyCommand.Parameters is Nothing, or
MyCommand.Parameters("@" & Cols(i)) is Nothing, this could also be the
offending line.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

I am having a problem with a page that I am trying to develop. I am new at
asp.net.
The page I have created makes a datagrid that has two links a edit and a
cancel link. When you click either of the links it throws the following
error "Object reference noet set to an instance of an object. The following
lines appear along with that error message
Line 69: Dim CurrentTextBox As System.Web.UI.WebControls.Textbox
Line 70: CurrentTextBox=E.Item.FindControl("edit_" & Cols(I))
Line 71: Dim ColValue As String = CurrentTextBox.Text
Line 72: MyCommand.Parameters("@" & Cols(I)).Value=ColValue
Line 73: Next

I forgot to mention that this is part of a For Next Loop
What could be causing this--I can post more code if needed
Thank you in advance
Max
 

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