Help with displaying Text value in DropDownList when moving through records found from search results

Hi,

The  C1CountryOrigin.Text  is returning ID values instead of the text value, when I place a message Box to view if I'm accessing the right text value, The message box returns the correct text valus. Appareently C1CountryOrigin.Text doesn't work.

How do I fix this problem?

dtDonor = Session("dtDonor")
        CurrentIndex = Session("CurrentIndex")
        If CurrentIndex < dtDonor.Rows.Count - 1 Then
            CurrentIndex += 1
            Session("CurrentIndex") = CurrentIndex
            C1CountryOrigin.Text = dtDonor.Rows(currentIndex).Item("CountryOrigin")
              ' MsgBox(dtDonor.Rows(currentIndex).Item("CountryOrigin"))
        End If

The grid was initially loaded in the Page load event with the following code:

 Dim CountryOrigin As New DataSet
        CountryOrigin.ReadXml(Server.MapPath("~/App_Data/CountryOrigin.xml"))
        C1CountryOrigin.DataSource = CountryOrigin.Tables(0)
        C1CountryOrigin.DataMember = "CountryOrigin"
        C1CountryOrigin.DataTextField = "CountryOrigin"
        C1CountryOrigin.DataValueField = "CountryOrigin_ID"
        C1CountryOrigin.DataBind()

Thanks,

Victor
vcharlesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AngryBinaryCommented:
To get the displayed text value, you have to go through the item:

CICountryOrigin.SelectedItem.Text
0
ddayx10Commented:
>>Appareently C1CountryOrigin.Text doesn't work<<

This is true. That doesn't work. The dropdownlist doesn't have text of its own. The items inside have text/values. What are you trying to do exactly?

Change the text of the selected item?
Set the selected (currently displayed) item based on a text value?
Something else?

Based on your issue "title" I'm going to guess you're looking for something like this:

