Solved

Help wityh using multyiple values in code using VB.NET

Posted on 2014-11-11
12
122 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

707 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