Saving all cells in Datagrid from SQL Server
I'm new to ASP.NET and I am getting an error when I try to save a Datagrid. I am bringing in a table from SQL Server and placing it inside the datagrid which I've turned all the cells into text boxes to display the data and to edit anything at any time. The data is displayed fine and I can edit the contents but when I click on my save button to save the entire datagrid I get the following error:
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.
The error is coming from my save function which is as follows:
Private Sub doSave(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim dstCopy As New DataSet
Dim strQuery As String
myConnection = New SqlConnection("server=SQLSERVER;database=upsizedCa ndidate;Integrated Security=True;")
myConnection.Open()
Dim dgItem As DataGridItem
For Each dgItem In myInfo.Items
Dim txtCAGE As TextBox = _
CType(dgItem.Cells(0).FindControl("txtCAGE"), TextBox)
Dim txtName As TextBox = _
CType(dgItem.Cells(1).FindControl("txtName"), TextBox)
Dim txtStreet As TextBox = _
CType(dgItem.Cells(2).FindControl("txtStreet"), TextBox)
Dim txtCity As TextBox = _
CType(dgItem.Cells(3).FindControl("txtCity"), TextBox)
Dim txtState As TextBox = _
CType(dgItem.Cells(4).FindControl("txtState"), TextBox)
Dim txtNation As TextBox = _
CType(dgItem.Cells(5).FindControl("txtNation"), TextBox)
Dim txtPostal As TextBox = _
CType(dgItem.Cells(6).FindControl("txtPostal"), TextBox)
Dim txtPhone As TextBox = _
CType(dgItem.Cells(7).FindControl("txtPhone"), TextBox)
Dim item As Integer
If txtCAGE.Text.Trim <> String.Empty Then
If IsNumeric(dgItem.Cells(8).Text) Then
item = CInt(dgItem.Cells(8).Text)
strQuery = "Update XH SET CAGE=" + txtCAGE.Text _
+ ", Name='" + txtName.Text + _
+", Street='" + txtStreet.Text + _
+", City='" + txtCity.Text + _
+", State='" + txtState.Text + _
+", Nation='" + txtNation.Text + _
+", Postal='" + txtPostal.Text + _
+", Phone='" + txtPhone.Text
Else
item = 0
strQuery = "INSERT INTO XH (CAGE, Name, Street, City, State, Nation, Postal, Phone) VALUES('" _
+ txtCAGE.Text + ", " + _
+txtName.Text + ", " + _
+txtStreet.Text + ", " + _
+txtCity.Text + ", " + _
+txtState.Text + ", " + _
+txtNation.Text + ", " + _
+txtPostal.Text + ", " + _
+txtPhone.Text + ")"
End If
myCommand = New SqlCommand(strQuery, myConnection)
myCommand.ExecuteNonQuery()
End If
Next
myConnection.Close()
BindData()
End Sub
I've tried alot of testing and nothing seems to work. I think it's something small but I can't get it. Please help! Thanks in advance.
|