Home All Groups Group Topic Archive Search About

Help, NullReferenceException

Author
6 Sep 2006 12:26 PM
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?

Author
6 Sep 2006 12:54 PM
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 =3D=3D=
  =

null.  The documentations indicate that this will happen if no row is  =

selected, or possibly that DataKeyNames is not set.


On Wed, 06 Sep 2006 14:26:52 +0200, Jeff  =

<it_consultant1@NO_SPAM.hotmail.com> wrote:

Show quoteHide quote
> Hey
>
> asp.net 2.0
>
> The line "int selectedID =3D
> 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, EventAr=
gs  =

> e)
> {
>   try
>   {
>     int selectedID =3D
> Convert.ToInt32(gvQuestions.SelectedDataKey.Value.ToString());
>     if (Request.QueryString["question"] =3D=3D null)
>     {
>       Response.Redirect(Request.Url.ToString() + "?id=3D" +
> selectedID.ToString());
>     }
>     else
>     {
>     }
>   }
>   catch (Exception exception)
>   {
>     System.Diagnostics.Debug.WriteLine(exception.Message.ToString());
>   }
> }
>
>
> Any ideas what I'm doing wrong here?
>
>



-- =

Happy Coding!
Morten Wennevik [C# MVP]
Are all your drivers up to date? click for free checkup

Author
6 Sep 2006 1:21 PM
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?






"Morten Wennevik" <MortenWenne***@hotmail.com> wrote in message
news:op.tfgjgtw5klbvpo@tr024.bouvet.no...
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.


On Wed, 06 Sep 2006 14:26:52 +0200, Jeff
<it_consultant1@NO_SPAM.hotmail.com> wrote:

Show quoteHide quote
> 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?
>
>



--
Happy Coding!
Morten Wennevik [C# MVP]

Bookmark and Share