Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 06, 2012 03:03 PM by Wozer
Nov 05, 2012 06:14 PM|LINK
I have a gridview that is populated from a slq datasource in the page.load with
Gridview1.Datasource = sdsDocs
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)
Nov 05, 2012 06:23 PM|LINK
Nov 05, 2012 06:31 PM|LINK
Nov 05, 2012 07:57 PM|LINK
You need to set the DataSourceId property (not DataSource) to sdsDocs, since it's a SqlDataSource.
This did not fix the issue, it is still giving the incorrect ID value.
Nov 05, 2012 08:23 PM|LINK
Nov 06, 2012 01:11 PM|LINK
Tried this as well, and it is still giving the wrong ID
Nov 06, 2012 01:30 PM|LINK
Nov 06, 2012 01:42 PM|LINK
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.
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?
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..