updating a multitable dataset

aydinozdemir

Member
Joined
May 15, 2011
Messages
15
Programming Experience
Beginner
hello. i have got a form with 2 buttons and a tabcontrol.
button1 adds tabpages and datagridviews as the number of data tables inside of my database file, then fills the grids. works good

button2 saves the dataset back to database. this doesnt work.i tried

da.Update(ds2, "name_surname")
da.Update(ds2.Tables(0))... etc and still no success. where i am doing wrong? can you help me please?


Imports System.Data.OleDb

PublicClass Form4
Dim con AsNew OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Ders\lessons.mdb")
Dim cmd As OleDbCommand
Dim cb As OleDbCommandBuilder
Dim da As OleDbDataAdapter
Dim ds AsNew DataSet
Dim ds2 AsNew DataSet
Dim sql AsString
Dim dt As DataTable
Dim DGV AsNew DataGridView
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TabControl1.Controls.Clear()
Try
con.Open()
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, NewObject() {Nothing, Nothing, Nothing, Nothing})
Dim int AsInteger
Dim i AsInteger
i = -1
For int = 0 To dt.Rows.Count - 1
If dt.Rows(int)!TABLE_TYPE.ToString = "TABLE"Then

Dim teacher AsString
teacher = dt.Rows(int)!TABLE_NAME
If teacher <> "student"Then

Try
i = i + 1
sql = "Select * from " & teacher & ""
da = New OleDbDataAdapter(sql, con)

da.Fill(ds, "" & teacher & "")
ds2.Merge(ds)
ds.Clear()

Dim NTP AsNew TabPage
Dim LineOfText AsString
Dim i2 AsInteger
Dim aryTextFile() AsString
LineOfText = teacher
aryTextFile = LineOfText.Split("_")
For i2 = 0 To UBound(aryTextFile)
LineOfText = String.Join(" ", aryTextFile)
Next i2
TabControl1.Controls.Add(NTP)
NTP.Text = (LineOfText)
DGV = New DataGridView
NTP.Controls.Add(DGV)
DGV.Name = i
DGV.DataSource = ds2.Tables(i)
'DGV.DataMember = "" & teacher & ""
DGV.Size = New System.Drawing.Size(927, 594)

con.Close()

Catch ex As Exception
MsgBox(ex.ToString)
EndTry
EndIf
EndIf
Next
i = i + 1
MsgBox(i.ToString + " Listed.")
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Data Load Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
EndTry
con.Close()
EndSub

PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Try
cb = New OleDbCommandBuilder(da)
da.Update(ds2)
ds2.AcceptChanges()
Catch ex As Exception
MsgBox(ex.ToString)
EndTry
EndSub
EndClass
 
Last edited:
i want to update my database from ds2. ds2 has got multiple tables. and each table in ds2 is being shown in seperate datagridviews inside seperate tabpages.after i make changes in one or more datagridviews, i want to reflect changes to my database file.

this error catches when i click the button2.

System.InvalidOperationException: TableMapping['Table'] or Data Table 'Table' couldnt found by Update
System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
System.Data.Common.DbDataAdapter.Update(DataSet dataSet)
 
When you get data you should do this:
myDataAdapter.Fill(myDataSet, "MyTableName")
and when you save data you should do this:
myDataAdapter.Update(myDataSet, "MyTableName")
Be explicit about the name of the DataTable in each case. Note that the table name is the name of the DataTable in the DataSet's Tables collection. It generally should be if reasonably possible, but it doesn't have to be the same as the database table.
 
i am using this code inside a loop to collect dataset1 inside dataset2. so i can easily manuplate whole dataset2 with no need of variable "" & teacher & "".am i correct :confused:

da.Fill(ds, "" & teacher & "")
ds2.Merge(ds)
ds.Clear()


i wonder to know what dataset2 looks like each time i merge. is there multiple datatables which have same name as taken from dataset1? or one datatable with the last name taken from dataset1?
i did as you suggested but only the last datagridview is being updated with no error. if i try to update the initial grids i get errors.
 
okay problem solved. i ve placed the dataadapter update command inside a similar loop as datagridfill. it works now fine. thanks for the info about tablenames.saved lots of time for me.
 
Back
Top