473,387 Members | 2,436 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,387 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 4104
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: 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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.