Solved

Help wityh using multyiple values in code using VB.NET

Posted on 2014-11-11
12
121 Views
Last Modified: 2015-01-23
Hi,

I'm using the code below to delete records in other xml files based on record deleted from a datagrid, how I modify the code to apply to multiple countries, for example if I include countries in s = ('BEL', 'FRA', NLD, USA) , How do I loop through the variable and replace country in the code if the file exist (i.e.LinkItemUSA.xml) ?

   For i = Me.C1TrueDBGrid4.SelectedRows.Count - 1 To 0 Step -1
                    Dim DVa As New DataView(dtb, "Child_Item_ID like " & "'" & C1TrueDBGrid4.Columns(0).Value & "'" & " or Parent_Item_ID like " & "'" & C1TrueDBGrid4.Columns(0).Value & "'" & "", Nothing, DataViewRowState.CurrentRows)
                    For X = DVa.Count - 1 To 0 Step -1
                        DVa.Item(X).Delete()
                    Next
                    Dim DV1 As New DataView(dtb, "", Nothing, DataViewRowState.CurrentRows)
                    Dim FilteredDT As DataTable
                    FilteredDT = DV1.ToTable
                    FilteredDT.TableName = "Row"
                    FilteredDT.WriteXml(Application.StartupPath + "\Data\LinkFiles\LinkItemUSA.xml")

                    Dim DVa1 As New DataView(dtc, "Item_ID like " & "'" & C1TrueDBGrid4.Columns(0).Value & "'" & "", Nothing, DataViewRowState.CurrentRows)
                    For X = DVa1.Count - 1 To 0 Step -1
                        DVa1.Item(X).Delete()
                    Next
                    Dim DV11 As New DataView(dtc, "", Nothing, DataViewRowState.CurrentRows)
                    Dim FilteredDT1 As DataTable
                    FilteredDT1 = DV11.ToTable
                    FilteredDT1.TableName = "Row"
                    FilteredDT1.WriteXml(Application.StartupPath + "\Data\LinkFiles\LinkPPFUSA.xml")

Open in new window

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
  • 6
  • 6
12 Comments
 
LVL 20

Expert Comment

by:ElrondCT
ID: 40437646
How do you know which country you want to write to? Is it coming from one of the columns of C1TrueDBGrid4? You'd probably want to change the filename in your .WriteXML statements to something like:
             FilteredDT.WriteXml(Application.StartupPath + "\Data\LinkFiles\LinkItem" _
                 & C1TrueDBGrid4.Columns(0).Value & ".xml")

Open in new window

0
 

Author Comment

by:vcharles
ID: 40447419
Hi,

I'm sorry for the late reply.

I need to delete data for all countries listed in s = ('BEL', 'FRA', NLD, USA).


Victor
0
 
LVL 20

Expert Comment

by:ElrondCT
ID: 40448763
Is "s" defined somewhere outside this block of code? Is that intended to be a string, or a set? That is, is this stored like:

Dim s as String = ('BEL', 'FRA', NLD, USA)

or

Dim s(3) as String = {"BEL", "FRA", "NLD", "USA"}

so that S(0) = "BEL", S(1) = "FRA", etc.

Why do some of the country codes have single quotes around them, while others don't?

Do you have files like LinkItemUSA.xml, LinkItemBEL.xml, etc.?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vcharles
ID: 40458650
Hi,

My mistake,  all the countries have single quotes between them.

Victor
0
 
LVL 20

Expert Comment

by:ElrondCT
ID: 40458875
I still don't understand exactly how they're coming. You're giving me one-sentence comments which don't fully describe what's happening. What exactly does "s = ('BEL', 'FRA', NLD, USA)" mean? Is that a string? Is it an array of strings? Is it a field in a table?
0
 

Author Comment

by:vcharles
ID: 40459081
The countries are  in a string, would like to apply the code for each  country instead of writing the same for each country.
0
 
LVL 20

Expert Comment

