Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

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
0
vcharles
Asked:
vcharles
  • 7
  • 5
1 Solution
 
binaryevoCommented:
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

0
 
vcharlesAuthor Commented:
Thanks for the code, I will get back to you tomorrow.
0
 
vcharlesAuthor Commented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
binaryevoCommented:
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.
0
 
vcharlesAuthor Commented:
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
0
 
binaryevoCommented:
        CmbDonor.DataSource = donorlst

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

Try that!
0
 
vcharlesAuthor Commented:
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
0
 
binaryevoCommented:
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.  
0
 
vcharlesAuthor Commented:
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
0
 
binaryevoCommented:
Why dont you just declare a dataset and use the .ReadXML method?   See below:

Dataset ds = new Dataset();
ds.ReadXML("Donor.xml");

this.cmbDonor.DataSource = ds.Tables[0];
this.cmbDonor.DataTextField = "Name";
this.cmbDonor.DataValueField = "DonorID";
this.cmbDonor.DataBind();

Open in new window

0
 
vcharlesAuthor Commented:
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?
0
 
vcharlesAuthor Commented:
I will ask the post follow up question as another post, will close this post.

Thanks,

Victor
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now