473,241 Members | 1,571 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,241 software developers and data experts.

Save changes to database using datagrid

37
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.Update(dt)". Can someone advise me how to debug it?
Nov 16 '07 #1
11 4096
debasisdas
8,127 Expert 4TB
Can you please post the exact error message for reference of our experts.
Nov 16 '07 #2
kunal pawar
297 100+
DataAdapter.update() 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 Expert Mod 8TB
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.Update(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
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=DataAdapter.Update(dt)"

anyway, the variable, dt is a datatable that i declared in the beginning of the program.
Nov 18 '07 #5
gyap88
37
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=DataAdapter.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
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
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 Expert Mod 8TB
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
I m developing a desktop application
Nov 19 '07 #10
mzmishra
390 Expert 256MB
DataAdapter.update() method excepts a Dataset as parameter

Dim da As DataAdapter
Dim dataSet As DataSet
Dim returnValue As Integer

returnValue = da.Update(dataSet)
Nov 19 '07 #11
gyap88
37
How do i convert my datatable to a dataset?
Nov 19 '07 #12

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

Similar topics

2
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...
4
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...
3
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...
2
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...
0
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...
0
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...
4
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...
2
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...
1
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...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.