Input string was not in a correct format.

G

Guest

Hi can someone please tell me why I keep getting the following error when I execute this code!

I have checked the database and it seems like everything is set as integer for pageID!

Error: Input string was not in a correct format.

<CODE>
Sub DGPages_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
'Read in the values of the updated row
Dim pageID As Integer = e.Item.Cells(1).Text 'Error
Dim modificationDate As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim description As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim title As String = CType(e.Item.Cells(4).Controls(0), TextBox).Text

'Construct the Database Connection
Dim Myconn As New SqlConnection(ConfigurationSettings.AppSettings("strConn"))
Dim cmd As New SqlCommand("PageUpdate", Myconn)
cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

' Add Parameters to the SQL query
Dim objID, objModDate, objDescription, objTitle As SqlParameter
objID = cmd.Parameters.Add("@ID", SqlDbType.Int)
objModDate = cmd.Parameters.Add("@ModDate", SqlDbType.DateTime)
objDescription = cmd.Parameters.Add("@Description", SqlDbType.NVarChar)
objTitle = cmd.Parameters.Add("@Title", SqlDbType.NVarChar)


objID.Direction = ParameterDirection.Input
objModDate.Direction = ParameterDirection.Input
objDescription.Direction = ParameterDirection.Input
objTitle.Direction = ParameterDirection.Input

objID.Value = pageID
objModDate.Value = modificationDate
objDescription.Value = description
objTitle.Value = title

Dim myReader As SqlDataReader = cmd.ExecuteReader()
myReader.Read()

myReader.Close()

Myconn.Close()

'Finally, set the EditItemIndex to -1 and rebind the DataGrid
DGPages.EditItemIndex = -1
BindData()
End Sub
</CODE>
 
G

Guest

On your line:

Dim pageID As Integer = e.Item.Cells(1).Text 'Error

You are trying to convert a string to an integer. Instead, the code should be:

Dim pageID As Integer = CInt(e.Item.Cells(1).Text)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top