when I update my row and provide plenty of data, click on update the
sub crashes. through the debugger I noticed that not a single cell
value is recognized. are cells not existent on update...or are the not accessible?
Protected Sub AlternativeUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridViewAlternatives.RowUpdating
Dim selectRow As GridViewRow = GridViewAlternatives.Rows(e.RowIndex)
Dim x As String = selectRow.Cells(0).Text
Dim y As String = selectRow.Cells(1).Text
SqlDataSourceGetAlternatives.UpdateParameters("NewAlternative").DefaultValue = selectRow.Cells(0).Text
SqlDataSourceGetAlternatives.UpdateParameters("NewAlternativeImage").DefaultValue = selectRow.Cells(2).Text
SqlDataSourceGetAlternatives.UpdateParameters("DSessionID").DefaultValue = Request.QueryString("strDSessionID")
SqlDataSourceGetAlternatives.UpdateParameters("AlternativeNumber").DefaultValue = GridViewAlternatives.DataKeys(e.RowIndex).Value
SqlDataSourceGetAlternatives.Update()
End Sub
Well first off put a try/catch around the whole of that function so you can at least catch and handle the exception. Being able to see the exception will give you better insight into the cause of the problem as you'll be able to see the message/exception
typ. Do the NewAlternative and NewAlternativeImage fields exist in your <UpdateParameters> section? If you could please provide the <UpdateParameters> section from your DataSource object so that we can verify the available parameters are indeed available.
If you will let us know that data and any exception information you get after wrapping that code in the try/catch so we can better see what the exact error is.
______________________________________
Robert Lindley | Sr. Software Engineer - MCP, MCAD
ASTRA Innovations, Ltd. - Better Solutions by Design.
I wanted to verify that the parameter actually existed, but if you're saying it works minus the row's cell is returning empty text then try this if the cell contains a textbox.
There is a hidden gem in the datacontrols that i'm sure not many people know about, i'll be blogging about it soon. Extracting values from the grid is a pain, going through cells and trying to get the text that may be encoded or formatted can be tough sometimes.
There is a function on GridView ExtractRowValues, which is unfortunately protected that does all the heavy lifting for you and knows how to extract values from the known types of fields. You have 4 options:
<div>Try to figure out the correct cell index and continue what you are doing</div>
<div>Put everything in DataKeyNames and use the DataKeys[rowIndex][field] property to get the values you need (kind of a hack but it works)</div>
<div>Use private reflection to call ExtractRowValues on the GridView</div>
<div>Derive a custom gridview and expose the ExtractRowValues function to through a public method</div>
Hope this helps
David Fowler
Senior SDE, ASP.NET Team, Microsoft
Marked as answer by polynaux on Dec 14, 2008 04:52 PM
I might shudder about my code as well once I have seen your blog.
I will try options 2. thats seems the quickest to me.
"Put everything in DataKeyNames and use the DataKeys[rowIndex][field] property to get the values you need (kind of a hack but it works)"
My gridview has a select, edit and delete buttoon (auto-generated). The update SQL itself is working
as I have tried with test strings "". Nevertheless, when I want
to get what I have typed in my gridview in edit mode I am failing. Quite odd since all the methods I tried (the dim x... strings) have worked on other pages! :
Protected Sub Location_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridViewLocations.RowUpdating
Dim x1 As String = GridViewLocations.DataKeys(e.RowIndex)("LocationPhoto").ToString() 'gets value that was there before I typed in something new. How is that possible?
Dim x2 As String = e.NewValues("LocationDescription") 'GridViewLocations.DataKeys(e.RowIndex)("LocationDescription").ToString()
Dim x22 As String = (CType((selectedRow.Cells(4).Controls(0)), LiteralControl)).Text
Dim x23 As String = (CType((selectedRow.Cells(3).Controls(0)), TextBox)).Text
Dim x3 As String = GridViewLocations.DataKeys(e.RowIndex)("LocationID").ToString()
SqlDataSourceLocations.UpdateParameters("LocationID").DefaultValue = GridViewLocations.DataKeys(e.RowIndex)("LocationID").ToString()
SqlDataSourceLocations.UpdateParameters("LocationName").DefaultValue = GridViewLocations.DataKeys(e.RowIndex)("LocationName").ToString()
SqlDataSourceLocations.UpdateParameters("LocationDescription").DefaultValue = GridViewLocations.DataKeys(e.RowIndex)("LocationDescription").ToString()
SqlDataSourceLocations.UpdateParameters("LocationPhoto").DefaultValue = GridViewLocations.DataKeys(e.RowIndex)("LocationPhoto").ToString()
SqlDataSourceLocations.UpdateParameters("UserID").DefaultValue = strUserID
SqlDataSourceLocations.Update()
End Sub
polynaux
Member
195 Points
775 Posts
Empty cells for GridView.Rows(e.RowIndex).cells(9)
Dec 10, 2008 08:14 PM|LINK
Hi Guys,
when I update my row and provide plenty of data, click on update the
sub crashes. through the debugger I noticed that not a single cell
value is recognized. are cells not existent on update...or are the not accessible?
Protected Sub AlternativeUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridViewAlternatives.RowUpdating Dim selectRow As GridViewRow = GridViewAlternatives.Rows(e.RowIndex) Dim x As String = selectRow.Cells(0).Text Dim y As String = selectRow.Cells(1).Text SqlDataSourceGetAlternatives.UpdateParameters("NewAlternative").DefaultValue = selectRow.Cells(0).Text SqlDataSourceGetAlternatives.UpdateParameters("NewAlternativeImage").DefaultValue = selectRow.Cells(2).Text SqlDataSourceGetAlternatives.UpdateParameters("DSessionID").DefaultValue = Request.QueryString("strDSessionID") SqlDataSourceGetAlternatives.UpdateParameters("AlternativeNumber").DefaultValue = GridViewAlternatives.DataKeys(e.RowIndex).Value SqlDataSourceGetAlternatives.Update() End Suba
Rampidbyter
Participant
944 Points
157 Posts
Re: Empty cells for GridView.Rows(e.RowIndex).cells(9)
Dec 11, 2008 12:42 AM|LINK
Well first off put a try/catch around the whole of that function so you can at least catch and handle the exception. Being able to see the exception will give you better insight into the cause of the problem as you'll be able to see the message/exception typ. Do the NewAlternative and NewAlternativeImage fields exist in your <UpdateParameters> section? If you could please provide the <UpdateParameters> section from your DataSource object so that we can verify the available parameters are indeed available. If you will let us know that data and any exception information you get after wrapping that code in the try/catch so we can better see what the exact error is.
Robert Lindley | Sr. Software Engineer - MCP, MCAD
ASTRA Innovations, Ltd. - Better Solutions by Design.
--------------------------------------------------
I like points just as much as the next person. If I helped you out please help me out by marking my response as the ANSWER if or when it is.
polynaux
Member
195 Points
775 Posts
Re: Empty cells for GridView.Rows(e.RowIndex).cells(9)
Dec 11, 2008 09:41 AM|LINK
Hello Robert,
what I can tell you right now is that the issue is not that the update is not working (thats the next stage)
but that
SqlDataSourceGetAlternatives.UpdateParameters("NewAlternative").DefaultValue = selectRow.Cells(0).Text
results in "".
The problem is my data binding. I am unable to bind the value of cell(0) to the parameter.
Rampidbyter
Participant
944 Points
157 Posts
Re: Empty cells for GridView.Rows(e.RowIndex).cells(9)
Dec 11, 2008 09:58 AM|LINK
I wanted to verify that the parameter actually existed, but if you're saying it works minus the row's cell is returning empty text then try this if the cell contains a textbox.
. . . = (CType((selectRow.Cells(1).Controls(0)), TextBox)).Text
Robert Lindley | Sr. Software Engineer - MCP, MCAD
ASTRA Innovations, Ltd. - Better Solutions by Design.
--------------------------------------------------
I like points just as much as the next person. If I helped you out please help me out by marking my response as the ANSWER if or when it is.
polynaux
Member
195 Points
775 Posts
Re: Empty cells for GridView.Rows(e.RowIndex).cells(9)
Dec 11, 2008 08:38 PM|LINK
Hi Robert,
thanks a lot, that worked for that case!
I have the same issue with that code, instead of a SQLdatasource I use
a OBJECTdatasource that takes the update parameters from the gridview:
<asp:TemplateField HeaderText="Description" SortExpression="LocationDescription"> <EditItemTemplate> <asp:TextBox ID="TextBoxLocationDescribtionUpdate" runat="server" TextMode="MultiLine" Text='<%# Bind("LocationDescription") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:TextBox ID="TextBoxLocaitonDescribtionItem" runat="server" TextMode="MultiLine" Enabled="False" Text='<%# Bind("LocationDescription") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField>'ERROR
I just wonder why your solution does not work in this, second, case? The error I am getting is "input in incorrect format..."
davidfowl
Contributor
2699 Points
611 Posts
Microsoft
Re: Empty cells for GridView.Rows(e.RowIndex).cells(9)
Dec 11, 2008 08:49 PM|LINK
I shudder when i see code like this.
There is a hidden gem in the datacontrols that i'm sure not many people know about, i'll be blogging about it soon. Extracting values from the grid is a pain, going through cells and trying to get the text that may be encoded or formatted can be tough sometimes. There is a function on GridView ExtractRowValues, which is unfortunately protected that does all the heavy lifting for you and knows how to extract values from the known types of fields. You have 4 options:
Hope this helps
Senior SDE, ASP.NET Team, Microsoft
polynaux
Member
195 Points
775 Posts
Re: Empty cells for GridView.Rows(e.RowIndex).cells(9)
Dec 12, 2008 09:29 AM|LINK
Hi David Fowler,
I might shudder about my code as well once I have seen your blog.
I will try options 2. thats seems the quickest to me.
"Put everything in DataKeyNames and use the DataKeys[rowIndex][field] property to get the values you need (kind of a hack but it works)"
Please keep us informed about you blog.
polynaux
Member
195 Points
775 Posts
Re: Empty cells for GridView.Rows(e.RowIndex).cells(9)
Dec 12, 2008 12:14 PM|LINK
polynaux
Member
195 Points
775 Posts
Re: Empty cells for GridView.Rows(e.RowIndex).cells(9)
Dec 12, 2008 12:19 PM|LINK
I still have not solved this issue :(
My gridview has a select, edit and delete buttoon (auto-generated). The update SQL itself is working
as I have tried with test strings "". Nevertheless, when I want
to get what I have typed in my gridview in edit mode I am failing. Quite odd since all the methods I tried (the dim x... strings) have worked on other pages! :
Protected Sub Location_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridViewLocations.RowUpdating Dim x1 As String = GridViewLocations.DataKeys(e.RowIndex)("LocationPhoto").ToString() 'gets value that was there before I typed in something new. How is that possible? Dim x2 As String = e.NewValues("LocationDescription") 'GridViewLocations.DataKeys(e.RowIndex)("LocationDescription").ToString() Dim x22 As String = (CType((selectedRow.Cells(4).Controls(0)), LiteralControl)).Text Dim x23 As String = (CType((selectedRow.Cells(3).Controls(0)), TextBox)).Text Dim x3 As String = GridViewLocations.DataKeys(e.RowIndex)("LocationID").ToString() SqlDataSourceLocations.UpdateParameters("LocationID").DefaultValue = GridViewLocations.DataKeys(e.RowIndex)("LocationID").ToString() SqlDataSourceLocations.UpdateParameters("LocationName").DefaultValue = GridViewLocations.DataKeys(e.RowIndex)("LocationName").ToString() SqlDataSourceLocations.UpdateParameters("LocationDescription").DefaultValue = GridViewLocations.DataKeys(e.RowIndex)("LocationDescription").ToString() SqlDataSourceLocations.UpdateParameters("LocationPhoto").DefaultValue = GridViewLocations.DataKeys(e.RowIndex)("LocationPhoto").ToString() SqlDataSourceLocations.UpdateParameters("UserID").DefaultValue = strUserID SqlDataSourceLocations.Update() End SubThe gridview:
the datasource: