473,406 Members | 2,847 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,406 software developers and data experts.

reflecting changes of DataTable to Database

Hi All,

Ive spent days breaking my head over why the code below doesnt update changes to the database. Could someone please help...


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.             OleDbDataAdapter adp = new OleDbDataAdapter("select * from TestTable", connection );
  4.             adp.InsertCommand = new OleDbCommandBuilder(adp).GetInsertCommand();
  5.  
  6.             DataTable dt_dst = new DataTable("TestTable");
  7.             adp.Fill(dt_dst);
  8.  
  9.         DataRow rw = dt_dst.NewRow();
  10.         rw["ID"] =123;
  11.         rw["CAT_ID"] = "adadas";
  12.  
  13.         dt_dst.Rows.Add(rw);
  14.  
  15.  
  16.     dt_dst.AcceptChanges();
  17.     adp.Update(dt_dst);
  18.  
  19.  
Dec 7 '06 #1
3 4120
bplacker
121 100+
Hi All,

Ive spent days breaking my head over why the code below doesnt update changes to the database. Could someone please help...


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.             OleDbDataAdapter adp = new OleDbDataAdapter("select * from TestTable", connection );
  4.             adp.InsertCommand = new OleDbCommandBuilder(adp).GetInsertCommand();
  5.  
  6.             DataTable dt_dst = new DataTable("TestTable");
  7.             adp.Fill(dt_dst);
  8.  
  9.         DataRow rw = dt_dst.NewRow();
  10.         rw["ID"] =123;
  11.         rw["CAT_ID"] = "adadas";
  12.  
  13.         dt_dst.Rows.Add(rw);
  14.  
  15.  
  16.     dt_dst.AcceptChanges();
  17.     adp.Update(dt_dst);
  18.  
  19.  
I've never done it this way... the way I've done it before involves writing the insert command out.
so once you have your insert command... just do:

apd.InsertCommand = new SQLCommand(insertSQL)
adp.InsertCommand.Connection = connection
adp.InsertCommand.ExecuteNonQuery()
Dec 7 '06 #2
bplacker
121 100+
You might also try something like this:

Dim addedFields as new DataTable
Dim builder as new OleDBCommandBuilder
builder.DataAdapter = myTableAdapter

newFields = myDataTable.GetChanges(Data.DataRowState.Added)

builder.refreshSchema()
myTableAdapter.UpdateCommand = builder.getUpdateCommand()
'or insert command, I'm not sure.

myTableAdapter.Update(newFields)

myDataTable.AcceptChanges()

please let me know if this works, that might be somewhat useful
Dec 8 '06 #3
bhar
37
You might also try something like this:

Dim addedFields as new DataTable
Dim builder as new OleDBCommandBuilder
builder.DataAdapter = myTableAdapter

newFields = myDataTable.GetChanges(Data.DataRowState.Added)

builder.refreshSchema()
myTableAdapter.UpdateCommand = builder.getUpdateCommand()
'or insert command, I'm not sure.

myTableAdapter.Update(newFields)

myDataTable.AcceptChanges()

please let me know if this works, that might be somewhat useful

Hi,

Steps:

Instantiate the DataAdapter
Set the DataAdapter command properties
The DataAdapter class has four properties:

SelectCommand
InsertCommand
UpdateCommand
DeleteCommand

Each of the property perform a specific operation on database.

InsertCommand:
This read-write property returns or sets the SQL statement that will be used whjen rows are inserted into the data source.

DataAdapter needs to retrieve or send data to and from the database, it uses Command objects which must be specified.

This is done by creating command objects, and then assigning them to teh appropriate DataAdapter property:

Ex:

MyAdapter.InsertCommand=comInsert

Updation happens with Update() method of DataAdapter.

Try this book: "Database programming using Vb.net and sql server", [Link Removed]
Regards
Madhv
Dec 11 '06 #4

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

Similar topics

0
by: San | last post by:
Hello, I have DataSet which has a table named bills . I'm filling this bills table from another class named Class2 which also has the bills table with same schema. After copying the data from...
4
by: Glenn Owens | last post by:
I have a DataGrid web control which I've dynamically populated with template columns to be used for bulk-editting. Generally, all of the columns are textbox and/or dropdownlist child controls. ...
2
by: Geir Sanne | last post by:
hi!! how am i supposed to update the db witht he changes the user make in webpage am trying to make a simple page that have a grid bound to a table the user can delete,add or update the grid...
1
by: Luis Esteban Valencia | last post by:
Hello Everyone, Iam an intermediate ASP.Net programmer and iam facing a challenging task. I have a table in MS-SQL server database called 'Members'. The table has following fields... ...
1
by: TM | last post by:
I have a problem with the datagrid where I wanted to detect any changes to the grid, such as a record being changed, added or deleted. So I used the currentcellchanged method and used the code...
1
by: Steve Amey | last post by:
Hi all I have a function that gets information from a database and merges the DataTable into a DataSet that some controls are bound to. What I would like is for the information to be merged in...
14
by: Ron L | last post by:
All I am working with a DataGrid and a form consisting of a number of text, checkbox, combobox controls, all bound to the same datatable. When I click on my "New" button, I create a new row,...
2
by: Joe | last post by:
Hi I am building a datatable manually and putting in a grid I would like to edit a row then determine that the table has changes and needs to be saved The problem I am having is that all...
30
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data, from a database. One user changes the data and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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
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...
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
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...

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.