Victor Charles
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("Ro ot/Link")
Dim RecName As XmlNodeList = RecStream.SelectNodes("Roo t/Receiver ")
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("R oot/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).InnerT ext)
Dim dID As Integer = Val(n.ChildNodes(1).InnerT ext)
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe xt = rID.ToString Then
reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerTe xt})
'Exit For
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe xt = dID.ToString Then
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT ext, node.ChildNodes(1).InnerTe xt})
'Exit For
End If
Next
Next
CmbDonor.DataSource = donorlst
CmbDonor.DisplayMember = "Name" *****Error
CmbDonor.ValueMember = "ID" ********Error
Thanks,
Victor
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("Ro
Dim RecName As XmlNodeList = RecStream.SelectNodes("Roo
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("R
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).InnerT
Dim dID As Integer = Val(n.ChildNodes(1).InnerT
For Each node As XmlNode In RecName
If node.ChildNodes(0).InnerTe
reclst.Rows.Add(New Object() {rID, node.ChildNodes(1).InnerTe
'Exit For
End If
Next
For Each node As XmlNode In DonorName
If node.ChildNodes(0).InnerTe
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT
'Exit For
End If
Next
Next
CmbDonor.DataSource = donorlst
CmbDonor.DisplayMember = "Name" *****Error
CmbDonor.ValueMember = "ID" ********Error
Thanks,
Victor
ASKER
Thanks for the code, I will get back to you tomorrow.
ASKER
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("R oot/Donor" )
Dim donorlst As New DataTable()
donorlst.Columns.Add("Dono rID")
donorlst.Columns.Add("Name ")
For Each node As XmlNode In DonorName
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT ext, node.ChildNodes(1).InnerTe xt})
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
'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("R
Dim donorlst As New DataTable()
donorlst.Columns.Add("Dono
donorlst.Columns.Add("Name
For Each node As XmlNode In DonorName
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT
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.
ASKER
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("R oot/Donor" )
Dim donorlst As New DataTable()
donorlst.Columns.Add("Dono rID")
donorlst.Columns.Add("Name ")
For Each node As XmlNode In DonorName
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT ext, node.ChildNodes(1).InnerTe xt})
Next
CmbDonor.DataSource = donorlst
CmbDonor.DataTextField = "Name"
CmbDonor.DataValueField =
CmbDonor.DataBind()
End Sub
Thanks,
Victor
Public Sub FillCombos()
Dim DonorName As XmlNodeList = DonorStream.SelectNodes("R
Dim donorlst As New DataTable()
donorlst.Columns.Add("Dono
donorlst.Columns.Add("Name
For Each node As XmlNode In DonorName
donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerT
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!
CmbDonor.DataTextField = donorlst.rows(0)("name")
CmbDonor.DataValueField = donorlst.rows(0)("id")
CmbDonor.DataBind()
Try that!
ASKER
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
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.
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.SelectedIt em.ToStrin g) Any ideas what I'm doing wrong?
MsgBox(CmbDonor.SelectedIt
ASKER
I will ask the post follow up question as another post, will close this post.
Thanks,
Victor
Thanks,
Victor
Open in new window