We help IT Professionals succeed at work.

Help with retrieving data using For Next Loop

vcharles
vcharles used Ask the Experts™
on
Hi,

Can you please help me with the code below. I am trying to retrieve CountryUser_ID wherever CountryUser (Griddata) = CountryUser(xml file). The code below is giing me CountryUser_ID values that don't even exist in my xml file.

CurXYZ = CountryUser
xtable = CountryUserTable
xid = CountryUser_ID
 
            Dim searchFix As String = C1GridView1.Rows(x).Cells(0).Text
            Dim domFix As New XmlDocument()
            domFix.Load(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))
            Dim listFix As XmlNodeList = domFix.SelectNodes("//" & xtable)
            Dim CheckForItemFix = (From el In MyFix.Descendants(xtable).Elements(curXYZ)  
            Select el.Value).ToList
            For x As Integer = 0 To C1GridView1.Rows.Count - 1
                If CheckForItemFix.Contains(C1GridView1.Rows(x).Cells(0).Text) Then
                    For Each node As XmlNode In listFix
                        If node(curXYZ).InnerText.Equals(C1GridView1.Rows(x).Cells(0).Text) Then
                            numa = node(xid).InnerText
                            numa += numa & ","
                        End If
                    Next
                End If
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Shouldn't this line:

For Each node As XmlNode In listFix

be this:

For Each node As XmlNode In CheckForItemFix

Author

Commented:
Hi,
I tried your suggestion but received the following error message:
"Value of type 'String' cannot be converted to 'System.Xml.XmlNode'."      

Do you have any other ideas how to solve this issue?

Thanks,

V.

Commented:
Dim dsPubs As New DataSet()
Dim mycuid as string
mycuid =""
 ' Read in XML from file
 dsPubs.ReadXml("yourxmlfile.xml")
For Each row As GridViewRow In gdData.Rows
foreach datarow dr in dsPubs.Rows
if (row .Cells(0).Text=datarow ("CountryUser")) then
mycuid =mycuid &"," & datarow ("CountryUser_ID ")
end if
Next
Next

* synatx might be wrong this is just an idea...usinfg dotnet features..

Author

Commented:
Hi,

The problem is:

 If node(xid).InnerText.Equals(C1GridView1.Rows(x).Cells(0).Text) Then

never executes because node(xid).InnerText returns CountryUser_ID (i.e. 1) and C1GridView1.Rows(x).Cells(0).Text returns a Text (i.e. BEL), for example

How do i set node(xid).InnerText to equal "CountryUser" instead of "CountryUser_ID"?

For example the If statement should read when BEL equals  BEL than retreive the CountryUer_ID

Victor

Author

Commented:
Hi,

Just saw your message, a code similar to

 if (row .Cells(0).Text=datarow ("CountryUser")) then

Given the problem I explained in  my last post, is there a way to retreive the text value of CountryUser instead of the CountryUser_ID, as you can see the condition to retrieve  CountryUser_ID is based on wheter the text in the Grid and CountryUser are equal.

Thanks,

Victor

Author

Commented:
Hi,

Getting closer to a solution but need help with setting the num string variable. For example when I have a match for IDs 21, 22, 23. My num value is only 22, How do set my num syntax to include all IDs found in a string (i.e. num = 21,22,23)




For x As Integer = 0 To C1GridView1.Rows.Count - 1
                If CheckForItemFix.Contains(C1GridView1.Rows(x).Cells(0).Text) Then
                    For Each node As XmlNode In listFix
                        For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/Root/CountryUserTable[CountryUser='" & C1GridView1.Rows(x).Cells(0).Text & "']")
                            countryUser_IDA = xnLink.SelectSingleNode("CountryUser_ID").InnerText
                            If countryUser_IDA <> "" Then
                                CD = countryUser_IDA
                                Exit For
                            End If
                        Next
                        If node(xid).InnerText = CD Then
                            numa = node(xid).InnerText
                            num = numa & ","
                        End If
                    Next
                End If
                MsgBox(num)  ***** Wrong value 22
            Next

Author

Commented:
Hi again,

Modified the code below, but now the problem is I'm getting an ID (23)  that doesn't exist in my countryuser.xml file, any ideas what is causing this problem?

num = 23,20,21,22 but it should be
num = 20,21,22

 For x As Integer = 0 To C1GridView1.Rows.Count - 1
                If CheckForItemFix.Contains(C1GridView1.Rows(x).Cells(0).Text) Then
                    For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/Root/CountryUserTable[CountryUser='" & C1GridView1.Rows(x).Cells(0).Text & "']")
                        countryUser_IDA = xnLink.SelectSingleNode("CountryUser_ID").InnerText
                        If countryUser_IDA <> "" Then
                            CD = countryUser_IDA
                            num = num & "," & "" & CD & ""
                        End If
                    Next
                End If
            Next

Author

Commented:
Below is the latest code, but still the same problem.

 For x As Integer = 0 To C1GridView1.Rows.Count - 1
                For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/Root/CountryUserTable[CountryUser='" & C1GridView1.Rows(x).Cells(0).Text & "']")
                    countryUser_IDA = xnLink.SelectSingleNode("CountryUser_ID").InnerText
                    If countryUser_IDA <> "" Then
                        CD = countryUser_IDA
                        num = num & "," & "" & CD & ""
                    End If
                Next
            Next

Author

Commented:
Problem Solved!  Thanks.

Author

Commented:
Hi again,  

How do I get rid of the last "," in my string? When I combine both string, with the code below:
 For x As Integer = 0 To C1GridView1.Rows.Count - 1
                If CheckForItemFix.Contains(C1GridView1.Rows(x).Cells(0).Text) Then
                    For Each xnLink As Xml.XmlNode In xdReceiver.SelectNodes("/Root/CountryUserTable[CountryUser='" & C1GridView1.Rows(x).Cells(0).Text & "']")
                        countryUser_IDA = xnLink.SelectSingleNode("CountryUser_ID").InnerText
                        If countryUser_IDA <> "" Then
                            CD = countryUser_IDA
                            If u > 0 Then
                                num1 = num1 & "," & "" & CD & ""
                            Else
                                num1 = "" & CD & ","
                            End If
                            u = u + 1
                        End If
                    Next
                End If
            Next

            For x As Integer = 0 To C1GridView1.Rows.Count - 1
                If Not CheckForItemFix.Contains(C1GridView1.Rows(x).Cells(0).Text) Then
                    Dim xNew As XElement = New XElement(xtable)
                    xNew.Add(New XElement(xid, num))
                    xNew.Add(New XElement(curXYZ, C1GridView1.Rows(x).Cells(0).Text))
                    MyFix.Root.Add(xNew)
                    MyFix.Save(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))
                    Select Case curXYZ
                        Case "CountryUser"
                            searchIdCountryUser = num
                    End Select
                    num2 += num & ","
                    num = num + 1
                End If
            Next
            numa = num1 & num2


I receive the following results for numa: "20, 21,22," However I don't need the "'" after 22.

Thanks,

V.

Author

Commented:
I am also runing into situation where my string looks like (double commas)

20,,21,22,

Is there a way to make sure I don't receive multiple "," or remove them from the string?

Victor

Commented:
1) REplace this with

num2 += num & ","


if num =null then
   num2 = num2
else
   num2 += num & ","
end if

2) to remove last comma

num2 =Substring(num2 ,0,len(num2)-1)

Author

Commented:
Thanks, I received "Substring not declared" error message.

num2 =Substring(num2 ,0,len(num2)-1)

How do I fix this error?
Commented:
num2 =num2.Substring(0,len(num2)-1)