I have a DataGrid with an Edit/Update Column. I press the Edit Button and the row changes to Text boxes and DropDown Lists as designed. When I then press the Update button the following code runs:
Then I get this error:
Server Error in '/syronpress' Application.
--------------------------------------------------------------------------------
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 462: da_press.Fill(Ds_press)
Line 463: da_type.Fill(Ds_press)
Line 464: Dim objEditedRow As DataRow = Ds_press.Tables(0).Rows.Find( _
Line 465: CType(e.Item.Cells(1).Controls(0), TextBox).Text)
etc.
This code is largeley recycled from a book, and I have had trouble with other snippets from the book and have been able to figure them out, but I can't seem to crack this one. Any ideas would be most appreciated!
Code:
Private Sub dg_press_detail_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg_press_detail.UpdateCommand
' Step one: Put dropdown list value into foreign key field
CType(e.Item.Cells(4).Controls(0), TextBox).Text = _
CType(e.Item.FindControl("ddl2"), DropDownList).SelectedItem.Value()
' Step two: Fill DataSet and identify row to edit
da_press.SelectCommand.Parameters.Add("@cust", SqlDbType.Int).Direction = ParameterDirection.Input
da_press.SelectCommand.Parameters("@cust").Value = Session("CustomerKey")
da_press.Fill(Ds_press)
da_type.Fill(Ds_press)
Dim objEditedRow As DataRow = Ds_press.Tables(0).Rows.Find( _
CType(e.Item.Cells(1).Controls(0), TextBox).Text)
' Step three: Cycle through valid "data" cells and put
' information back in underlying DataSet
Dim intCount As Integer
For intCount = 0 To e.Item.Cells.Count - 1
If e.Item.Cells(intCount).Controls.Count > 0 Then
If TypeOf (e.Item.Cells(intCount).Controls(0)) Is TextBox Then
' This appears to be a TextBox-holding "data" cell
Dim strValue As String = CType(e.Item.Cells(intCount). _
Controls(0), TextBox).Text
' Put value (or null if empty) back into relevant DataSet field
If strValue = "" Then
objEditedRow.Item(dg_press_detail.Columns(intCount). _
SortExpression) = System.DBNull.Value
Else
objEditedRow.Item(dg_press_detail.Columns(intCount). _
SortExpression) = strValue
End If
End If
End If
Next
' Update backend data
da_press.Update(Ds_press)
' Deselect DataGrid items and rebind
With dg_press_detail
.SelectedIndex = -1
.EditItemIndex = -1
.DataSource = Ds_press
.DataBind()
End With
End Sub
Then I get this error:
Server Error in '/syronpress' Application.
--------------------------------------------------------------------------------
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 462: da_press.Fill(Ds_press)
Line 463: da_type.Fill(Ds_press)
Line 464: Dim objEditedRow As DataRow = Ds_press.Tables(0).Rows.Find( _
Line 465: CType(e.Item.Cells(1).Controls(0), TextBox).Text)
etc.
This code is largeley recycled from a book, and I have had trouble with other snippets from the book and have been able to figure them out, but I can't seem to crack this one. Any ideas would be most appreciated!