473,549 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Save changes to database using datagrid

37 New Member
Hello i m using vb 2005 express to do my project. I m suppose to create a datagrid to allow user to make changes to the database. The program display the database in a datagrid where users can juz make changes there. I encountered a problem when user are trying save changes. The following code is executed when the save button is click:


Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
  2.  
  3.         Dim SQLCon As New SqlClient.SqlConnection("Data Source=BICU111AA\SQLEXPRESS;Initial Catalog=ProteinDatabase;Integrated Security=True")
  4.         SQLCon.Open()
  5.         Dim SQLCom As New SqlClient.SqlCommand("SELECT * FROM Table_1", SQLCon)
  6.         Dim DataAdapter As New SqlClient.SqlDataAdapter(SQLCom)
  7.         Dim CommandBuilder As New SqlClient.SqlCommandBuilder(DataAdapter)
  8.         Dim changes As Integer
  9.         changes = DataAdapter.Update(dt)
  10.         DataAdapter.Dispose()
  11.         If changes > 0 Then
  12.             MsgBox(changes & " changed rows were made.")
  13.         Else
  14.             MsgBox("No changes made")
  15.         End If
  16.     End Sub

It reports an error when it reaches "changes = DataAdapter.Upd ate(dt)". Can someone advise me how to debug it?
Nov 16 '07 #1
11 4125
debasisdas
8,127 Recognized Expert Expert
Can you please post the exact error message for reference of our experts.
Nov 16 '07 #2
kunal pawar
297 Contributor
DataAdapter.upd ate() this method expect the Data set as Agumant not DataTable. Plz do tht it may solve your Problem
Nov 16 '07 #3
Frinavale
9,735 Recognized Expert Moderator Expert
Hello i m using vb 2005 express to do my project. I m suppose to create a datagrid to allow user to make changes to the database. The program display the database in a datagrid where users can juz make changes there. I encountered a problem when user are trying save changes. The following code is executed when the save button is click:


Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
  2.  
  3.         Dim SQLCon As New SqlClient.SqlConnection("Data Source=BICU111AA\SQLEXPRESS;Initial Catalog=ProteinDatabase;Integrated Security=True")
  4.         SQLCon.Open()
  5.         Dim SQLCom As New SqlClient.SqlCommand("SELECT * FROM Table_1", SQLCon)
  6.         Dim DataAdapter As New SqlClient.SqlDataAdapter(SQLCom)
  7.         Dim CommandBuilder As New SqlClient.SqlCommandBuilder(DataAdapter)
  8.         Dim changes As Integer
  9.         changes = DataAdapter.Update(dt)
  10.         DataAdapter.Dispose()
  11.         If changes > 0 Then
  12.             MsgBox(changes & " changed rows were made.")
  13.         Else
  14.             MsgBox("No changes made")
  15.         End If
  16.     End Sub

It reports an error when it reaches "changes = DataAdapter.Upd ate(dt)". Can someone advise me how to debug it?
First of all...what is your variable dt?
I don't see it declared anywhere in this code.
Secondly, are you actually grabbing any data from your DataGrid while updating?

Thirdly, could you please post the exact error that you are getting so that we can help you solve this problem.

Thanks,

-Frinny
Nov 16 '07 #4
gyap88
37 New Member
The error message is Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information and it points to the line "changes=DataAd apter.Update(dt )"

anyway, the variable, dt is a datatable that i declared in the beginning of the program.
Nov 18 '07 #5
gyap88
37 New Member
The error message is Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information and it points to the line "changes=DataAd apter.Update(dt )"

The variable, dt is a datatable that i declared in the beginning of the program.

Lastly, i need to the program to return the number of rows that has changed after the database is saved...
Hope u can help me out thx
Nov 18 '07 #6
fenix71
1 New Member
Hello, you may do two things to solve:
1)Create the update method yourself, look "Update method data adapter" on google and you'll have plenty.
2)Retrieve the key of the table if any, if you don't have a key you have to create the update method.

