|
-
Apr 27th, 2003, 10:18 AM
#1
Thread Starter
Hyperactive Member
Database Update...
Hello, I can't update my database. Why??? I found code snippets in thius forum, mine is the same. Here's the code:
VB Code:
Dim myConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" _
& "c:\mypath\db.mdb" ' Put here the connection string
Dim myConnection As New OleDbConnection(myConnString) 'Create a new connection
Dim MyDataSet As New DataSet()
Dim MyDataAdapter As New OleDbDataAdapter("SELECT * from MyTable", myConnection)
MyDataAdapter.Fill(MyDataSet, "MyTable")
MyDataSet.Tables(0).Rows(0).Item(2) = 123412 'modify 3nd column of first row
MyDataAdapter.Update(MyDataSet) 'Here's the error
The error is: "Update was not able to find TableMapping['Table'] or DataTable 'Table'".
Thnx
Xmas
Learn, this is the Keyword...
-
Apr 27th, 2003, 11:15 AM
#2
Sleep mode
How many tables do you have in the database ?
-
Apr 27th, 2003, 11:20 AM
#3
Sleep mode
Try this
VB Code:
'instead of the one you wrote
MyDataAdapter.Update(MyDataSet,"MyTable")
-
Apr 27th, 2003, 11:50 AM
#4
Thread Starter
Hyperactive Member
Got another error:
"Update needs a valid UpdateCommand if DataRow collection is passed with modified rows"
What's that?
Thx
Learn, this is the Keyword...
-
Apr 27th, 2003, 11:52 AM
#5
Sleep mode
-
Apr 27th, 2003, 12:09 PM
#6
Thread Starter
Hyperactive Member
In the same place:
MyDataAdapter.Update(MyDataSet,"MyTable")
I have 10 tables in my DB, but I tried with a new DB with only 1 table. Nothing to do 
Pirate, any ideas?
Tx
Learn, this is the Keyword...
-
Apr 27th, 2003, 12:11 PM
#7
Sleep mode
Ok , are you updating existing data or adding new data ?
-
Apr 27th, 2003, 12:27 PM
#8
Thread Starter
Hyperactive Member
I'm updating, not adding. I tried without modifying the content of the table, and everything works. But when I change something in the dataset I get this error. I didn't try adding a new row yet.
Learn, this is the Keyword...
-
Apr 27th, 2003, 12:45 PM
#9
Frenzied Member
Do you have a valid update command for your dataadapter?
It should look something like this:
VB Code:
Me.myDataAdapter.UpdateCommand = Me.OleDbUpdateCommand1
Me.OleDbUpdateCommand1.Connection = myConnection
Me.OleDbUpdateCommand1.CommandText = "UPDATE myTable SET myField= ? where (myField = ?)"
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("myField", System.Data.OleDb.OleDbType.VarWChar, 50, "myField"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_myField", System.Data.OleDb.OleDbType.VarWChar, 50, _
System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "myField", System.Data.DataRowVersion.Original, Nothing))
Edits: Some Spelling Errors
Last edited by Lunatic3; Apr 27th, 2003 at 12:50 PM.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Apr 27th, 2003, 12:46 PM
#10
Sleep mode
I sounds you are missing Update sql command . I've not tried your method to update existing data but I only use that when I add new data .
-
Apr 27th, 2003, 12:47 PM
#11
Sleep mode
Lunatic3 was a bit faster...
-
Apr 27th, 2003, 12:51 PM
#12
Frenzied Member
In case you need to insert data to table you also need a valid Insert command.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Apr 27th, 2003, 03:04 PM
#13
Frenzied Member
You can let VS.NET build an update command for you. Just declare a commandbuilder object and pass it to your dataadapter.
Dont gain the world and lose your soul
-
Apr 27th, 2003, 03:41 PM
#14
Thread Starter
Hyperactive Member
Wow! Looking at Lunatic3's code it seems to me very complicated, but with commandbuilder, DevGrp's trick, it works great!!!
Thank you all, guys!
Xmas
Learn, this is the Keyword...
-
Apr 27th, 2003, 03:55 PM
#15
Frenzied Member
The command builder just automates that process for you and the idea is basically the same.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Apr 27th, 2003, 03:56 PM
#16
Thread Starter
Hyperactive Member
Sure, but i'm not so strong with SQL
Learn, this is the Keyword...
-
Apr 27th, 2003, 05:48 PM
#17
New Member
Hi,
I also can't complete the database update routine.
I wanted to bind a dataset to a datagrid. Then to modify data in the datagrid, and on "Save" click to update a table.
'To populate the grid:
Dim da As New OleDbDataAdapter("Select * From Table1", cnn)
Dim ds As New DataSet()
da.Fill(ds, "Table1")
da.Dispose()
cnn.Close()
dGrid.SetDataBinding(ds, "")
'On Save click:
Dim cnn As New OleDbConnection(ConnString)
cnn.Open()
Dim da As New OleDbDataAdapter("Select * From Table1", cnn)
Dim cb As New OleDbCommandBuilder(da)
da.Update(ds.Tables("Table1"))
This code allows me to delete rows from the table, and to edit a specific columns, but I can't add new records. I'm getting "Error in the INSERT Statement"
Any ideas why?
Thanks
-
Apr 28th, 2003, 02:54 AM
#18
Thread Starter
Hyperactive Member
Originally posted by Lunatic3
In case you need to insert data to table you also need a valid Insert command.
I think this is the reason. Try to add
VB Code:
cb.getinsertcommand() ' I think this is the method, now I can't check it...
Another question: I noticed that I can select only ONE table at time. In fact, if I select 2 or more tables, i.e. "SELECT * FROM table1,table2", it returns me only "</newdataset>". Any ideas ?
Learn, this is the Keyword...
-
Apr 28th, 2003, 05:06 AM
#19
Frenzied Member
it returns me only "</newdataset>".
What you mean by that?
You can select from multiple tables in one dataadapter, however they will be mapped to only one table in the dataset.
Edits:
cb.getinsertcommand() ' I think this is the method, now I can't check it...
By the way your method you mentioned just returns the insert statment(does not produce one, its produced already) so he can check what may be wrong with it. More detail of error may be helpful.
Last edited by Lunatic3; Apr 28th, 2003 at 05:32 AM.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Apr 28th, 2003, 06:20 AM
#20
Thread Starter
Hyperactive Member
VB Code:
Dim myConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" _
& "c:\mypath\db.mdb" ' Put here the connection string
Dim myConnection As New OleDbConnection(myConnString) 'Create a new connection
Dim MyDataSet As New DataSet()
Dim MyDataAdapter As New OleDbDataAdapter("SELECT * from MyTable1,MyTable2", myConnection) 'Select from 2 tables
MyDataAdapter.Fill(MyDataSet)
MsgBox(MyDataSet.GetXml()) ' MsgBox's message is only "</newdataset>"
If I remove MyTable2 (or MyTable1) I get the real content of my database.
In my code I used this:
VB Code:
Mydataadapter.insert.textcommand=cb.getinsertcommand()
and it worked.
Learn, this is the Keyword...
-
Apr 28th, 2003, 07:29 AM
#21
Frenzied Member
Look, you can select from two table, but as far as you dont define any logical relationship (Inner join, outer join...) between them then you will have a table that will contain ALL of the fields of both tables and ... (give it a try ..)
So if you want to have two separate tables you have to define tow seprate dataaadapters, but still you can map that adapaters to one Dataset and your dataset will have two tables.
Mydataadapter.insert.textcommand=cb.getinsertcommand()
and it worked.
It would have worked without that too. All you have to do is defining a commandbuilder for a dataadapter. It does the job automatically.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Apr 28th, 2003, 11:14 AM
#22
Thread Starter
Hyperactive Member
Thanks, I'll do in that way, but I can't still understand why it returns me a blank dataset instead of a dataset with one big table with all the columns.
Xmas.
Learn, this is the Keyword...
-
Apr 28th, 2003, 11:56 AM
#23
Frenzied Member
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|