Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on 

Help wityh using multyiple values in code using VB.NET

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

.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Victor Charles
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

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

Avatar of Victor  Charles
Victor Charles
Flag of United States of America image

ASKER

Hi,

I'm sorry for the late reply.

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


Victor
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

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.?
Avatar of Victor  Charles
Victor Charles
Flag of United States of America image

ASKER

Hi,

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

Victor
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

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?
Avatar of Victor  Charles
Victor Charles
Flag of United States of America image

ASKER

The countries are  in a string, would like to apply the code for each  country instead of writing the same for each country.
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

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.
Avatar of Victor  Charles
Victor Charles
Flag of United States of America image

ASKER

Hi,

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

Thanks,

Victor
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

If File.Exists(myfile) ...
Avatar of Victor  Charles
Victor Charles
Flag of United States of America image

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Victor  Charles

ASKER

Thank You.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo