PLEASE HELP ME TO SOLVE THIS ERROR
cs file
public partial class Control : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
OrderDataRepository rep = new OrderDataRepository();
var results = rep.GetAllOrderData().
GroupBy(o => o.DRIVER_ID).
Select(g =>
new
{
DriverId = g.Key,
OrderCount = g.Count(),
OrderCountWhereNameIsNotNull =
g.Count(o => o.RECEIVE_NAME != null)
}).ToList();
DataViewer.DataSource = results;
DataViewer.DataBind();
}
}
aspx file
<asp:GridView ID="DataViewer" runat="server">
<Columns>
<TemplateColumn>
<ItemTemplate>
<div style='width: <%# Eval("OrderCount") %>' />
</ItemTemplate>
</TemplateColumn>
</Columns>
</asp:GridView>
Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. Parser Error Message: System.Web.UI.WebControls.DataControlFieldCollection must have items of type ‘System.Web.UI.WebControls.DataControlField’. ‘TemplateColumn’ is of type ‘System.Web.UI.HtmlControls.HtmlGenericControl

int OrderCount, OrderCountWhereNameIsNotNull;
System.Web.UI.WebControls.TableRow oRow;
System.Web.UI.WebControls.TableCell oCell;
System.Web.UI.HtmlControls.HtmlGenericControl oDiv;
while (true)
//loop through records
//do while not eof
{
oRow = new System.Web.UI.WebControls.TableRow();
oCell = new System.Web.UI.WebControls.TableCell();
oDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
OrderCount = 200; //get value from DB, convert to meaningful width
OrderCountWhereNameIsNotNull = 100; //get value from DB, convert to meaningful width
oDiv.InnerHtml = "<div style='border: 3px solid black; width: " + OrderCount + "px;'>";
oDiv.InnerHtml += Environment.NewLine + " <div style='border: 0px; background-color: red; width: " + OrderCountWhereNameIsNotNull + "px;'> </div>";
oDiv.InnerHtml += Environment.NewLine + "</div>";
oCell.Controls.Add(oDiv);
oRow.Cells.Add(oCell);
tblData.Rows.Add(oRow);
}
Change this
aspx file
to this
aspx file
Explanation: The error message does in fact tell you what’s wrong, although it’s cryptic if you don’t know what it means! Here’s a breakdown:
This means that something went wrong while the ASP.NET engine was examining a source file. The hints are ‘request’ (as in, a web request was made) and ‘during the parsing of a resource’.
The error is in a file parsed by asp.net, not the C# compiler. This means the problem is in the
aspx, not a.csfile.Here, something is expecting to contain things which are
DataControlFields, but you’ve given it aTemplateColumn, which is aHtmlGenericControl(which isn’t aDataControlField, so isn’t what it wants). So we examine the aspx markup and say, where is there aTemplateControl? And we see that yourGridView\Columnscollection has aTemplateControl. Whereas (on checking the help) it should be directly containing anItemTemplate, when you want a templated column. And we’re done.