Solved

How update xml file based on values selected from a data grid?

Posted on 2010-11-09
5
607 Views
Last Modified: 2012-05-10
Subject:  How update xml file based on values selected from a data grid?

Hello,

I’m using the code in below (Section A) to update data in my “link.xml” using a combo box with my grid control, the problem is I can’t figure out how to save the data modified to the xml file, below (Section B) is the code I use to save new data to the xml file, can you please help me modify the code in section B to only save the rows modified?

Thanks,

Victor

Section A.

dtLink = New DataTable("Link")
        dtLink.Columns.Add("Link_ID")
        dtLink.Columns.Add("WDeveloper_ID")
        dtLink.Columns.Add("WUser_ID")
        dtLink.Columns.Add("DInventory_ID")


        '2
        Dim linker As XElement = XElement.Load(Application.StartupPath + "\Link.xml")
        '3
        For Each item As XElement In linker.Elements("Row")
            Dim linkID As String = item.Element("Link_ID").Value
            Dim CtryDeveloperID As String = item.Element("WDeveloper_ID").Value
            Dim CtryUserID As String = item.Element("WUser_ID").Value
            Dim DInventoryID As String = item.Element("DInventory_ID").Value
 
            Dim drLink As DataRow = dtLink.NewRow()
            drLink("Link_ID") = linkID
            drLink("WDeveloper_ID") = CtryDeveloperID
            drLink("WUser_ID") = CtryUserID
            drLink("DInventory_ID") = DInventoryID
            dtLink.Rows.Add(drLink)
        Next

        Dim bs As New BindingSource()
        bs.DataSource = dtLink
        C1TrueDBGrid1.DataSource = bs
        C1TrueDBGrid1.AlternatingRows = True
        C1TrueDBGrid1.Caption = "Inventory Charts"
        Me.C1TrueDBGrid1.Splits(0).DisplayColumns(0).Width = Me.C1TrueDBGrid1.Splits(0).DisplayColumns(0).Visible = False

        Dim vReceiverDev As C1.Win.C1TrueDBGrid.ValueItemCollection
        Dim vReceiverUsr As C1.Win.C1TrueDBGrid.ValueItemCollection
        vReceiverDev = Me.C1TrueDBGrid1.Columns("WDeveloper_ID").ValueItems.Values
        vReceiverUsr = Me.C1TrueDBGrid1.Columns("WUser_ID").ValueItems.Values

        Dim WCtryDeveloper As XElement = XElement.Load(Application.StartupPath + "\Receiver.xml")

        For Each item As XElement In WCtryDeveloper.Elements("Row")
            Dim receiverID As String = item.Element("Receiver_ID").Value
            Dim receiverName As String = item.Element("Receiver").Value

            vReceiverDev.Add(New C1.Win.C1TrueDBGrid.ValueItem(receiverID, receiverName))
            vReceiverUsr.Add(New C1.Win.C1TrueDBGrid.ValueItem(receiverID, receiverName))
        Next

        Me.C1TrueDBGrid1.Columns("WDeveloper_ID").ValueItems.Translate = True
        Me.C1TrueDBGrid1.Columns("WDeveloper_ID").ValueItems.Presentation = C1.Win.C1TrueDBGrid.PresentationEnum.ComboBox
        Me.C1TrueDBGrid1.Columns("WUser_ID").ValueItems.Translate = True
        Me.C1TrueDBGrid1.Columns("WUser_ID").ValueItems.Presentation = C1.Win.C1TrueDBGrid.PresentationEnum.ComboBox

        Dim vInventory As C1.Win.C1TrueDBGrid.ValueItemCollection
        vInventory = Me.C1TrueDBGrid1.Columns("DInventory_ID").ValueItems.Values

        Dim DInventory As XElement = XElement.Load(Application.StartupPath + "\Inventory.xml")

        For Each item As XElement In DInventory.Elements("Row")
            Dim inventoryID As String = item.Element("Inventory_ID").Value
            Dim inventoryName As String = item.Element("Inventory").Value

            vInventory.Add(New C1.Win.C1TrueDBGrid.ValueItem(inventoryID, inventoryName))

        Next

        Me.C1TrueDBGrid1.Columns("DInventory_ID").ValueItems.Translate = True
        Me.C1TrueDBGrid1.Columns("DInventory_ID").ValueItems.Presentation = C1.Win.C1TrueDBGrid.PresentationEnum.ComboBox





Section B.


Try
            Dim ds As New DataSet
            ds.ReadXml(Application.StartupPath + "\Link.xml")
            Dim dt As DataTable = ds.Tables(0)
            Dim r As DataRow = dt.NewRow
            r!Link_ID = dt.Rows(dt.Rows.Count - 1)("Link_ID") + 1
            r!WDeveloper_ID = C1TrueDBGrid42.Columns(0).Value
            r!WUser_ID = C1TrueDBGrid43.Columns(0).Value
            r!DInventory_ID = C1TrueDBGrid44.Columns(0).Value
            dt.Rows.Add(r)
            ds.WriteXml(Application.StartupPath + "\Link.xml")

        Catch ex As Exception
            MsgBox("Items selected were not saved", MsgBoxStyle.OkOnly, "Confirmation")
            Exit Sub
        End Try

0
Comment
Question by:vcharles
  • 4
5 Comments
 

Author Comment

by:vcharles
ID: 34096968
Hello again,

I tried the code below, but receiving the following error on the last "Next" Any ideas on how to fix this error?

Error: Collection was modified; enumeration operation might not execute.

Code:
Dim linker As XElement = XElement.Load(Application.StartupPath + "\Link.xml")
For Each dr As DataRow In dtLink.Rows
Dim row As String
row = String.Format("{0} - {1} - {2} - {3}", dr(0), dr(1), dr(2), dr(3))
'MessageBox.Show(row)
For Each item As XElement In linker.Elements("Row")
Dim drLink As DataRow = dtLink.NewRow()
drLink("Link_ID") = dr(0)
drLink("WDeveloper_ID") = dr(1)
drLink("WUser_ID") = dr(2)
drLink("DInventory_ID") = dr(3)
dtLink.Rows.Add(drLink)
Next
Next 'ERROR Line

Thanks,

Victor

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34100233
The error you get in above row is because you are looping through rows and adding items to the rows collection so the loop bounds get changed.
0
 

Accepted Solution

by:
vcharles earned 0 total points
ID: 34101541
I found the solution:
Dim linker As XElement = XElement.Load(Application.StartupPath + "\Link.xml")
        linker.Elements("Row").Remove()

        For Each dr As DataRow In dtLink.Rows
            Dim xdr As New XElement("Row")
            xdr.Add(New XElement("Link_ID", dr(0)))
            xdr.Add(New XElement("WDeveloper_ID", dr(1)))
            xdr.Add(New XElement("WUser_ID", dr(2)))
            xdr.Add(New XElement("DInventory_ID", dr(3)))
            linker.Add(xdr)
        Next

        linker.Save(Application.StartupPath + "\Link.xml")
0
 

Author Comment

by:vcharles
ID: 34101569
Problem solved.
0
 

Author Closing Comment

by:vcharles
ID: 34134501
Thank You
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question