ddl.Items.FindByText(dtDonor.Rows(currentIndex).Item("CountryOrigin").Selected = True
0
vcharlesAuthor Commented:
Hi,

In part A I am populating dtdonor, and in part B (Initial post) I am trying to move through the records in dtDonor to display its text value based on the link ID.

I don't know how to use  the code in the previous post with the DropdownList, because I am not selecting any rows, I'm just moving through the records.

ddl.Items.FindByText(dtDonor.Rows(currentIndex).Item("CountryOrigin").Selected = True

Part A:
       Dim LinkID2 As String = "1"
        ' create a new XML Document object for each XML file
        Dim xdDonor As New Xml.XmlDocument
        Dim xdLink As New Xml.XmlDocument
        Dim xdReceiver As New Xml.XmlDocument

        ' load all of the XML documents
        xdDonor.Load(Server.MapPath("~/App_Data/CountryOrigin.xml"))
        xdLink.Load(Server.MapPath("~/App_Data/LinkSSADB.xml"))
        xdReceiver.Load(Server.MapPath("~/App_Data/CountryUser.xml"))

        ' Extract the ReceiverID and DonorID values from the current node
        Dim ReceiverID As String
        Dim DonorID As String
        Dim Link_ID As String
        Dim DonorID2 As String
        Dim Name As String
        Dim ReceiverID2 As String

        dtReceiver = New DataTable
        dtReceiver.Columns.Add("CountryUser_ID", GetType(String))
        dtReceiver.Columns.Add("CountryUser", GetType(String))

        dtDonor = New DataTable
        dtDonor.Columns.Add("CountryOrigin_ID", GetType(String))
        dtDonor.Columns.Add("CountryOrigin", GetType(String))

        dtLinkID = New DataTable
        dtLinkID.Columns.Add("LinkID", GetType(String))

        Session("CurrentIndex") = 0
        Session("dtDonor") = dtDonor
        Session("dtReceiver") = dtReceiver
        Session("dtLinkID") = dtLinkID

        ' This outer loop will iterate over the Link file where the ReceiverID node is equal to our search criteria
        ' For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/LinkA/[Receiver_ID='" & C1Receiver.SelectedItem.Value & "']")
        For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/LinkA[Receiver_ID='" & C1Receiver.SelectedItem.Value & "']")
            Link_ID = xnLink.SelectSingleNode("LinkAID").InnerText
            ReceiverID = xnLink.SelectSingleNode("CountryUser_ID").InnerText
            DonorID = xnLink.SelectSingleNode("CountryOrigin_ID").InnerText

            ' Perform another similar search based upon the Donor XML file
            For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root/CountryOriginTable[CountryOrigin_ID='" & DonorID & "']")
                DonorID2 = xnDonor.SelectSingleNode("CountryOrigin_ID").InnerText
                Name = xnDonor.SelectSingleNode("CountryOrigin").InnerText
                dtDonor.Rows.Add({DonorID2, Name})
            Next


            ' Perform yet another practically identical search on the Receiver XML file
            For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/Root/CountryUserTable[CountryUser_ID='" & ReceiverID & "']")
                ' Extract the ReceiverID and Name values from the current node
                ReceiverID2 = xnReceiver.SelectSingleNode("CountryUser_ID").InnerText
                Name = xnReceiver.SelectSingleNode("CountryUser").InnerText
                dtReceiver.Rows.Add({ReceiverID2, Name})
            Next
            ' Extract the Link_ID avalues from the current node
            For Each xnLinkA As Xml.XmlNode In xdLink.SelectNodes("/Root/LinkA[LinkAID='" & Link_ID & "']")
                ' Extract the ReceiverID and Name values from the current node
                LinkID2 = xnLinkA.SelectSingleNode("LinkAID").InnerText
                dtLinkID.Rows.Add({LinkID2})
                MsgBox(LinkID2)
            Next
        Next

        C1CountryOrigin.DataSource = dtDonor
        C1CountryOrigin.DataTextField = "CountryOrigin"
        C1CountryOrigin.DataValueField = "CountryOrigin_ID"
        C1CountryOrigin.DataBind()

        C1CountryUser.DataSource = dtReceiver
        C1CountryUser.DataTextField = "CountryUser"
        C1CountryUser.DataValueField = "CountryUser_ID"
        C1CountryUser.DataBind()
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

vcharlesAuthor Commented:
Hello,

I'm now trying to move through the records based on the ID values in the Link file with the code below: But I am getting error message: 'CmbRec' has a SelectedIndex which is invalid because it does not exist in the list of items.Parameter name: value

On line:  CmbRec.SelectedIndex = Val(Subnode1.InnerText) - 1

How do I fix this error? Part B is the code I am using to filter the data.

 Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
        Dim NodeCount As Integer = LinkStream.SelectNodes("Root/Link").Count

        If (NodeNumber < NodeCount) Then
            NodeNumber += 1   'Move to some next node
            Dim node As XmlNode = LinkStream.SelectSingleNode("Root/Link[" & NodeNumber & "]")
            Dim Subnode1 As XmlNode = node.ChildNodes(0)
            Dim Subnode2 As XmlNode = node.ChildNodes(1)
            MsgBox(NodeNumber)
            CmbRec.SelectedIndex = Val(Subnode1.InnerText) - 1
            CmbDonor.SelectedIndex = Val(Subnode2.InnerText) - 1
            btnPrev.Enabled = True
            If (NodeNumber = NodeCount) Then
                btnNext.Enabled = False
            End If
        Else
            NodeNumber = 0   ' Reset count so start searching from zero (You can change strategy)
            ' btnNext.Enabled = False
        End If
    End Sub

Part B:

Dim xdDonor As New Xml.XmlDocument
        Dim xdLink As New Xml.XmlDocument
        Dim xdReceiver As New Xml.XmlDocument

        ' load all of the XML documents
        xdDonor.Load(Server.MapPath("~/App_Data/Donor.xml"))
        xdLink.Load(Server.MapPath("~/App_Data/Link.xml"))
        xdReceiver.Load(Server.MapPath("~/App_Data/Receiver.xml"))
        MsgBox(C1Country.SelectedItem.Value)
        ' This outer loop will iterate over the Link file where the ReceiverID node is equal to our search criteria
        For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/Link[ReceiverID='" & C1Country.SelectedItem.Value & "']")

            ' Extract the ReceiverID and DonorID values from the current node
            Dim ReceiverID As String
            Dim DonorID As String

            ReceiverID = xnLink.SelectSingleNode("ReceiverID").InnerText
            DonorID = xnLink.SelectSingleNode("DonorID").InnerText

            ' Perform another similar search based upon the Donor XML file
            For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root/Donor[DonorID='" & DonorID & "']")

                ' Extract the DonorID and Name values from the current node
                Dim DonorID2 As String
                Dim Name As String

                DonorID2 = xnDonor.SelectSingleNode("DonorID").InnerText
                Name = xnDonor.SelectSingleNode("Name").InnerText
                dtDonor.Rows.Add({DonorID2, Name})
            Next

            ' Perform yet another practically identical search on the Receiver XML file
            For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/Root/Receiver[ReceiverID='" & ReceiverID & "']")
                ' Extract the ReceiverID and Name values from the current node
                Dim ReceiverID2 As String
                Dim Name As String

                ReceiverID2 = xnReceiver.SelectSingleNode("ReceiverID").InnerText
                Name = xnReceiver.SelectSingleNode("Name").InnerText
                dtReceiver.Rows.Add({ReceiverID2, Name})

            Next
        Next
       
        CmbDonor.DataSource = dtDonor
        CmbDonor.DataTextField = "Name"
        CmbDonor.DataValueField = "DonorID"
        CmbDonor.DataBind()

        CmbRec.DataSource = dtReceiver
        CmbRec.DataTextField = "Name"
        CmbRec.DataValueField = "ReceiverID"
        CmbRec.DataBind()
0
AngryBinaryCommented:
Use SelectedValue instead of SelectedIndex.

SelectedValue refers to the property you've specified as the Value member - in this case, the ID of the record.

SelectedIndex is the numerical index of the item in the collection. For instance, if there are 4 items in a dropdown, the first item has index "0", the second is "1", the third is "2", and the fourth is "3". "-1" is often used for SelectedIndex when no item is selected.
0
vcharlesAuthor Commented:
Hi,

The code below works but the DropdownList is unable to display the records using the .Text property, a message just above the line where the DropdownList should show the records shows the correct data, but the Dropdown list gives me error message:  A third party ComboBox .text property also shows the correct data as I move to the next records found from my search. How do i fix the error caused by: C1CountryOrigin.Text = dtDonor.Rows(currentIndex).Item("Name")? The control was initially loaded in the Page_Load event, Do I need to clear it first, if yes, How do I do that?

Error Message: 'C1CountryOrigin' has a SelectedValue which is invalid because it does not exist in the list of items.Parameter name: value


Protected Sub Search_Click(sender As Object, e As EventArgs) Handles Search.Click
        ' create a new XML Document object for each XML file
        Dim xdDonor As New Xml.XmlDocument
        Dim xdLink As New Xml.XmlDocument
        ' load all of the XML documents
        xdDonor.Load(Server.MapPath("~/App_Data/CountryOrigin.xml"))
        xdLink.Load(Server.MapPath("~/App_Data/LinkSSADB.xml"))
        ' Extract the DonorID values from the current node
        Dim DonorID As String
        Dim DonorID2 As String
        Dim Name As String
        dtDonor = New DataTable
        dtDonor.Columns.Add("DonorID", GetType(String))
        dtDonor.Columns.Add("Name", GetType(String))
        Session("CurrentIndex") = 0
        Session("dtDonor") = dtDonor
        ' This outer loop will iterate over the Link file where the ReceiverID node is equal to our search criteria
        For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/LinkA[Manufacturer_ID='" & C1Manufacturer.SelectedItem.Value & "']")
            DonorID = xnLink.SelectSingleNode("CountryOrigin_ID").InnerText

            ' Perform another similar search based upon the Donor XML file
            For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root/CountryOriginTable[CountryOrigin_ID='" & DonorID & "']")
                DonorID2 = xnDonor.SelectSingleNode("CountryOrigin_ID").InnerText
                Name = xnDonor.SelectSingleNode("CountryOrigin").InnerText
                dtDonor.Rows.Add({DonorID2, Name})
               
            Next
        Next
        C1CountryOrigin.DataSource = dtDonor
        C1CountryOrigin.DataTextField = "Name"
        C1CountryOrigin.DataValueField = "DonorID"
        C1CountryOrigin.DataBind()
    End Sub

    Protected Sub Next_Click(sender As Object, e As EventArgs) Handles Next.Click

        dtDonor = Session("dtDonor")
        CurrentIndex = Session("CurrentIndex")
        If CurrentIndex < dtDonor.Rows.Count - 1 Then
            CurrentIndex += 1
            Session("CurrentIndex") = currentIndex
            MsgBox(dtDonor.Rows(currentIndex).Item("Name")) *** Shows correct data
            C1CountryOrigin.Text = dtDonor.Rows(currentIndex).Item("Name") ****Error
        End If
    End Sub

0
ddayx10Commented:
In part A I am populating dtdonor, and in part B (Initial post) I am trying to move through the records in dtDonor to display its text value based on the link ID.

Answer: Yes I understood what was happening to an extent. You were trying to assign a value to a dropdownlist incorrectly. I needed to know what exactly you wanted to do to the dropdownlist with your dtDonor "CountryOrigin" value.

If you were trying to make the dropdown display(select) the item that matched your dtDonor>>"CountryOrigin" then I gave you the code to do that.

How to use it?

Replace:

C1CountryOrigin.Text = dtDonor.Rows(currentIndex).Item("CountryOrigin")

With:

ddl.Items.FindByText(dtDonor.Rows(currentIndex).Item("CountryOrigin").Selected = True

If that is not what you were trying to do then I needed to know more specifically what you wanted. Your reply above seems to re-inforce that this is still what you wanted?

-------------------------------------------------------------
I don't know how to use  the code in the previous post with the DropdownList, because I am not selecting any rows, I'm just moving through the records.

Answer: You were moving through the records and trying to assign a text value to a dropdownlist. To what end is still unclear. You were not "just moving through the records".

-------------------------------------------------------------

Lastly you've posted a bunch of new code in response, but this code does not at all seem to relate to the original stated problem. As far as I can tell it doesn't even include the original problem code shown.

If you want to set the dropdownlist selected item to match the dtDonor.....("CountryOrigin") the code shown does that. If you've changed approach, dropped the original code, and now have a new problem then you need to state that and see if someone can help you.
0
vcharlesAuthor Commented:
Hi,

Thank yoiu for the clarification,  what is ddl at the beginning of your code?, I tried replacing it with the name of the DropdownList control, but it doesn't work.
0
ddayx10Commented:
Sorry, you were right to change it to the dropdownlist:

C1CountryOrigin.Items.FindByText(dtDonor.Rows(currentIndex).Item("CountryOrigin")).Selected = True

Its possible you didnt notice I made a typo in that code and it needed one more parenthesis(I just caught it):

I would have expected you to report an error if you didnt catch this though?

If that doesn't work then the dtDonor.Rows(currentIndex).Item("CountryOrigin") value is not in any of the C1CountryOrigin.Items text fields.

Maybe the "CountryOrigin" is in the "value" field of the dropdownlist listitems?

Maybe the text "case" is different, the code I gave you is case sensitive? I don't think this is it because you bound to the same column and so they should be identical, but I can't really know if the item you bound with is identical to dtDonor?

Anyway you can try this to check the items are the same:

<see code>

Also its possible depending on what else is happening on the page that this is working fine but the DropDownList.SelectedItem is being changed elsewhere.

dtDonor = Session("dtDonor")
        CurrentIndex = Session("CurrentIndex")
        If CurrentIndex < dtDonor.Rows.Count - 1 Then
            CurrentIndex += 1
            Session("CurrentIndex") = CurrentIndex
            C1CountryOrigin.Text = dtDonor.Rows(currentIndex).Item("CountryOrigin")
            MsgBox(dtDonor.Rows(currentIndex).Item("CountryOrigin") & " | " & ddl.Items.FindByText(dtDonor.Rows(currentIndex).Item("CountryOrigin")).Text
        End If

Open in new window

****DEMONSTRATION CODE****

<asp:DropDownList ID="ddl" runat="server">
    <asp:ListItem Text="One" Value="1" />
    <asp:ListItem Text="Two" Value="2" /><%--note case of text--%>
    <asp:ListItem Text="Three" Value="3" />
</asp:DropDownList>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ddl.Items.FindByText("Two").Selected = True
    'This will not work
    ddl.Items.FindByText("two").Selected = True
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddayx10Commented:
Maybe the "CountryOrigin" is in the "value" field of the dropdownlist listitems?

That was dumb...I can see in your binding that it is not the case.
0
vcharlesAuthor Commented:
Thank you for your help, I will get back to you tomorrow.

Victor
0
ddayx10Commented:
I'm taking for granted you are doing this correctly:

Dim CountryOrigin As New DataSet
        CountryOrigin.ReadXml(Server.MapPath("~/App_Data/CountryOrigin.xml"))
        C1CountryOrigin.DataSource = CountryOrigin.Tables(0)
        C1CountryOrigin.DataMember = "CountryOrigin"
        C1CountryOrigin.DataTextField = "CountryOrigin"
        C1CountryOrigin.DataValueField = "CountryOrigin_ID"
        C1CountryOrigin.DataBind()

Because you havent reported that you are not seeing the text you expect in the dropdownlist itself.

At a glance I can see potential for muck-up here, but without seeing your XML file I can't know.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.