Solved

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

Posted on 2010-11-09
5
610 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 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