473,472 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help, NullReferenceException

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.Messa ge.ToString());
}
}
Any ideas what I'm doing wrong here?
Sep 6 '06 #1
2 1603
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.comwrote:
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.Messa ge.ToString());
}
}
Any ideas what I'm doing wrong here?



--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 6 '06 #2
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" <Mo************@hotmail.comwrote in message
news:op***************@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.comwrote:
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.Messa ge.ToString());
}
}
Any ideas what I'm doing wrong here?



--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
5
by: TT (Tom Tempelaere) | last post by:
Hi, Once in a while my application throws an NullReferenceException at startup, however it appears to be occurring in an unknown module. If I debug it and ask to 'break', then there is no source...
1
by: Ghada Ayed via DotNetMonster.com | last post by:
Hello every body, please help me in this problem i have written the following C# code in VS.Net and when i run the window application i have got 'System.NullReferenceException' so please if...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
0
by: noe | last post by:
I have done a DLL in unmanaged code C++ and in that dll I have defined a function that use memcpy.This dll run ok.I use that dll in a aplication in C# managed code in Visual Studio .NET. I import...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
6
by: William Mild | last post by:
I must be getting brain fried. I can't see the error. Create a new web form with the following code begind: Public Class test Inherits System.Web.UI.Page Public Class ReportCardData ...
9
by: Xero | last post by:
could anybody describes a scenario where this error will occur? i have visited the page about the NullReferenceException in msdn library but i still have no idea what it is about. thanks. ...
1
by: Jeff | last post by:
hey asp.net 2.0 The line "e.Item.Parent.ChildItems.Remove(e.Item);" gives this error: 'e.Item' threw an exception of type 'System.NullReferenceException' System.Web.UI.WebControls.MenuItem...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.