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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input string was not in correct format (UpdateCommand event)

Status
Not open for further replies.

Mike555

Technical User
Feb 21, 2003
1,200
US
I have a webform datagrid that errors out on the UpdateCommand event. The error occurs on the green line below, and the error is "Input string was not in a correct format." FlitchNum is the PK of the table bound to datagrid, and is also the 2'nd column of the datagrid. The datatype of FlitchNum is varchar. Is this error occuring because I need to format the green-line below to handle a varchar data type differently? Could someone help me understand why this error is occuring? Thanks.


Code:
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
    Handles DataGrid1.UpdateCommand

        Dim DDL As DropDownList = _
          CType(e.Item.Cells(15).Controls(1), DropDownList)
        Dim NewSpecie As Integer = DDL.SelectedValue
        [COLOR=green][b]Dim FlitchNum As String = Int32.Parse(e.Item.Cells(1).Text)[/b][/color]
        Dim SQL As String = _
          "UPDATE tblLogs SET SpecieID=@SelectSpecie WHERE OrderID=@ID"
        Dim ConnStr As String
        ConnStr = "workstation id=LAPTOP;packet size=4096;integrated security=SSPI;data source=servername;persist security info=False;initial catalog=dbasename"
        Dim Conn As SqlConnection = New SqlConnection(ConnStr)
        Dim Cmd As New SqlCommand(SQL, Conn)
        Cmd.Parameters.Add(New SqlParameter("@SelectSpecie", NewSpecie))
        Cmd.Parameters.Add(New SqlParameter("@ID", FlitchNum))
        Conn.Open()
        Cmd.ExecuteNonQuery()
        Conn.Close()
        DataGrid1.EditItemIndex = -1
        DataGrid1.DataBind()

    End Sub

--
Mike
 
Dim FlitchNum As String = Int32.Parse(e.Item.Cells(1).Text)"

Why do you "parse" a string into an integer just to convert it back to a string?
In any case, you will get that error if the .Text = "", if it is non-numeric, if it is > 32767 or < -32768. What is in that "e.Item.Cells(1).Text" field.


Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
e.Item.Cells(1).Text" field is the second column in the datagrid which contains the PK (which is data type varchar) of the dataset. The dataset contains 3 records: 77848, 4548, 8788. Based on what you said, the 77848 value is probably causing the error. Could you suggest what I might review to fix this?

I know that varchar is not the optimal choice for PKs, but unfortunately for now I have to make it work.

Thanks for your reply.

--
Mike
 
Why not
Dim FlitchNum As String = e.Item.Cells(1).Text
If not that then
Dim FlitchNum As String = Int64.Parse(e.Item.Cells(1).Text)
OR
Dim FlitchNum As String = Decimal.Parse(e.Item.Cells(1).Text)

I still do not understand the need to for from string, to int64 back to string.



Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
I still do not understand the need to for from string, to int64 back to string.

Are you saying that the dimension should be all that is needed?

Thanks.

--
Mike
 
I had a similar problem with the DateTime column while updating the datagrid! i have fixed it by placing a default value for dates instead of NULLS! so if you can update a value (for eg 0) in the database for all the nulls that might fix your problem
 
Since I'm working with a Primary Key, there are no nulls in in the db that could be causing me problems.

--
Mike
 
Dim FlitchNum As String
FlitchNum is a STRING

e.Item.Cells(1).Text is a STRING

So what is the point of parsing a string into any kind of number when the result is just going to be implicitly converted back to string
Ex.
Dim FlitchNum As String = Int32.Parse(e.Item.Cells(1).Text)
is equivalent to

Dim intW as Integer = _
Int32.Parse(e.Item.Cells(1).Text
Dim Flitch as String = intW.Tostring

CORRECTION: My bad. Int16 has limit of < 32767 and > -32768
not Int32 as I indicated.


In any case, the error says that
e.Item.Cells(1).Text
can not be converted to Int32




Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top