I am looking to store the value from the selected record in a GridView so that I can Navigate to a new page with this as a session variable.
I have tried many variations of the code (held in the master page) but get a variation of issue. The simplified version of the code below returns and issue that GridView1 is not declared. As you can see from the Sub my gridview is called Grid view1. If I
uncomment the Dim statement for Gridview1 and run the screen i get "Exception Details:
System.NullReferenceException: Object reference not set to an instance of an object"
Protected Sub GridView1_SelectedIndexChanged(ByVal sender
As Object,
ByVal e As System.EventArgs)
' Dim
Gridview1
As GridView
Dim NewText
As String
NewText = GridView1.Cells(0)
Session("Table_ID") = NewText
Response.Redirect(
"../Newpage.aspx")
End Sub
I have also tried variations on what to put in place of the system.EventsArgs and replacing
"NewText = GridView1.Cells(0)"
with
"NewText = e.item.Cells(0)"
as I have seen e.item in other examples but the item does not seem to be a member of
System.EventArgs or the other variations I use (i.e
GridViewCommandEventArgs)
However DataGridItemEventArgs does allow e.items.Cells(0) but returns the error that it "
does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs) "
and I get an error that GridView1 is not Declared, so in the same sub I set
Dim
Gridview1 As GridView
However it now warns me that
Variable 'GridView1' is used befor it has been assigned a value. A null reference exception could result at runtime.
Which it does.
Could part of the issue be that it is set in the Master page? also another quirk but probably not affect this issue is that this GridView has 2 dataKeys set as it is using two joined tables.
Hi,
When you were using MasterPages, you need to find the GridView control and its not that you declare a variable as GridView.
Have a look at this article,
MasterPages and FindControl()
You can have multiple datakeys defined for a GridView.
Got there in the end thanks to your help. I was using the Findcontrol command, but was doing this in the Master document and not the child document. I have now moved the source to the child document and this now works well.
crippsy
Member
302 Points
60 Posts
Storing the Key from GridView
Jul 02, 2006 03:21 PM|LINK
I am looking to store the value from the selected record in a GridView so that I can Navigate to a new page with this as a session variable.
I have tried many variations of the code (held in the master page) but get a variation of issue. The simplified version of the code below returns and issue that GridView1 is not declared. As you can see from the Sub my gridview is called Grid view1. If I uncomment the Dim statement for Gridview1 and run the screen i get "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object"
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
' Dim
Gridview1 As GridView Dim NewText As StringNewText = GridView1.Cells(0)
Session("Table_ID") = NewTextResponse.Redirect(
"../Newpage.aspx") End SubI have also tried variations on what to put in place of the system.EventsArgs and replacing
"NewText = GridView1.Cells(0)"
with
"NewText = e.item.Cells(0)"
as I have seen e.item in other examples but the item does not seem to be a member of System.EventArgs or the other variations I use (i.e GridViewCommandEventArgs)
However DataGridItemEventArgs does allow e.items.Cells(0) but returns the error that it " does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs) "
Any advice would be most greatfully recieved
e_screw
All-Star
19528 Points
3893 Posts
Re: Storing the Key from GridView
Jul 02, 2006 05:05 PM|LINK
Set the DataKeyNames of the GridView to the primaryKey of your data, so you can access the value of the selected DataKey in your code this way..
GridView1.SelectedDataKey.Value
Thanks
Electronic Screw
Website||Blog||Dub@i.net
crippsy
Member
302 Points
60 Posts
Re: Storing the Key from GridView
Jul 02, 2006 06:54 PM|LINK
Thanks for the advice and such a quick responce.
First of all I set I set
NewText = GridView1.SelectedDataKey.Values
and I get an error that GridView1 is not Declared, so in the same sub I set
Dim
Gridview1 As GridViewHowever it now warns me that
Variable 'GridView1' is used befor it has been assigned a value. A null reference exception could result at runtime.
Which it does.
Could part of the issue be that it is set in the Master page? also another quirk but probably not affect this issue is that this GridView has 2 dataKeys set as it is using two joined tables.
Any further views,?
Thanks in advance.
e_screw
All-Star
19528 Points
3893 Posts
Re: Storing the Key from GridView
Jul 02, 2006 07:07 PM|LINK
When you were using MasterPages, you need to find the GridView control and its not that you declare a variable as GridView.
Have a look at this article, MasterPages and FindControl()
You can have multiple datakeys defined for a GridView.
Thanks
Electronic Screw
Website||Blog||Dub@i.net
crippsy
Member
302 Points
60 Posts
Re: Storing the Key from GridView
Jul 03, 2006 10:22 PM|LINK
Got there in the end thanks to your help. I was using the Findcontrol command, but was doing this in the Master document and not the child document. I have now moved the source to the child document and this now works well.
Many thanks again
Simon
ReyN
Contributor
2148 Points
413 Posts
Re: Storing the Key from GridView
Jul 04, 2006 06:41 AM|LINK
with DataKeyNames, you can simply use
GridView.SelectedValue [;)]
aspxtreme