【问题标题】:DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'DEPARTMENT_NAME' [duplicate]DataBinding:“System.Data.DataRowView”不包含名为“DEPARTMENT_NAME”的属性 [重复]
【发布时间】:2019-10-04 03:32:41
【问题描述】:

我的下拉功能有问题。下拉函数假设从数据库中获取值。我认为问题出在 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();


    }

【问题讨论】:

    标签: c# sql asp.net


    【解决方案1】:

    输入字段的代码使值为真。试试看

    <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();
    
    
        }
    

    【讨论】:

      【解决方案2】:

      替换您的命令查询以加入 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>
      

      【讨论】:

      • 第二个答案输出:DataBinding:“System.Data.DataRowView”不包含名为“DEPARTMENT_CODE”的属性。
      • 第一个答案输出不正确的语法
      • 还包括DropDownList2.DataSource = SqlDataSource1;
      • 更改为DropDownList2.DataTextField = "CHARGE";
      • 太棒了。实际上你可以删除你的 SqlCommandSqlConnection 对象
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      相关资源
      最近更新 更多