Solved

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

Posted on 2010-11-09
5
602 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now