|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Help, NullReferenceExceptionasp.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? 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] 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 Either SelectedDataKey is null or gvQuestions is null. Seeing that thisnews:op.tfgjgtw5klbvpo@tr024.bouvet.no... Hi Jeff, 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]
Other interesting topics
WAP, VS2005 keeps changing my user control types!!
Howto inherit from an exisiting webcontrol in VS 2005 CSS examples Intellisense with web.config registration of controls XML PROBLEM HELP NEEDED URGENTLY problem with GridView Page hang after updating web.config ASP.net using old version of DLLs Making New web site by using ASP.Net 2005 date in datagrid |
|||||||||||||||||||||||