C U
Davide
Nov 18 '07 #7
gyap88
37 New Member
hello, i have the code to the following:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2.         Dim changes As Integer
  3.         Dim SQLCon As New SqlClient.SqlConnection("Data Source=TP-TM3282WXMI\SQLEXPRESS;Initial Catalog=ProteinDatabase;Integrated Security=True;Pooling=False")
  4.         SQLCon.Open()
  5.          Dim DataAdapter As New SqlClient.SqlDataAdapter()
  6.                DataAdapter.SelectCommand = New SqlClient.SqlCommand("SELECT * FROM Table_1", SQLCon)
  7.         Dim CommandBuilder As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(DataAdapter)
  8.         DataAdapter.Fill(dt)
  9.         'CommandBuilder.GetUpdateCommand()
  10.         changes = DataAdapter.Update(dt)
  11.  
  12.         DataAdapter.Dispose()
  13.         If changes > 0 Then
  14.             MsgBox(changes & " changed rows were made.")
  15.         Else
  16.             MsgBox("No changes made")
  17.         End If
  18.     End Sub
  19.  



But it still displays the same error message
Nov 18 '07 #8
Frinavale
9,735 Recognized Expert Moderator Expert
Sorry, but there's no mention here....are you developing a desktop application or a web application?

If you are using a web application and create the DataTable at the beginning of the program, then this table could possibly be lost during PostBack during the next page request. This could explain the error, as you would be trying to update an empty table.
Nov 18 '07 #9
gyap88
37 New Member
I m developing a desktop application
Nov 19 '07 #10

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

Similar topics

2
1712
by: Angie | last post by:
datagrid ex. a 1 checkbox b 2 checkbox c 3 checkbox once a user selects these 3 rows from a datagrid and i want to save the 1, 2, 3 value into the database how do i do this? I know how to determine the checkboxes and save data into
4
560
by: HomeyDaClown | last post by:
I'm new to the VB.net world and have been reading alot of books and threw newsgroups Is it even posable to save user input from a form to a data grid. I could just edit the data gri but would prefer useing the user form to import the data Any help would be greatly appreciated Thanks Homey
3
3817
by: Islamegy | last post by:
When i press btn_save all changed rows in my grid get saved except the last editing row... I want my btn_Save to end editing in the datagrid.. so if user didn't press "enter" after editing last cell of the current row, I could force this row to be saved in the datasource. help plz
2
4810
by: Alpha | last post by:
Hi, I have a window based program. One of the form has several textboxes and a datagrid. The textboxes are bind to the same dataset table as the datagrid and the text changes to reflect different row selected in the datagrid. I want to save the changes that user make in the textboxes when they select a different row in the datagrid. I...
0
1278
by: tafs7 | last post by:
Hello, I have a custom datagrid control that works great. But I was thinking about adding some functionality to it. Currently, I use the datagrid to display one record at a time, and with item templates, if you click the edit button, the datagrid becomes a nice form to make changes. Kinda like a big rolodex, and as you page you flip thru...
0
3618
by: Patty05 | last post by:
I have a datagrid on a form that update properly when form loads. When the program runs and I type in/add a new row in the datagrid, it does not save the changes. Any help would be greatly appreaciated. Thanks. Code below: Imports System.Data.OleDb Public Class Form2 Inherits System.Windows.Forms.Form #Region " Windows Form...
4
1987
by: nch1978 | last post by:
I am new to programming and am sorry if this post is in the wrong area. I have a listbox bound to a table and a datagrid bound to another one with a connection through xml. I can change the data within the grid, with the following through a button on a different tab page within the form, but it does not save changes to the database, just shows...
2
3870
by: toddw607 | last post by:
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...
1
1951
by: jagjitsurfer | last post by:
hi, I am using datagrid view in my solution which displays data from a table of database. I allows users to directly update, insert or delete data from data grid view, but i can't save the data of the current row of datagridview which user is currently editing. If user changes the selection from editing row to another row, the data can be...
0
7527
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7726
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. ...
0
7819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6052
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5377
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5097
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...
0
3505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
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...

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.