hi,
I am trying to return some columns from my ms sql table and assigning url links to each row returned. i am trying to do so via a gridview.
this is the asp code i am using:
X marks the spot
Wheni run it i get the follow error:
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: Type 'System.Web.UI.WebControls.TemplateField' does not have a public property named 'DataField'.

Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 11, 2024, 10:19 AM EDT
Check your code & compare this one.
Initialize GridView and Label Controls: Ensure that the GridView and label controls are properly initialized in the markup file (e.g., ASPX page) and linked to the corresponding controls in the code-behind.
Handle Data Binding Logic: Modify the
BindGridViewmethod to include proper initialization and error handling for the controls.private void BindGridView() { if (Session["userId"] != null) { string userId = Session["userId"].ToString(); string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; string query = "SELECT client_ref, ck_ref, first_name, second_name, status FROM md_temp WHERE client_code = @userId"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(query, connection); command.Parameters.AddWithValue("@userId", userId); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable dt = new DataTable(); adapter.Fill(dt); if (dt.Rows.Count > 0) { GridViewMatters.DataSource = dt; GridViewMatters.DataBind(); NoRecordsExist.Visible = false; // Hide the label when records exist } else { GridViewMatters.DataSource = null; GridViewMatters.DataBind(); NoRecordsExist.Visible = true; // Show the label when no records exist } } } }One more thing, please ensure that the data is being retrieved successfully by running your query and debug the code. If the data is retrieved, then check if it is bound to the GridView
Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 11, 2024, 8:06 AM EDT
The error message "Type 'System.Web.UI.WebControls.TemplateField' does not have a public property named 'DataField'" suggests that the TemplateField in the GridView is incorrectly configured. TemplateFields do not have a 'DataField' property; instead, you should use controls like Label or Literal within the ItemTemplate to display dynamic content.
'>fergus72 reputationPosted Apr 11, 2024, 8:16 AM EDT
@Jayray thank you.....but its pulled up an error on the next time for itemTemplate:
Parser Error Message: System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'ItemTemplate' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.
Any idea what the caus eof this error is? is asp:hyperlink valid?
Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 11, 2024, 8:29 AM EDT
To resolve this issue, ensure that the
ItemTemplateis correctly defined within aTemplateFieldof theGridView. Here's an example of how to structure theTemplateFieldwith anItemTemplatecontaining anasp:HyperLinkcontrol:'>Make sure that the
ItemTemplateis enclosed within theTemplateFieldtags and that the control hierarchy is correct. Theasp:HyperLinkcontrol is valid for creating hyperlinks in ASP.NET.fergus72 reputationPosted Apr 11, 2024, 9:15 AM EDT
ok great i have taken your advice and cleaned it up and no more error messages - but i am not getting the rows to print on screen,
i toggled between AutoGenerateColumns="true" and false but i still cant get data to show up. i can see the grid ok in the design view but nothing shows up when i run it in the browser
Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 11, 2024, 9:24 AM EDT
Try this code
' />If you still face any error then share some more code or error details.
fergus72 reputationPosted Apr 11, 2024, 9:41 AM EDT
yes still nothing showing up. perhaps i am not doing something correctly. i will share some of my CS code:
private void BindGridView() { string userId = Session["userId"].ToString(); string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; string query = "SELECT client_ref, ck_ref, first_name, second_name, status FROM md_temp WHERE client_code = @userId"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(query, connection); command.Parameters.AddWithValue("@userId", userId); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable dt = new DataTable(); adapter.Fill(dt); if (dt.Rows.Count > 0) { GridViewMatters.DataSource = dt; GridViewMatters.DataBind(); } else { NoRecordsExist.Visible = true; } } } protected void GridViewMatters_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // Add CSS class to alternate rows for better readability if (e.Row.RowIndex % 2 == 0) { e.Row.CssClass = "evenRow"; } else { e.Row.CssClass = "oddRow"; } } }fergus72 reputationPosted Apr 11, 2024, 1:26 PM EDT
yes i can retrieve the data in a table no problem but it wont come up for gridview so i know connecrtion and data retrieval is good.
Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 12, 2024, 2:54 AM EDT
Okay, fergus
If my answer is helpful to you, then please accept the answer.