DataBind()System.Web.HttpException数据源中找不到ID

时间:2018-01-03 11:14:34

标签: c# asp.net data-binding webforms

我正在使用visual studio制作网络表单

我在.aspx中有一个网格,我将使用c#

在后端页面中填充它

这是.aspx

中的代码
  <asp:GridView ID="gvusers" runat="server" AutoGenerateColumns="false"> 

            <Columns>
                 <asp:BoundField DataField="ID" HeaderText="ID" />
                <asp:BoundField DataField="First Name" HeaderText="First Name" />
                  <asp:BoundField DataField="Last Name" HeaderText="Last Name" />
                    <asp:BoundField DataField="Phone Number" HeaderText="Phone Number" />




            </Columns>

        </asp:GridView> 

这是应该填充它的函数(aspx.cs):

void GridFill() {
            using (MySqlConnection sqlCon = new MySqlConnection(connectionString))
            {
                sqlCon.Open();
                MySqlDataAdapter sqlda = new MySqlDataAdapter("userviewall", sqlCon);
                sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataTable dtbl = new DataTable();
                sqlda.Fill(dtbl);
                gvusers.DataSource = dtbl;
                gvusers.DataBind();
            }

我无法检测到此异常的来源,因此我可以修复它。

堆栈追踪:

[HttpException (0x80004005): لم يتم العثور على حقل أو خاصية بالاسم 'ID' في مصدر البيانات المحدد.]
   System.Web.UI.WebControls.BoundField.TryGetSimplePropertyValue(Object dataItem, Object& data) +416
   System.Web.UI.WebControls.BoundField.GetValue(Control controlContainer) +384
   System.Web.UI.WebControls.BoundField.OnDataBindField(Object sender, EventArgs e) +147
   System.Web.UI.Control.OnDataBinding(EventArgs e) +165
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +215
   System.Web.UI.Control.DataBind() +36
   System.Web.UI.Control.DataBindChildren() +244
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +225
   System.Web.UI.Control.DataBind() +36
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +243
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +7051
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +117
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +65
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +208
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +131
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +226
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +140
   System.Web.UI.WebControls.GridView.DataBind() +29
   NoufsTask.Index.GridFill() in c:\Users\abdulaziz\Desktop\NoufsTask\NoufsTask\Index.aspx.cs:77
   NoufsTask.Index.Page_Load(Object sender, EventArgs e) in c:\Users\abdulaziz\Desktop\NoufsTask\NoufsTask\Index.aspx.cs:22
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +159
   System.Web.UI.Control.OnLoad(EventArgs e) +165
   System.Web.UI.Control.LoadRecursive() +170
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3615

1 个答案:

答案 0 :(得分:0)

我猜您正确的错误信息是:

  

在所选数据源

上找不到名称为“ID”的字段或属性

解决方案:

您没有在选择查询中选择IDuserviewall它应该是SQL查询),以便出现错误消息。只需在您的查询中选择ID(哪些列需要在网格中绑定)。

重要: 不仅是ID提交,您应该将所有字段返回到网格视图中的数据绑定。您的选择查询应该类似于

select ID, FirsName, LastName,PhoneNumber from YourTableName // or try select * from YourTableName

此外,您应该在"FirstName" DataFiled属性中为Data grid Bound Filed提供列名。 DataField媒体资源不接受variable space。因此,您应该将datafield属性设为DataField="FirstName",而不是DataField="First Name"

请查看此论坛site