数据绑定:“ System.Data.DataRowView”不包含名称为“ DEPARTMENT_NAME”的属性

时间:2019-10-04 03:32:41

标签: c# sql asp.net

我的下拉菜单功能有问题。下拉功能假定从数据库中获取值。我认为问题出在sql select命令上,但对于这种东西(asp.net和sql)我还是陌生的。有人可以帮我吗,谢谢你。

这是SQL DataSourceID

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:*****ConnectionString %>" SelectCommand="SELECT TOP 10 
 C.CASE_KEY, C.DEPARTMENT_CASE_NUMBER, D.DEPARTMENT_NAME, O.OFFENSE_DESCRIPTION AS CHARGE, LAB_CASE, 
   OFFENSE_DATE

   FROM TV_LABCASE C

   INNER JOIN TV_DEPTNAME D ON C.DEPARTMENT_CODE = D.DEPARTMENT_CODE

    INNER JOIN TV_OFFENSE O ON C.OFFENSE_CODE = O.OFFENSE_CODE

    ORDER BY CASE_DATE DESC

   "></asp:SqlDataSource>

输入字段的代码

 <table class="style2" >
    <tr>
        <td class="style3" >Department Case #</td>
        <td> <asp:TextBox ID="TextBox1" runat="server" Enabled="False" ontextchanged="btnCancel_Click"></asp:TextBox></td>
    </tr>

    <tr>
         <td class="style3">Department</td>
         <td> 
             <asp:DropDownList ID="DropDownList1" runat="server" 
                  Height="18px" Width="166px" Enabled="False">
             </asp:DropDownList>
         </td>
    </tr>

    <tr> 
         <td class="style3">Charge</td>
         <td>
             <asp:DropDownList ID="DropDownList2" runat="server" 
                 Height="25px" Width="165px" Enabled="False">
             </asp:DropDownList>
         </td>
    </tr>

    <tr>
        <td class="style3">Lab Case #</td>
        <td><asp:TextBox ID="TextBox4" runat="server" Enabled="False"  ontextchanged="btnCancel_Click"></asp:TextBox></td>
   </tr>

   <tr>
       <td class="style3">Incident Report Date</td>
       <td><asp:TextBox ID="TextBox5" runat="server" Enabled="False" ontextchanged="btnCancel_Click"></asp:TextBox></td>
   </tr>

</table>

ASP.NET C#(服务器端代码)

     protected void Page_Load(object sender, EventArgs e)
      {
        string connetionString;
        SqlConnection cnn;
        connetionString = @"Data Source=A**SE****D***\MSSQL****;Initial Catalog=****;User 
        ID=****;Password=****";
        cnn = new SqlConnection(connetionString);
        cnn.Open();


        SqlCommand cmd = new SqlCommand("select * from TV_LABCASE", cnn);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataBind();
        DropDownList1.DataTextField = "DEPARTMENT_NAME";
        DropDownList1.DataValueField = "DEPARTMENT_CODE";
        DropDownList1.DataBind();

        DropDownList2.DataSource = dt;
        DropDownList2.DataBind();
        DropDownList2.DataTextField = "OFFENSE_DESCRIPTION";
        DropDownList2.DataValueField = "OFFENSE_CODE";
        DropDownList2.DataBind();


    }

2 个答案:

答案 0 :(得分:1)

替换您的命令查询以连接TV_DEPTNAME

SqlCommand cmd = new SqlCommand("select C.*, D.DEPARTMENT_NAME from TV_LABCASE C INNER JOIN TV_DEPTNAME D ON C.DEPARTMENT_CODE = D.DEPARTMENT_CODE, ", cnn);

或将您的datasource更改为

DropDownList1.DataSource = SqlDataSource1;
DropDownList1.DataBind();
DropDownList1.DataTextField = "DEPARTMENT_NAME";
DropDownList1.DataValueField = "DEPARTMENT_CODE";
DropDownList1.DataBind();


DropDownList2.DataSource = SqlDataSource1;
DropDownList2.DataBind();
DropDownList2.DataTextField = "CHARGE";

并更新您的asp:SqlDataSource配置

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:*****ConnectionString %>" 
    SelectCommand="SELECT TOP 10 
        C.CASE_KEY, C.DEPARTMENT_CASE_NUMBER, D.DEPARTMENT_NAME, O.OFFENSE_DESCRIPTION AS CHARGE, LAB_CASE, 
        OFFENSE_DATE, C.DEPARTMENT_CODE
        FROM TV_LABCASE C
        INNER JOIN TV_DEPTNAME D ON C.DEPARTMENT_CODE = D.DEPARTMENT_CODE
        INNER JOIN TV_OFFENSE O ON C.OFFENSE_CODE = O.OFFENSE_CODE
        ORDER BY C.CASE_DATE DESC"
></asp:SqlDataSource>

答案 1 :(得分:1)

用于输入字段的代码使该值为true。尝试

<table class="style2" >
    <tr>
        <td class="style3" >Department Case #</td>
        <td> <asp:TextBox ID="TextBox1" runat="server" Enabled="true" ontextchanged="btnCancel_Click"></asp:TextBox></td>
    </tr>

    <tr>
         <td class="style3">Department</td>
         <td> 
             <asp:DropDownList ID="DropDownList1" runat="server" 
                  Height="18px" Width="166px" Enabled="true">
             </asp:DropDownList>
         </td>
    </tr>

    <tr> 
         <td class="style3">Charge</td>
         <td>
             <asp:DropDownList ID="DropDownList2" runat="server" 
                 Height="25px" Width="165px" Enabled="true">
             </asp:DropDownList>
         </td>
    </tr>

    <tr>
        <td class="style3">Lab Case #</td>
        <td><asp:TextBox ID="TextBox4" runat="server" Enabled="true"  ontextchanged="btnCancel_Click"></asp:TextBox></td>
   </tr>

   <tr>
       <td class="style3">Incident Report Date</td>
       <td><asp:TextBox ID="TextBox5" runat="server" Enabled="true" ontextchanged="btnCancel_Click"></asp:TextBox></td>
   </tr>

</table>

ASP.NET C#(服务器端代码)

 protected void Page_Load(object sender, EventArgs e)
      {
        string connetionString;
        SqlConnection cnn;
        connetionString = @"Data Source=A**SE****D***\MSSQL****;Initial Catalog=****;User 
        ID=****;Password=****";
        cnn = new SqlConnection(connetionString);
        cnn.Open();


        SqlCommand cmd = new SqlCommand(@"SELECT * from TV_LABCASE C Left join TV_DEPTNAME D ON C.DEPARTMENT_CODE = D.DEPARTMENT_CODE Left join TV_OFFENSE O ON C.OFFENSE_CODE = O.OFFENSE_CODE", cnn);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataBind();
        DropDownList1.DataTextField = "DEPARTMENT_NAME";
        DropDownList1.DataValueField = "DEPARTMENT_CODE";
        DropDownList1.DataBind();

        DropDownList2.DataSource = dt;
        DropDownList2.DataBind();
        DropDownList2.DataTextField = "OFFENSE_DESCRIPTION";
        DropDownList2.DataValueField = "OFFENSE_CODE";
        DropDownList2.DataBind();


    }