0

i am quite new to asp.net I downloaded this xll file with is like this

    <?xml version="1.0" encoding="utf-8"?>
<countries author="Banmeet Singh" title="Country, State-Province selections"
date="2008-Feb-05">
  <country name="Afghanistan">
    <state>Badakhshan</state>
    <state>Badghis</state>
    <state>Baghlan</state>
    <state>Balkh</state>
    <state>Bamian</state>
    <state>Farah</state>
    <state>Faryab</state>
    <state>Ghazni</state>
    <state>Ghowr</state>
    <state>Helmand</state>
    <state>Herat</state>
    <state>Jowzjan</state>
    <state>Kabol</state>
    <state>Kandahar</state>

And the list goes on. Now i want to display the countries in drop down list, so here is the code in c# on page_load

     DataSet myDataSet = new DataSet();

        myDataSet.ReadXml(Server.MapPath("xml/country_state.xml"));
        DropDownList1.DataSource = myDataSet;
        DropDownList1.DataBind();
        DropDownList1.DataTextField = "country";
        DropDownList1.DataBind();

Ir gives this error :- DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'state' Please tell me what i am doing wrong. thanks

Ok updates, here is the html code

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>



    </form>
</body>
</html>
4

1 回答 1

0

您必须将您的列表绑定TableDataset

所以你的绑定应该更像这样(假设你在Dataset'中只有1个表)

DropDownList1.DataSource = myDataSet.Tables[0];

也尝试像这样绑定到列:

DropDownList1.DataTextField = ds.Tables[0].Columns["country"].ToString();

最后 - 你不需要Bind()两次,只需一次。设置好所有属性后通常是个好地方。

关于你的state错误 - 你确定你发布了所有涉及的代码吗?只有当您尝试访问“状态”并且您看起来不像您发布的内容时,才会出现这种情况。

于 2013-07-12T17:19:07.180 回答