Solved

Help wityh using multyiple values in code using VB.NET

Posted on 2014-11-11
12
114 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:vcharles
Comment Utility
Hi,

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

Victor
0
 
LVL 20

Expert Comment

by:ElrondCT
Comment Utility
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
Comment Utility
The countries are  in a string, would like to apply the code for each  country instead of writing the same for each country.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 20

Expert Comment

by:ElrondCT
Comment Utility
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
Comment Utility
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
Comment Utility
If File.Exists(myfile) ...
0
 

Author Comment

by:vcharles
Comment Utility
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
Comment Utility
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
Comment Utility
Thank You.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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:…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now