Solved

Help with loading data in dropdownlist of a web form

Posted on 2011-09-09
12
319 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
  • 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now