473,387 Members | 1,882 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.

ADO.NET

I am using OleDbDataAdapter in order to fill a data table and update it.
The fill method is working fine but the update does not work, and I dont
know why.
Here is my code:

Dim data_table As DataTable
Dim conn As OleDbConnection
Dim data_adapter As OleDbDataAdapter
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=
test_db.mdb")
conn.Open()
data_adapter = New OleDbDataAdapter("select * from test_table", conn)
data_table = New DataTable
data_adapter.Fill(data_table)
Me.DataGridView1.DataSource = data_table

'do some changes on the datagridview

Dim temp_val As String = data_table.Rows(row_index)(col_index)
data_adapter.UpdateCommand = New OleDbCommand("update test_table set minimum
= '" & temp_val & "' where growth_day = 1", conn)
data_adapter.Update(data_table)
conn.Close()

minimum and growth_day are fields in my database wich growth_day is the key.
I think the code is ok but the database stays the same and does not accept
the changes. Can someone please help me and tell why does the update is not
working?
I am using VS2005.
Jun 11 '07 #1
9 1340
Hi Gad,

I think the issue is that you are trying to update the data_table like
it is a real table. In ADO.Net it is similar to a real table but it
has different objects that you need to work with. The easiest way I
find to think about it is to break the problem up into parts like
this:

'set up the needed variables

Dim DS as NEW DataSet
Dim strConn as String
Dim strSQL as String
Dim Conn as OLEDBConnection
Dim DA as DataAdapter
Dim CMD as NEW OleDbCommand

'set up the strings

strSQL = "Select * from test_Table"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= test_db.mdb"

'set up the connection object

Conn = new OleDBConnection(strConn)

'set up the command object

CMD = NEW OleDbCommand (strSQL,Conn)

'set up the DataAdapter, set the Select command, and map the incoming
data to
'a datatable and name it

DA = NEW OleDbDataAdapter
DA.TableMappings.Add("Table", "NameOfTable")
DA.SelectCommand = CMD
DA.Fill(DS)
'next is optional and will retrieve schema data for the datatable
'DA.FillSchema(DS,schematype.source,"NameOfTable")

'Now you have a dataset, filled datatable, and a data adapter, BUT you
dont
'have the necessary updating logic to put data back to the underlying
table in
'the database yet. Since this is a single table query it is probably
easiest to use
'a command builder object to do the work for you.

DIM cb as NEW OleDb.OleDbCommandBuilder(DA)

'your updating logic is not set for update, insert, and delete in the
data adapter

'now you need to modify a row in the datatable to alter any values you
want...
'ADD a row:

dim ROW as DataRow = ds.tables("NameOfTable").NewRow
ROW("ColumnName") = "Give it a value"
.....

'MODIFY an existing row:
dim ROW as DataRow
ROW = ds.tables("NameOfTable").rows.find("Primary Key Value")
IF ROW is nothing then
'what you want to do in case of failure
ELSE
ROW("ColumnName") = "New Value Goes Here"
.....
END IF

'DELETE a row
dim ROW as DataRow
ROW = ds.Tables("NameOfTable").rows.find("Primary Key Value")
ds.tables("NameOfTable").remove(ROW)

'OR

ds.Tables("NameOfTable").RemoveAt(rowIndexNumber) ' this is an integer
value

'now you can update your changes to the table in the database

da.update("NameOfTable")

I hope that this points you in the right direction. There is a
significant change with the ADO.Net tools when compared to the classic
ADO tools. The big one is that you must supply your own updating logic
(or use a command builder), and that all data is worked with
'offline'. The sample code above should get you on the right path. I
would also recommend a book Microsoft ADO.NET by Dave Sceppa
(Microsoft Press). I have found this to be the most useful thing on
the topic I have ever read. Lots of examples and nice clear
explanations.

Cheers

The Frog

Jun 11 '07 #2
Why do I need the command builder?
Jun 11 '07 #3
The update row still not working, only add and delete.

Jun 11 '07 #4
On 11 juin, 10:44, gad <g...@discussions.microsoft.comwrote:
The update row still not working, only add and delete.
The update is working, but it's lacking something
you must make 3 case :
1)when an added item
2)when a deleted item
3)when modified
you must detect those 3 case and then do the necessary to update
Now I don't remember exactly the code, I'll send it to you later

Jun 11 '07 #5
Hi Gad,

Can you post your new modified code that includes the changes listed
above?
Are you trying to run this inside a transaction? Do you have other
users accessing the data at the same time (I am looking for
concurrency issues here)? Could you also please do the following:
- add a line of code after the commandbuilder object is created as
follows:
console.writeline(cb.getupdatecommand.commandtext)

and post that as well. I would like to see the updating logic that the
command builder has generated, perhaps it needs a little tweaking...

Cheers

The Frog

