473,385 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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;")
myConnection.Open()
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)
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()
strQuery = String.Empty
myCommand.Dispose()
End If
Next

myConnection.Close()
BindData()
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
BindData()
End If
End Sub


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

My datagrid is:
<asp:DataGrid id="myInfo"
OnItemCommand="doInsert"
OnEditCommand="doEdit"
OnCancelCommand="doCancel"
OnUpdateCommand="doUpdate"
OnSaveCommand="doSave"
<asp:TemplateColumn HeaderText="Save">
<FooterTemplate>
<asp:Button CommandName="Save" Text="Save" ID="btnSave" runat="server" />
</FooterTemplate>
</asp:TemplateColumn>


Anyone have any thoughts or suggestions, it would be GREATLY appreciated. Thanks in advance!!
Apr 18 '07 #1
2 3854
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
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

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

Similar topics

3
by: Vikram | last post by:
Hi, Given below is my task. An user can open the xls file from my website (loaded on the top frame). After filling the Excel, he can click a send button at the bottom frame. By clicking the...
4
by: Sangeetha. | last post by:
I have a datagrid with just one row. When I edit the contents of the datagrid and click on Save button (somewhere else in the form), the changes are lost. However, if I click TAB from the editable...
2
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid...
3
by: Clay Black | last post by:
I need to find a way to save an HTML page to the IIS server. What I need is to have a button that once it is clicked the current page being displayed is saved to a location on the local IIS...
7
by: Joe | last post by:
Hi, I’m new to asp.net. I want to create an asp.net page that allows user to edit the data. I have pasted my code below. I am able to display the data in a datagrid. At the bottom of the page...
0
by: js | last post by:
I have a DataGrid with a command button on the first column of the DataGrid. The DataGrid is databound to a dataset with DataKeyFiled bound to "ControlNumber" field in the dataset. I am trying to...
11
by: Kevin | last post by:
I've been searching forever for examples of saving data changes in a DataGridView. There's all kinds of examples, but none really show how to save changes. Someone please help me. I have a...
1
by: RSH | last post by:
Hi, Im using the code below to render a datagrid as an Excel file. It seems to work fine when the user clicks on the Save button. But when they click on the open button, it renders correctly...
3
by: psyvanz | last post by:
How to save all the information in a datagrid in just one command button, click, and it will automatically save in the table in access(student name,and student subjects will added and save in table...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.