by:ElrondCT
ID: 40463347
It'll probably be easiest to break the string into an array, then loop through the array of country codes:
        Dim s As String = "'BEL', 'FRA', NLD, USA"
        Dim charSeparators() As Char = {","c}
        Dim CountrySet As String() = s.Split(charSeparators)
        For j As Integer = 0 To CountrySet.GetUpperBound(0)
            ' Remove quotes if around code
            If Mid(CountrySet(j), 1, 1) = "'" Then
                CountrySet(j) = Mid(CountrySet(j), 2, Len(CountrySet(j)) - 2)
            End If
            For i = Me.C1TrueDBGrid4.SelectedRows.Count - 1 To 0 Step -1
                Dim DVa As New DataView(dtb, "Child_Item_ID like " & "'" & C1TrueDBGrid4.Columns(0).Value & "'" & " or Parent_Item_ID like " & "'" & C1TrueDBGrid4.Columns(0).Value & "'" & "", Nothing, DataViewRowState.CurrentRows)
                For X = DVa.Count - 1 To 0 Step -1
                    DVa.Item(X).Delete()
                Next
                Dim DV1 As New DataView(dtb, "", Nothing, DataViewRowState.CurrentRows)
                Dim FilteredDT As DataTable
                FilteredDT = DV1.ToTable
                FilteredDT.TableName = "Row"
                FilteredDT.WriteXml(Application.StartupPath + "\Data\LinkFiles\LinkItem" & CountrySet(j) & ".xml")

                Dim DVa1 As New DataView(dtc, "Item_ID like " & "'" & C1TrueDBGrid4.Columns(0).Value & "'" & "", Nothing, DataViewRowState.CurrentRows)
                For X = DVa1.Count - 1 To 0 Step -1
                    DVa1.Item(X).Delete()
                Next
                Dim DV11 As New DataView(dtc, "", Nothing, DataViewRowState.CurrentRows)
                Dim FilteredDT1 As DataTable
                FilteredDT1 = DV11.ToTable
                FilteredDT1.TableName = "Row"
                FilteredDT1.WriteXml(Application.StartupPath + "\Data\LinkFiles\LinkPPF" & CountrySet(j) & ".xml")
            Next i
        Next j

Open in new window

This goes through the same set of selected rows from the C1TrueDBGrid4 table for each country. You may need to change the selection of rows for each country, or something that. The place to change the row selection would be right before the Next j at the end of the routine.
0
 

Author Comment

by:vcharles
ID: 40469699
Hi,

How do you check if the file for a country exist before executing the code below?

Thanks,

Victor
0
 
LVL 20

Expert Comment

by:ElrondCT
ID: 40470736
If File.Exists(myfile) ...
0
 

Author Comment

by:vcharles
ID: 40470810
Hi,

Do you mean the approach below? Don't currently have access to my project.

Dim myfile as string = Application.StartupPath + "\Data\LinkFiles\LinkPPF" & CountrySet(j) & ".xml")
  Dim s As String = "'BEL', 'FRA', NLD, USA"
        Dim charSeparators() As Char = {","c}
        Dim CountrySet As String() = s.Split(charSeparators)
        For j As Integer = 0 To CountrySet.GetUpperBound(0)
If File.Exists(myfile) then
            ' Remove quotes if around code
            If Mid(CountrySet(j), 1, 1) = "'" Then
                CountrySet(j) = Mid(CountrySet(j), 2, Len(CountrySet(j)) - 2)
            End If
            For i = Me.C1TrueDBGrid4.SelectedRows.Count - 1 To 0 Step -1
                Dim DVa As New DataView(dtb, "Child_Item_ID like " & "'" & C1TrueDBGrid4.Columns(0).Value & "'" & " or Parent_Item_ID like " & "'" & C1TrueDBGrid4.Columns(0).Value & "'" & "", Nothing, DataViewRowState.CurrentRows)
                For X = DVa.Count - 1 To 0 Step -1
                    DVa.Item(X).Delete()
                Next
                Dim DV1 As New DataView(dtb, "", Nothing, DataViewRowState.CurrentRows)
                Dim FilteredDT As DataTable
                FilteredDT = DV1.ToTable
                FilteredDT.TableName = "Row"
                FilteredDT.WriteXml(Application.StartupPath + "\Data\LinkFiles\LinkItem" & CountrySet(j) & ".xml")

                Dim DVa1 As New DataView(dtc, "Item_ID like " & "'" & C1TrueDBGrid4.Columns(0).Value & "'" & "", Nothing, DataViewRowState.CurrentRows)
                For X = DVa1.Count - 1 To 0 Step -1
                    DVa1.Item(X).Delete()
                Next
                Dim DV11 As New DataView(dtc, "", Nothing, DataViewRowState.CurrentRows)
                Dim FilteredDT1 As DataTable
                FilteredDT1 = DV11.ToTable
                FilteredDT1.TableName = "Row"
                FilteredDT1.WriteXml(Application.StartupPath + "\Data\LinkFiles\LinkPPF" & CountrySet(j) & ".xml")
End If

Thanks,

Victor
0
 
LVL 20

Accepted Solution

by:
ElrondCT earned 500 total points
ID: 40470920
Setting myfile to the file name needs to happen inside the For J loop (you could put it right before If File.Exists). Otherwise, yes, that's what I had in mind. You can use myfile again when you're actually writing the XML file as well. Do you need to check for the existence of both files, LinkItemxxx.xml and LinkPPFxxx,xml?
0
 

Author Closing Comment

by:vcharles
ID: 40566997
Thank You.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

737 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