【问题标题】:Error:DataBinding: 'System.Data.Common.DataRecordInternal' does not contain a property with the name 'EmpName_vc'错误:DataBinding:“System.Data.Common.DataRecordInternal”不包含名为“EmpName_vc”的属性
【发布时间】:2019-02-10 09:31:52
【问题描述】:

我正在尝试将提交绑定到来自 sql 数据库的下拉列表的值。我收到错误:DataBinding:“System.Data.Common.DataRecordInternal”不包含名为“EmpName_vc”的属性。我无法弄清楚到底是什么问题。我想根据传递的 sql 查询将数据绑定到下拉列表。

protected void BindSubmitTo()
    {
       try
        {
            SqlDataReader dr;
            using (SqlCommand cmd = new SqlCommand("select D.EmpId_Int, E.EmpName_vc from Dept_PM_HOD_List D Left Join Gen_Employee E on D.EmpId_Int = E.EmpId_Int where D.LCode_Int = @intDeptId and D.EmpId_Int<> @intEmpId and(E.EmpType_vc ='PM' or(E.EmpType_vc = 'HOD'))", Cnn))
            cmd.Parameters.AddWithValue("@intDeptId", LocId.Text);
            cmd.Parameters.AddWithValue("@intEmpId", EmpID.Text);
            cmd.CommandType = CommandType.Text;
            Cnn.Open();

            using (SqlDataReader dr = cmd.ExecuteReader())
            {

                if (dr.HasRows)
                {
                    Submit_dd.DataSource = dr;
                    Submit_dd.DataTextField = "EmpName_vc";
                    Submit_dd.DataValueField = "EmpId_Int";
                    Submit_dd.DataBind();
                    Submit_dd.Items.Insert(0, new ListItem("--Select--", "0"));
                }

                Cnn.Close();
            }
        }




      </div><br /><br />
              <div class="form-group col-sm-1">
                  <asp:DropDownList ID="Submit_dd" placeholder="" Cssclass="form-control" runat="server"></asp:DropDownList>
                  <asp:Label ID="sublbl_error" Text="" ForeColor="Red" runat="server" ></asp:Label>
              </div>
                </div>

         <div>
             <asp:GridView ID="gridview_TS"
                 CssClass="table table-striped table-hover"
                 runat="server"
                 GridLines="None"
                 AutoGenerateColumns="False"
                 AllowPaging="True"
                 AllowSorting="True"
                 PageSize="20"
                 DataKeyNames="EmpId_int,ProjectCode_int" DataSourceID="SqlDataSource1">
                 <Columns>

                     <asp:BoundField DataField="Chk_Int" HeaderText="Chk_Int" SortExpression="Chk_Int" Visible="False" />
                     <asp:TemplateField HeaderText="">
                         <ItemTemplate>
                             <asp:CheckBox ID="cbSelect" CssClass="form-control" runat="server" />

                         </ItemTemplate>
                     </asp:TemplateField>
                     <asp:BoundField DataField="EmpId_Int" HeaderText="EmpId_Int" />
                     <asp:BoundField DataField="TSDate_dt" HeaderText="Date" SortExpression="TSDate_dt" />
                     <asp:BoundField DataField="ProjectCode_int" HeaderText="Project Code" SortExpression="ProjectCode_int" />
                     <asp:BoundField DataField="ProjectName_vc" HeaderText="Project Name" SortExpression="ProjectName_vc" />

                     <asp:BoundField DataField="CCode_Int" HeaderText="CCode_Int" Visible="False" />
                     <asp:BoundField DataField="CostCode_vc" HeaderText="Cost Code Group" SortExpression="CostCode_vc" />
                     <asp:BoundField DataField="CCode_vc" HeaderText="Cost Code Description" SortExpression="CCode_vc" />

                     <asp:BoundField DataField="TSRhours_nu" HeaderText="Reg. Hours" SortExpression="TSRhours_nu" />
                     <asp:BoundField DataField="TSOhours_nu" HeaderText="OT. Hours" SortExpression="TSOhours_nu" />
                     <asp:BoundField DataField="JobDescp_vc" HeaderText="Job Narration" SortExpression="JobDescp_vc" />
                     <asp:BoundField DataField="EmpName_vc" HeaderText="Submit To" SortExpression="True" />
                     <asp:BoundField DataField="SubmitTo_Int" HeaderText="SubmitTo_Int" Visible="False" />

                 </Columns>
             </asp:GridView>
             <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TSMSConnectionString %>" 
                 SelectCommand="SELECT S.EmpId_Int, T.Chk_Int, T.TSDate_dt, T.ProjectCode_Int, P.ProjectName_vc, 
                 T.CCode_Int, C.CostCode_vc, C.CCode_vc, T.TSRhours_nu, T.TSOhours_nu, T.JobDescp_vc, E.EmpName_vc,
                  T.SubmitTo_Int FROM TSHistory T LEFT JOIN Sec_Users S ON T.EmpId_Int= S.EmpId_Int 
                 LEFT JOIN Gen_ProjectList P ON T.ProjectCode_Int = P.ProjectCode_Int 
                 LEFT JOIN Gen_CCList C ON T.CCode_Int= C.CCode_Int LEFT JOIN Gen_Employee E 
                 ON T.EmpId_Int = E.EmpId_Int WHERE (S.UserId_vc = @userId) AND (T.SubmitTo_Int = 0) ORDER BY T.TSDate_dt DESC ">

                 <SelectParameters>
                     <asp:SessionParameter DefaultValue ="" Name="userId" SessionField="user" Type="string" />
                 </SelectParameters>

             </asp:SqlDataSource>
         </div>

         <div class="btn btm-default btn-block" style="width:100%">
             <asp:Button ID="create_TS" runat="server" Text="Create" class="btn btn-success mr-5" OnClick="btnCreate_click" />
             <asp:Button ID="delete_TS" runat="server" Text="Delete" class="btn btn-danger mr-5" />
             <asp:Button ID="update_TS" runat="server" Text="Update" class="btn btn-info mr-5" />
             <asp:Button ID="clear_TS" runat="server" Text="Clear" CssClass="btn btn-outline-light mr-5" />
        </div>

【问题讨论】:

    标签: c# asp.net forms bind


    【解决方案1】:

    不直接提供源码,使用DataTable如下:

    using (SqlDataReader dr = cmd.ExecuteReader())
    {
        DataTable dt = new DataTable();
        dt.Load(dr); 
    
        if (dt.Rows.Count > 0)
        {
            Submit_dd.DataSource = dt;  
            Submit_dd.DataBind();  
            Submit_dd.DataTextField = "EmpName_vc";
            Submit_dd.DataValueField = "EmpId_Int";
            Submit_dd.DataBind();
            Submit_dd.Items.Insert(0, new ListItem("--Select--", "0"));
        }
        Cnn.Close();
    }
    

    这将解决您的问题。

    【讨论】:

    • 它给出一个错误:SQLDataReader 不包含 'Fill' 的定义。
    • 您可以使用SqlDataAdapter 代替SqlDataReader。或者我已经修改了答案,如果有不好的事情发生,请检查并告诉我。
    • 嗨。在阅读您的更新解决方案之前,我设法解决了此查询。如果你能帮我解决这个问题,我还有一个问题。我刚才也发了。我将不胜感激!
    • 当然@samyafayaz
    猜你喜欢
    • 1970-01-01
    • 2016-08-24
    • 2017-12-27
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多