Solved

Help with loading data in dropdownlist of a web form

Posted on 2011-09-09
12
323 Views
Last Modified: 2012-05-12
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
Comment
Question by:vcharles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 14

Expert Comment

by:binaryevo
ID: 36513255
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
 

Author Comment

by:vcharles
ID: 36513442
Thanks for the code, I will get back to you tomorrow.
0
 

Author Comment

by:vcharles
ID: 36514973
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 14

Expert Comment

by:binaryevo
ID: 36516146
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
 

Author Comment

by:vcharles
ID: 36517558
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
 
LVL 14

Expert Comment

by:binaryevo
ID: 36517608
        CmbDonor.DataSource = donorlst

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

Try that!
0
 

Author Comment

by:vcharles
ID: 36517634
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
 
LVL 14

Expert Comment

by:binaryevo
ID: 36522612
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
 

Author Comment

by:vcharles
ID: 36532502
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
 
LVL 14

Accepted Solution

by:
binaryevo earned 500 total points
ID: 36545482
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
 

Author Comment

by:vcharles
ID: 36546169
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
 

Author Comment

by:vcharles
ID: 36547474
I will ask the post follow up question as another post, will close this post.

Thanks,

Victor
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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