By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,830 Members | 1,664 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,830 IT Pros & Developers. It's quick & easy.

Saving using datagrid in VB.NET from SQL Server by SAVE button

P: 41
Hi all!
I am attempting to bring an SQL Server table into ASP.NET using the datagrid . I have set all cells to be a text box by which the user can just click on and edit the cell. I have a save button and when I click the save button the page reloads and the new data is in the cell, however when I close out and come back in the data is no longer there. I have a databind() subroutine which includes a postback included within my code for the save button 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 mySqlDataAdapter As SqlDataAdapter
Dim dstCopy As New DataSet
Dim strQuery As String

Dim intError As Integer = 0

myConnection = New SqlConnection( _
"server=SQLSERVER;database=upsizedCandidate;Integr ated Security=True;")
Dim dgItem As DataGridItem
For Each dgItem In myInfo.Items
'Dim txtID As TextBox = _
' CType(dgItem.Cells(0).FindControl("txtID"), TextBox)
Dim txtCAGE As TextBox = _
CType(dgItem.Cells(1).FindControl("txtCAGE"), TextBox)
Dim txtName As TextBox = _
CType(dgItem.Cells(2).FindControl("txtName"), TextBox)
Dim txtStreet As TextBox = _
CType(dgItem.Cells(3).FindControl("txtStreet"), TextBox)
Dim txtCity As TextBox = _
CType(dgItem.Cells(4).FindControl("txtCity"), TextBox)
Dim txtState As TextBox = _
CType(dgItem.Cells(5).FindControl("txtState"), TextBox)
Dim txtNation As TextBox = _
CType(dgItem.Cells(6).FindControl("txtNation"), TextBox)
Dim txtPostal As TextBox = _
CType(dgItem.Cells(7).FindControl("txtPostal"), TextBox)
Dim txtPhone As TextBox = _
CType(dgItem.Cells(8).FindControl("txtPhone"), TextBox)

Dim item As Integer
If txtCAGE.Text.Trim <> String.Empty Then
If IsNumeric(dgItem.Cells(9).Text) Then
item = CInt(dgItem.Cells(9).Text)
strQuery = "Update Table1 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 + _
"' WHERE ID=" + CStr(item)
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)
strQuery = String.Empty
End If

End Sub

Here is my page_load and databind:
Sub Page_Load(ByVal sender As Object, ByVal E As EventArgs)
If Not Page.IsPostBack Then
End If
End Sub

Public Sub BindData()
Dim myDataSet As New DataSet
Dim mySqlDataAdapter As SqlDataAdapter
mySqlDataAdapter = New SqlDataAdapter( _
"server=SQLSERVER;database=upsizedCandidate;Integr ated Security=True")
mySqlDataAdapter.Fill(myDataSet, "XH")
myInfo.DataSource = myDataSet.Tables("XH")
End Sub

My datagrid is:
<asp:DataGrid id="myInfo"
<asp:TemplateColumn HeaderText="Save">
<asp:Button CommandName="Save" Text="Save" ID="btnSave" runat="server" />

Anyone have any thoughts or suggestions, it would be GREATLY appreciated. Thanks in advance!!
Apr 18 '07 #1
Share this Question
Share on Google+
2 Replies

P: 41
My first problem was that I didn't properly call my doSave function. I just put a new button inside my page but outside the datagrid (above the datagrid code) and when I attempt to save it directs to the function but now I get a crazy error message that looks like:

Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Has anyone else got this error before. If anyone could help me out your help will be greatly appreciated. Thanks for helping
Apr 18 '07 #2

P: 41
Ok, I placed a different button inside the datagrid to access the save method and now I'm getting a much simpler error. I just wanted to post it so nobody wastes any time trying to figure out the last error I posted. The error I'm getting now is:
Input string was not in a correct format.
I've tested it numerous times and this error is displayed when I click on the save button and it's coming straight out of my method. Any thoughts or suggestions would help me very much.
Apr 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.