Jun 11 '07 #6
Hi

My application is very small and no other user is trying to access the data.
Here is the code i wrote:

Dim conn As OleDbConnection
Dim DA As New OleDbDataAdapter
Dim CMD As OleDbCommand
Dim DS As New DataSet

conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=test_db.mdb")
conn.Open()
CMD = New OleDbCommand("select * from test_table", conn)
DA.TableMappings.Add("test_table", "test_table")
DA.SelectCommand = CMD
DA.Fill(DS)
Me.DataGridView1.DataSource = DS.Tables(0)

Dim cb As New OleDb.OleDbCommandBuilder(DA)
MessageBox.Show(cb.GetUpdateCommand.CommandText)
Dim ROW As DataRow
ROW = DS.Tables(0).Rows(row_index)
ROW(col_index) = 15
DA.Update(DS.Tables(0))

Thank you very much for your help.
Jun 11 '07 #7
Hi Gad,

I think I found the problem. The issue is that the tablemapping is
trying to map from "test_table" to "test_table". When a data adpater
fetches data from an underlying table it automatically calls that
table by the generic name "table". Could you try to change the
tablemappings line to this:

DA.TableMappings.Add("table", "test_table")

That should give you the correct mappings.

Next, I would establish the command builder object before binding the
data grid view. So in the end you code would look like this...

Dim conn As OleDbConnection
Dim DA As New OleDbDataAdapter
Dim CMD As OleDbCommand
Dim DS As New DataSet
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=test_db.mdb")
CMD = New OleDbCommand("select * from test_table", conn)
DA.TableMappings.Add("test_table", "test_table")
DA.SelectCommand = CMD
DA.Fill(DS)

Dim cb As New OleDb.OleDbCommandBuilder(DA)

Me.DataGridView1.DataSource = DS.Tables(0)

Now I would make the modification code for the changes to the
DataRows.

I take it you are wanting to use the edits that are done in the data
grid view as the changes that you place into the database. If this is
so then what you need to do is, if I am not mistaken (MVP please feel
free to jump in here) simply call the update command at the
appropriate moment from either events occuring in the cells in the
data grid or from a button on the form.

The idea is that when the change is entered by the user, your code
captures the change when the user is finished, and then can call the
da.update("Test_Table") method from that change event.

I think that this should get you back on track. If you need help with
the data grid then you will find plenty of examples and problems in
this newsgroup. Simply search on DataGrid +Update and you will get
heaps of information. I think with these minor alterations your data
adapter is okay. By the way you dont need to "open" the connection
object, the data adapter will do that for you and close it again too!

Cheers

The Frog

Jun 11 '07 #8
On 11 juin, 12:38, The Frog <Mr.Frog.to....@googlemail.comwrote:
Hi Gad,

I think I found the problem. The issue is that the tablemapping is
trying to map from "test_table" to "test_table". When a data adpater
fetches data from an underlying table it automatically calls that
table by the generic name "table". Could you try to change the
tablemappings line to this:

DA.TableMappings.Add("table", "test_table")

That should give you the correct mappings.

Next, I would establish the command builder object before binding the
data grid view. So in the end you code would look like this...

Dim conn As OleDbConnection
Dim DA As New OleDbDataAdapter
Dim CMD As OleDbCommand
Dim DS As New DataSet

conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=test_db.mdb")
CMD = New OleDbCommand("select * from test_table", conn)
DA.TableMappings.Add("test_table", "test_table")
DA.SelectCommand = CMD
DA.Fill(DS)

Dim cb As New OleDb.OleDbCommandBuilder(DA)

Me.DataGridView1.DataSource = DS.Tables(0)

Now I would make the modification code for the changes to the
DataRows.

I take it you are wanting to use the edits that are done in the data
grid view as the changes that you place into the database. If this is
so then what you need to do is, if I am not mistaken (MVP please feel
free to jump in here) simply call the update command at the
appropriate moment from either events occuring in the cells in the
data grid or from a button on the form.

The idea is that when the change is entered by the user, your code
captures the change when the user is finished, and then can call the
da.update("Test_Table") method from that change event.

I think that this should get you back on track. If you need help with
the data grid then you will find plenty of examples and problems in
this newsgroup. Simply search on DataGrid +Update and you will get
heaps of information. I think with these minor alterations your data
adapter is okay. By the way you dont need to "open" the connection
object, the data adapter will do that for you and close it again too!

Cheers

The Frog
That's won't run it need to create a select command

Jun 11 '07 #9
Hi Omar,

The select statement is located in the CMD object that is appended to
the DataAdapter object (DA). The update, insert, and delete commands
are (in this case) built by the commandbuilder object and are appended
to the DataAdapter object as part of the commandbuilder objects
constructor.

This code works fine.

Cheers

The Frog

Jun 11 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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.