473,320 Members | 1,940 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,320 software developers and data experts.

update DataSet

Hi,

I have to update Some records in a DataSet, I now use this functions:

Dim NewDataRow As DataRow
'dtsXmldFormSettings = the DataSet
NewDataRow = dtsXmldFormSettings.Tables("elmFormSettings").NewR ow
'waarden schrijven
NewDataRow("FormName") = Me.Name
NewDataRow("FormKey") = pryFolder
NewDataRow("Left") = Me.Left
NewDataRow("Top") = Me.Top
NewDataRow("Height") = Me.Height
NewDataRow("Width") = Me.Width
'add to DataSet
dtsXmldFormSettings.Tables("elmFormSettings").Rows .Add(NewDataRow)

What my code does is just adding every time a new record, even if it is
already in it.

Does anybody knows how to update the DataSet instead of Adding a new record?
My goal is: if there isn't already a record in it with the same FormName and
FormKey, it must be added in the DataSet. If there is already a record in
it, it have to update the existing record.

Thanks in advance,

Pieter
Nov 20 '05 #1
3 1156
Hi,

You can construct a DataView instance with the appropriate value of the
RowFilter property. If the DataView is not empty, use it's indexer to access
a DataRowView instance corresponding to the row. Now, use the DataRowView
instance's indexer to update desired columns, something like that:

Dim theView As New DataView(....)
theView.RowFilter = "FormName='...' AND FormKey='...'"

If theView.Count > 0 Then
Dim rowView As DataRowView = theView(0)
rowView("Left") = ...
rowView("Top") = ...
End If

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"DraguVaso" <pi**********@hotmail.com> wrote in message
news:3f***********************@reader0.news.skynet .be...
Hi,

I have to update Some records in a DataSet, I now use this functions:

Dim NewDataRow As DataRow
'dtsXmldFormSettings = the DataSet
NewDataRow = dtsXmldFormSettings.Tables("elmFormSettings").NewR ow
'waarden schrijven
NewDataRow("FormName") = Me.Name
NewDataRow("FormKey") = pryFolder
NewDataRow("Left") = Me.Left
NewDataRow("Top") = Me.Top
NewDataRow("Height") = Me.Height
NewDataRow("Width") = Me.Width
'add to DataSet
dtsXmldFormSettings.Tables("elmFormSettings").Rows .Add(NewDataRow)

What my code does is just adding every time a new record, even if it is
already in it.

Does anybody knows how to update the DataSet instead of Adding a new record? My goal is: if there isn't already a record in it with the same FormName and FormKey, it must be added in the DataSet. If there is already a record in
it, it have to update the existing record.

Thanks in advance,

Pieter


Nov 20 '05 #2
Cor
Hi Pieter,

Did it the same way, here it is almost complete as sample
\\\
Try
ds.ReadXml("c:\testxml.xml", XmlReadMode.ReadSchema)
Catch ' this is not nice can better with a fileinfo I see now.
Dim dt As New DataTable("forms")
dt.Columns.Add(New DataColumn("form",
Type.GetType("System.String")))
dt.Columns.Add(New DataColumn("Width",
Type.GetType("System.Int32")))
dt.Columns.Add(New DataColumn("Height",
Type.GetType("System.Int32")))
dt.Columns.Add(New DataColumn("X",
Type.GetType("System.Int32")))
dt.Columns.Add(New DataColumn("Y",
Type.GetType("System.Int32")))
Dim keys(0) As DataColumn
keys(0) = dt.Columns("form")
dt.PrimaryKey = keys
ds.Tables.Add(dt)
End Try
Dim dr As DataRow = ds.Tables(0).Rows.Find("form1")
If dr Is Nothing Then
dr = ds.Tables("forms").NewRow
ds.Tables(0).Rows.Add(dr)
End If
dr("form") = "form1"
dr("Width") = Me.Width
dr("Height") = Me.Height
dr("X") = Me.Location.X
dr("Y") = Me.Location.Y
Dim dr2 As DataRow = ds.Tables(0).Rows.Find("form1")
ds.WriteXml("c:\testxml.xml", XmlWriteMode.WriteSchema)
///
I hope this works for you?

Cor

Dim NewDataRow As DataRow
'dtsXmldFormSettings = the DataSet
NewDataRow = dtsXmldFormSettings.Tables("elmFormSettings").NewR ow
'waarden schrijven
NewDataRow("FormName") = Me.Name
NewDataRow("FormKey") = pryFolder
NewDataRow("Left") = Me.Left
NewDataRow("Top") = Me.Top
NewDataRow("Height") = Me.Height
NewDataRow("Width") = Me.Width
'add to DataSet
dtsXmldFormSettings.Tables("elmFormSettings").Rows .Add(NewDataRow)

Nov 20 '05 #3
Thanks a lot guys! I combined your two posts to make something that worked
perfect form me!

Public Sub SaveFormSettings()
Dim dtvFS As DataView
dtvFS = New DataView(dtsXmldFormSettings.Tables("elmFormSettin gs"))
'look for a settign with these keys
dtvFS.RowFilter = "FormName = '" & Me.Name & "' AND FormKey = '" &
pryFolder & "'"

Dim rowView As DataRowView

If dtvFS.Count > 0 Then
rowView = dtvFS(0)
Else
rowView = dtvFS.AddNew
'add key-values
rowView("FormName") = Me.Name
rowView("FormKey") = pryFolder
End If

rowView("Left") = Me.Left
rowView("Top") = Me.Top
rowView("Height") = Me.Height
rowView("Width") = Me.Width
End Sub

I have to admit: If I hadn't such a poor understanding of the DataSet and
DataView objects I wouldn't have to ask these questions, hehe. I didn't know
before that updating the DataView updates the DataSet, hahaha :-)

See you later alligator,

Pieter
Nov 20 '05 #4

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

Similar topics

3
by: PAUL EDWARDS | last post by:
I have a windows form that is bound to a datatable. In VB6 I could just update the field contents and it would be updated in the database, however if I update the text property of the control from...
2
by: Niyazi | last post by:
Hi, I have not understand the problem. Before all the coding with few application everything worked perfectly. Now I am developing Cheque Writing application and when the cheque is clear the...
6
by: Babu Mannaravalappil | last post by:
Can somebody please help me figure out why the following method exceptions out? Execution at the line marked with ********** hangs for about 15 seconds and then I get an error that says an...
2
by: Mojtaba Faridzad | last post by:
Hi, Please check these lines: DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet, "mytable"); DataRow row; row = dataSet.Tables.Rows; row.BeginEdit(); row = "555";
1
by: Bennett Haselton | last post by:
Suppose I add a new row to a table in a dataset, and then I use an OleDbDataAdapter to add that new row to a SQL Server database using OleDbDataAdapter.Update(), as in the following code: ...
9
by: jaYPee | last post by:
I have search a lot of thread in google newsgroup and read a lot of articles but still i don't know how to update the dataset that has 3 tables. my 3 tables looks like the 3 tables from...
2
by: Richard | last post by:
Hi, I have 1 dataset with 2 tables (Table1 as parent, Table2 as Child), 1 row in both the tables. I am updating it with a transaction. First parent then child. When child update fails, it raise...
7
by: Jean Christophe Avard | last post by:
Hi! I have a dataset that retreive all the item information from the database. I need to be able to edit them, in the dataset and in the database. I have this code, could anyone tell me if I'm...
5
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
8
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.