update DataSet

  • Thread starter Thread starter DraguVaso
  • Start date Start date
D

DraguVaso

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").NewRow
'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
 
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
 
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
 
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("elmFormSettings"))
'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
 
Back
Top