473,789 Members | 3,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

41 New Member
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.EventArg s)
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim mySqlDataAdapte r As SqlDataAdapter
Dim dstCopy As New DataSet
Dim strQuery As String

Dim intError As Integer = 0

myConnection = New SqlConnection( _
"server=SQLSERV ER;database=ups izedCandidate;I ntegrated Security=True;" )
myConnection.Op en()
Dim dgItem As DataGridItem
For Each dgItem In myInfo.Items
'Dim txtID As TextBox = _
' CType(dgItem.Ce lls(0).FindCont rol("txtID"), TextBox)
Dim txtCAGE As TextBox = _
CType(dgItem.Ce lls(1).FindCont rol("txtCAGE"), TextBox)
Dim txtName As TextBox = _
CType(dgItem.Ce lls(2).FindCont rol("txtName"), TextBox)
Dim txtStreet As TextBox = _
CType(dgItem.Ce lls(3).FindCont rol("txtStreet" ), TextBox)
Dim txtCity As TextBox = _
CType(dgItem.Ce lls(4).FindCont rol("txtCity"), TextBox)
Dim txtState As TextBox = _
CType(dgItem.Ce lls(5).FindCont rol("txtState") , TextBox)
Dim txtNation As TextBox = _
CType(dgItem.Ce lls(6).FindCont rol("txtNation" ), TextBox)
Dim txtPostal As TextBox = _
CType(dgItem.Ce lls(7).FindCont rol("txtPostal" ), TextBox)
Dim txtPhone As TextBox = _
CType(dgItem.Ce lls(8).FindCont rol("txtPhone") , TextBox)

Dim item As Integer
If txtCAGE.Text.Tr im <> String.Empty Then
If IsNumeric(dgIte m.Cells(9).Text ) Then
item = CInt(dgItem.Cel ls(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,St reet,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(strQ uery, myConnection)
myCommand.Execu teNonQuery()
strQuery = String.Empty
myCommand.Dispo se()
End If
Next

myConnection.Cl ose()
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 mySqlDataAdapte r As SqlDataAdapter
mySqlDataAdapte r = New SqlDataAdapter( _
"SELECT * FROM XH", _
"server=SQLSERV ER;database=ups izedCandidate;I ntegrated Security=True")
mySqlDataAdapte r.Fill(myDataSe t, "XH")
myInfo.DataSour ce = myDataSet.Table s("XH")
myInfo.DataBind ()
End Sub

My datagrid is:
<asp:DataGrid id="myInfo"
OnItemCommand=" doInsert"
OnEditCommand=" doEdit"
OnCancelCommand ="doCancel"
OnUpdateCommand ="doUpdate"
OnSaveCommand=" doSave"
<asp:TemplateCo lumn HeaderText="Sav e">
<FooterTemplate >
<asp:Button CommandName="Sa ve" Text="Save" ID="btnSave" runat="server" />
</FooterTemplate>
</asp:TemplateCol umn>


Anyone have any thoughts or suggestions, it would be GREATLY appreciated. Thanks in advance!!
Apr 18 '07 #1
2 3887
toddw607
41 New Member
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 enableEventVali dation="true"/> in configuration or <%@ Page EnableEventVali dation="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 ClientScriptMan ager.RegisterFo rEventValidatio n 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
toddw607
41 New Member
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
12377
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 button i have to save the file in a shared directory on the web server by a unique name. Is it possible ?
4
2411
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 column of the datagrid, the changes are saved. Is there a way to save the changes without clicking TAB button ?
2
3199
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 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/creatingcustomcolumns.asp The problem I am having is that the data in the custom datagridcolumn is not saved to viewstate and after postback, the column does not contain data.
3
1729
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 server. I know there are ways to go and get a page, but this one would be dynamic and wouldn't be saved so I would need to save it while it is still active or have it forwarded to an asp.net page that will take the html code and save it to a file. TIA...
7
1788
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 there are two buttons “Save Changes” and “Cancel”. When “Save Changes” button is clicked saveRec function is called. But for some reason inside saveRec function the value of the textbox is not visible. The line Label2.Text =...
0
1428
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 save the row's ControlNumber to a page level variable when the corresponding item command is clicked . However, no matter what I do, the page property is alway blank. I also tried use CheckBox in the ItemTemplate. The CheckBox's...
11
27322
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 Windows Forms program with a DataGridView and a BindingSource added to a form. Here is the code I'm using to populate the grid: Dim CRClassesTableAdapter As SqlDataAdapter Dim CRClassesTable As New DataTable()
1
1334
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 but when the user attempts to save the document from the browser...it saves it as an HTML file. Is there any way to disable the Open button and force the user to save the file? Or some other trick to make sure the file saves as a type xls? ...
3
5186
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 ), regardless on how many records were bind in a datagrid. im using dataenvironment in connecting my database MS Access 2007, hope you help me on this matter (please just a dataenvironment code) cause modules is too complicated for me.. im...
0
9665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.