Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with loading data in dropdownlist of a web form

Hello,

I am able to load my dropdown list in my Windows application. However when I transfer the code to my ASP.NET application.  The program doesn't understand the properties where the Error line is listed (DisplayMember, ValueMember ). Which properties do 'i need to replace them with for the web application?

code:
 Dim lnk As XmlNodeList = LinkStream.SelectNodes("Root/Link")
        Dim RecName As XmlNodeList = RecStream.SelectNodes("Root/Receiver")
        Dim DonorName As XmlNodeList = DonorStream.SelectNodes("Root/Donor")

        Dim donorlst As New DataTable()
        donorlst.Columns.Add("ID")
        donorlst.Columns.Add("Name")
        Dim reclst As New DataTable
        reclst.Columns.Add("ID")
        reclst.Columns.Add("Name")

        For Each n As XmlNode In lnk
            Dim rID As Integer = Val(n.ChildNodes(0).InnerText)
            Dim dID As Integer = Val(n.ChildNodes(1).InnerText)

            For Each node As XmlNode In RecName
                If node.ChildNodes(0).InnerText = rID.ToString Then
                    reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerText})
                    'Exit For
                End If
            Next

            For Each node As XmlNode In DonorName
                If node.ChildNodes(0).InnerText = dID.ToString Then
                    donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerText, node.ChildNodes(1).InnerText})
                    'Exit For
                End If
            Next
        Next

        CmbDonor.DataSource = donorlst
        CmbDonor.DisplayMember = "Name"  *****Error
        CmbDonor.ValueMember = "ID" ********Error

Thanks,

Victor
Avatar of binaryevo
binaryevo
Flag of United States of America image

DataTextField and DataValueField.  See below:
this.drop1.DataSource = Domain.DataEntity.LoadStuff();
                this.drop1.DataTextField = "TextValue";
                this.drop1.DataValueField = "IDValue";
                this.drop1.DataBind();

Open in new window

Avatar of Victor  Charles

ASKER

Thanks for the code, I will get back to you tomorrow.
I tried modifying the code below but I receive the following error:

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

Code:
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("Root/Donor")

        Dim donorlst As New DataTable()
        donorlst.Columns.Add("DonorID")
        donorlst.Columns.Add("Name")
       
        For Each node As XmlNode In DonorName
            donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerText, node.ChildNodes(1).InnerText})
        Next

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

Below is an example of Donor.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Donor>
    <DonorID>1</DonorID>
    <Name>BEL</Name>
  </Donor>
  <Donor>
    <DonorID>2</DonorID>
    <Name>USA</Name>
  </Donor>
  <Donor>
    <DonorID>3</DonorID>
    <Name>CAN</Name>
  </Donor>
  <Donor>
    <DonorID>4</DonorID>
    <Name>DEU</Name>
  </Donor>
  <Donor>
    <DonorID>5</DonorID>
    <Name>HGH</Name>
  </Donor>
</Root>

Thanks.

Victoir
I don't use datatables very often but I think the syntax goes something like this dataset.tables[0].rows[0][columnname].  So in your case instead of saying datatextfield and datavaluefield = columnname you would use the above syntax.  I'm doing this from memory so you may need to play with the exact syntax a little bit.  Give that a shot and let me know.
I'm afraid the program doesn't understand the "Dataset", below is my code, can you please show me how you would modify it to use the code you are proposing.

Public Sub FillCombos()
        Dim DonorName As XmlNodeList = DonorStream.SelectNodes("Root/Donor")

        Dim donorlst As New DataTable()
        donorlst.Columns.Add("DonorID")
        donorlst.Columns.Add("Name")
       
        For Each node As XmlNode In DonorName
            donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerText, node.ChildNodes(1).InnerText})
        Next

        CmbDonor.DataSource = donorlst

        CmbDonor.DataTextField = "Name"
        CmbDonor.DataValueField =
        CmbDonor.DataBind()
End Sub

Thanks,

Victor
        CmbDonor.DataSource = donorlst

        CmbDonor.DataTextField = donorlst.rows(0)("name")
        CmbDonor.DataValueField = donorlst.rows(0)("id")
        CmbDonor.DataBind()

Try that!
I don't know why I'm getting this error message:

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'CAN'.

Below is my xml files. Any ideas what is causing this error?

Donor:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Donor>
    <DonorID>1</DonorID>
    <Name>BEL</Name>
  </Donor>
  <Donor>
    <DonorID>2</DonorID>
    <Name>USA</Name>
  </Donor>
  <Donor>
    <DonorID>3</DonorID>
    <Name>CAN</Name>
  </Donor>
  <Donor>
    <DonorID>4</DonorID>
    <Name>DEU</Name>
  </Donor>
 </Root>

Receiver:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Receiver>
    <ReceiverID>1</ReceiverID>
    <Name>BEL</Name>
  </Receiver>
  <Receiver>
    <ReceiverID>2</ReceiverID>
    <Name>USA</Name>
  </Receiver>
  <Receiver>
    <ReceiverID>3</ReceiverID>
    <Name>CAN</Name>
  </Receiver>
  <Receiver>
    <ReceiverID>4</ReceiverID>
    <Name>DEU</Name>
  </Receiver>
</Root>

Link:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Link>
    <LinkID>1</LinkID>
    <ReceiverID>3</ReceiverID>
    <DonorID>2</DonorID>
  </Link>
  <Link>
    <LinkID>1</LinkID>
    <ReceiverID>1</ReceiverID>
    <DonorID>3</DonorID>
  </Link>
  <Link>
    <LinkID>1</LinkID>
    <ReceiverID>2</ReceiverID>
    <DonorID>1</DonorID>
  </Link>
</Root>

Thanks,

Victor
Its hard to telll without the source code but, usually an error like that indicates that you are trying to use the property / column "CAN" to bind to.  Please check your code and ensure that you are in fact using DonorID and Name when you add the columns and DonorID and Name when you give the DataTextField and DataValueField.  
I ran the program again and noticed, The error is occuring on the first "Name" element of Donor.xml

Error: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Test'.

Any ideas what  is causing the error on this line?

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <Donor>
    <DonorID>1</DonorID>
    <Name>Test</Name> ***** Error Line
  </Donor>
  <Donor>
    <DonorID>2</DonorID>
    <Name>USA</Name>
  </Donor>
  <Donor>
    <DonorID>3</DonorID>
    <Name>CAN</Name>
  </Donor>
  <Donor>
    <DonorID>4</DonorID>
    <Name>DEU</Name>
  </Donor>
 </Root>

Victor
ASKER CERTIFIED SOLUTION
Avatar of binaryevo
binaryevo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the code, it works but when I select a row from cmbDonor, the value selected is not registered, for example when I try to see the value selected in a meaasge box with the code below, I get the value of the first row, even when I set the Auto postback property to True.
 MsgBox(CmbDonor.SelectedItem.ToString) Any ideas what I'm doing wrong?
I will ask the post follow up question as another post, will close this post.

Thanks,

Victor