我正在尝试将数据表绑定到aspx上的转发器。我在“DataBind”方法中得到了下面提到的错误。从昨天开始,我已经厌倦了试图解决这个问题。这是我的代码:
ASPX:
<asp:Repeater ID="HistoryList" runat="server">
<HeaderTemplate>
<table class="dataTable" style="width:100%;border-collapse: collapse;padding:0px 0px 0px 0px;">
<tr>
<td class="dataHeaderCell">#</td>
<td class="dataHeaderCell">Date</td>
<td class="dataHeaderCell">Dealer</td>
<td class="dataHeaderCell">Security</td>
<td class="dataHeaderCell">Bid</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<td class="dataCell right" title='<%# DataBinder.Eval(Container.DataItem, "HistoryId") %>'><%# Container.ItemIndex + 1 %></td>
<td class="dataCell"><%# (null != DataBinder.Eval(Container.DataItem, "EffectiveDate")) ? ((DateTime)DataBinder.Eval(Container.DataItem, "EffectiveDate")).ToString("d") : "..."%></td>
<td class="dataCell"><%# DataBinder.Eval(Container.DataItem, "OrganizationName") %></td>
<td class="dataCell nowrap"><a target="_blank" title="Click Here to View Name..." href='NameView.aspx?page=27&nid=<%# DataBinder.Eval(Container.DataItem, "SecurityId") %>'><%# (60587 == (int)DataBinder.Eval(Container.DataItem, "NameId")) ? DataBinder.Eval(Container.DataItem, "Expr1").ToString() + " (Unknown)" : DataBinder.Eval(Container.DataItem, "Name").ToString() %></a></td>
<td class="dataCell nowrap"><%# DataBinder.Eval(Container.DataItem, "Bid") %></td>
</ItemTemplate>
</table>
</asp:Repeater>
CS.aspx:
string dbConnect = System.Configuration.ConfigurationManager.AppSettings["dbConnect"].ToString();
SqlConnection thisConnection = new SqlConnection(@dbConnect);
thisConnection.Open();
SqlCommand customCommand = new SqlCommand(queryString.ToString());
customCommand.Connection = thisConnection;
customCommand.CommandType = System.Data.CommandType.Text;
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(customCommand4);
adapter.Fill(dt);
DataTable newsDataTable = new DataTable();
foreach(DataRow dr in dt.Rows)
{
//check for something
if (true)
{
newsDataTable.ImportRow(dr);
}
}
if (0 < newsDataTable.Rows.Count)
{
HistoryList.DataSource = newsDataTable;
HistoryList.DataBind();
HistoryList.Visible = true;
HistoryTitle.Text = string.Format(HistoryTitle.Text, HistoryColor.Count);
}
我正在努力解决的错误是:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'HistoryId'. ---> at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName)
at System.Web.UI.DataBinder.Eval(Object container, String[] expressionParts)
at System.Web.UI.DataBinder.Eval(Object container, String expression)
at ASP.comps_aspx.__DataBind__control60(Object sender, EventArgs e) in c:\Users\sk\Documents\Visual Studio 2013\Projects\DynamicWeb\Comps.aspx:line 562
at System.Web.UI.Control.OnDataBinding(EventArgs e)
at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
at System.Web.UI.Control.DataBind()
at System.Web.UI.Control.DataBindChildren()
at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
at System.Web.UI.Control.DataBind()
at System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem)
at System.Web.UI.WebControls.Repeater.AddDataItemsIntoItemsArray(IEnumerable dataSource, Boolean useDataSource)
at System.Web.UI.WebControls.Repeater.PostGetDataAction(IEnumerable dataSource)
at System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource)
at System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e)
at System.Web.UI.WebControls.Repeater.DataBind()
at ClarityDynamicWeb.Comps.Page_Load(Object sender, EventArgs e) in c:\Users\sk\Documents\Visual Studio 2013\Projects\DynamicWeb\Comps.aspx.cs:line 593
任何人都可以指出我错过了什么或做错了吗?谢谢。
答案 0 :(得分:0)
修改DataTable newsDataTable = new DataTable();
使用DataTable newsDataTable = dt.Clone();
newsDataTable.ImportRow(dr);
而不是newsDataTable.Rows.Add(dr.ItemArray);