Solved

Help wityh using multyiple values in code using VB.NET

Posted on 2014-11-11
12
120 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 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