Help, NullReferenceException

J

Jeff

Hey

asp.net 2.0

The line "int selectedID =
Convert.ToInt32(gvQuestions.SelectedDataKey.Value.ToString());" in the code
below gives an NullReferenceException (Object reference not set to an
instance of an object.)

protected void gvQuestions_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int selectedID =
Convert.ToInt32(gvQuestions.SelectedDataKey.Value.ToString());
if (Request.QueryString["question"] == null)
{
Response.Redirect(Request.Url.ToString() + "?id=" +
selectedID.ToString());
}
else
{
}
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message.ToString());
}
}


Any ideas what I'm doing wrong here?
 
M

Morten Wennevik

Hi Jeff,

Either SelectedDataKey is null or gvQuestions is null. Seeing that this
is an event method for gvQuestions I would assume SelectedDataKey ==
null. The documentations indicate that this will happen if no row is
selected, or possibly that DataKeyNames is not set.


Hey

asp.net 2.0

The line "int selectedID =
Convert.ToInt32(gvQuestions.SelectedDataKey.Value.ToString());" in the
code
below gives an NullReferenceException (Object reference not set to an
instance of an object.)

protected void gvQuestions_SelectedIndexChanged(object sender, EventArgs
e)
{
try
{
int selectedID =
Convert.ToInt32(gvQuestions.SelectedDataKey.Value.ToString());
if (Request.QueryString["question"] == null)
{
Response.Redirect(Request.Url.ToString() + "?id=" +
selectedID.ToString());
}
else
{
}
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message.ToString());
}
}


Any ideas what I'm doing wrong here?
 
J

Jeff

Hey Morten!

the DataKeyNames property is set to the primary key field in the datasource

And also:
When selecting a row in the GridView, the SelectedIndexChanged event isn't
triggered. The row I clicked on isn't marked as selected. I have enabled
selection on the GridView

So the SelectedIndexChanged event I sent in the previous post isn't executed
everytime...

Any suggestions?






Hi Jeff,

Either SelectedDataKey is null or gvQuestions is null. Seeing that this
is an event method for gvQuestions I would assume SelectedDataKey ==
null. The documentations indicate that this will happen if no row is
selected, or possibly that DataKeyNames is not set.


Hey

asp.net 2.0

The line "int selectedID =
Convert.ToInt32(gvQuestions.SelectedDataKey.Value.ToString());" in the
code
below gives an NullReferenceException (Object reference not set to an
instance of an object.)

protected void gvQuestions_SelectedIndexChanged(object sender, EventArgs
e)
{
try
{
int selectedID =
Convert.ToInt32(gvQuestions.SelectedDataKey.Value.ToString());
if (Request.QueryString["question"] == null)
{
Response.Redirect(Request.Url.ToString() + "?id=" +
selectedID.ToString());
}
else
{
}
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message.ToString());
}
}


Any ideas what I'm doing wrong here?
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top