The data source has a parameter in it that will restrict what is pulled from the database and displayed on the page. eg. If the datasource would usually pull items with ID values of 1,2,3,4,5,6,7,8 If this parameter is 1 it will, for example, only pull 2,3,7. If this is the case, and I click the view button for the item that has an ID of 7, it will actually pull the ID of 3.
The weird thing is, if I remove the databind from the page.load and instead go to the gridview, click the little arrow, and then say Choose Data Source: and select sdsDocs, then this behaviour does not repeat itself. Click the item with an ID of 7 when the parameter is 1, will give me an alert showing 7.
So best I can figure out is something is going on with my databind in the code behind that is causing the ID's to have the wrong values. Any help anyone might be able to provide would be greatly appreciated.
I am getting the DataKey Value like this:
Protected Sub Gridview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "View" Then
Dim rowIndex As Integer = CType(e.CommandArgument, Integer)
Dim documentIDValue As Integer = CType(GridView1.DataKeys(rowIndex).Value, Integer)
Functions.Alert(documentIDValue)
End Sub
Like I said in my first post, this only occurs when the gridview is bound in the code behind. If it is bound from the little arrow and choosing the datasource from the front end, then this does not occur.
I am not sure how the same datasource using the same stored procedure, is pulling different data, with the same parameters. Looking at the SqlProfiler, the data being pulled is fine, it is something going on when the data is being bound.
Ok I think I may have figured out what is going on. I ran SQLProfiler when I clicked the view button (still just alerting the ID value) Looking back through, the stored procedure is being run twice.
The first time, which seems to be running when I click the view button, is showing up with the parameter that limits what is shown equal to NULL.
Then a few miliseconds later, the stored procedure is being run again, this time with the parameter being equal to 1.
So it seems that instead of getting the ID value from the data on the page, it is going through the datasource again.
This function is done for you when using the designer if you select the data source as the data source directly copies the columns, in the code behind manual assign the column which is holds the primary key by which to sort..
Wozer
Member
390 Points
267 Posts
Gridview giving wrong DataKey Value
Nov 05, 2012 06:14 PM|LINK
Hi,
I have a gridview that is populated from a slq datasource in the page.load with
The data source has a parameter in it that will restrict what is pulled from the database and displayed on the page. eg. If the datasource would usually pull items with ID values of 1,2,3,4,5,6,7,8 If this parameter is 1 it will, for example, only pull 2,3,7. If this is the case, and I click the view button for the item that has an ID of 7, it will actually pull the ID of 3.
The weird thing is, if I remove the databind from the page.load and instead go to the gridview, click the little arrow, and then say Choose Data Source: and select sdsDocs, then this behaviour does not repeat itself. Click the item with an ID of 7 when the parameter is 1, will give me an alert showing 7.
So best I can figure out is something is going on with my databind in the code behind that is causing the ID's to have the wrong values. Any help anyone might be able to provide would be greatly appreciated.
I am getting the DataKey Value like this:
Protected Sub Gridview1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand If e.CommandName = "View" Then Dim rowIndex As Integer = CType(e.CommandArgument, Integer) Dim documentIDValue As Integer = CType(GridView1.DataKeys(rowIndex).Value, Integer) Functions.Alert(documentIDValue) End SubThanks again,
Chris
MetalAsp.Net
All-Star
112157 Points
18249 Posts
Moderator
Re: Gridview giving wrong DataKey Value
Nov 05, 2012 06:23 PM|LINK
RameshRajend...
Star
7983 Points
2099 Posts
Re: Gridview giving wrong DataKey Value
Nov 05, 2012 06:31 PM|LINK
Hai
Check it
http://www.nickgerne.com/2011/10/get-datakey-value-from-selectedindexchanged/
Wozer
Member
390 Points
267 Posts
Re: Gridview giving wrong DataKey Value
Nov 05, 2012 07:57 PM|LINK
This did not fix the issue, it is still giving the incorrect ID value.
Chris
oned_gk
All-Star
31515 Points
6433 Posts
Re: Gridview giving wrong DataKey Value
Nov 05, 2012 08:23 PM|LINK
Wozer
Member
390 Points
267 Posts
Re: Gridview giving wrong DataKey Value
Nov 06, 2012 01:11 PM|LINK
Tried this as well, and it is still giving the wrong ID
oned_gk
All-Star
31515 Points
6433 Posts
Re: Gridview giving wrong DataKey Value
Nov 06, 2012 01:30 PM|LINK
Wozer
Member
390 Points
267 Posts
Re: Gridview giving wrong DataKey Value
Nov 06, 2012 01:42 PM|LINK
oned,
Like I said in my first post, this only occurs when the gridview is bound in the code behind. If it is bound from the little arrow and choosing the datasource from the front end, then this does not occur.
I am not sure how the same datasource using the same stored procedure, is pulling different data, with the same parameters. Looking at the SqlProfiler, the data being pulled is fine, it is something going on when the data is being bound.
Wozer
Member
390 Points
267 Posts
Re: Gridview giving wrong DataKey Value
Nov 06, 2012 02:13 PM|LINK
Ok I think I may have figured out what is going on. I ran SQLProfiler when I clicked the view button (still just alerting the ID value) Looking back through, the stored procedure is being run twice.
The first time, which seems to be running when I click the view button, is showing up with the parameter that limits what is shown equal to NULL.
Then a few miliseconds later, the stored procedure is being run again, this time with the parameter being equal to 1.
So it seems that instead of getting the ID value from the data on the page, it is going through the datasource again.
Any Ideas why this might be happening?
Chris
PashBash
Member
371 Points
81 Posts
Re: Gridview giving wrong DataKey Value
Nov 06, 2012 02:46 PM|LINK
In your code when you bind your data.
Set the Key column manual in the code behind.
This function is done for you when using the designer if you select the data source as the data source directly copies the columns, in the code behind manual assign the column which is holds the primary key by which to sort..