Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error with the FIND command? in DataSet

Status
Not open for further replies.

NightZEN

Programmer
Apr 29, 2003
142
US
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:

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!
 
The main reason you are getting the error is described in the line which says "Input string was not in a correct format". One of the values that has been entered is not in the format that the code expects and so is chucking out an error.

I would suggest stepping through the code, finding which line the code is erroring on and then look at the values that the line is dealing with.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top