Reputation: 33
I am trying to bind a ListView to a datatable in the code behind and get it to display on the aspx page. I get "An ItemTemplate must be defined in ListView ''." error even though an ItemTemplate exists within the ListView. The error is strange because it does not give the name of my Listview in the quotes. Have I not bound it correctly? I do not need the other templates as this is a readonly list. Thanks!
Source Error:
Line 61: lvOtherAccts.DataBind();
CS code:
String strConnString10 = WebConfigurationManager.ConnectionStrings["billing_webConnectionString"].ConnectionString;
SqlConnection con10 = new SqlConnection(strConnString10);
SqlCommand cmd10 = new SqlCommand("SELECT landlord_nbr, svc_addr, svc_addr2, svc_city, svc_state, svc_zip, acct_nbr, billing_date, w_bal from landlord_info where landlord_nbr='" + ll_num + "'ORDER BY acct_nbr ASC", con10);
cmd10.Parameters.Add("conn_nbr", SqlDbType.VarChar).Value = Session["LLNum"];
cmd10.Connection = con10;
SqlDataAdapter da10 = new SqlDataAdapter(cmd10);
DataTable dtLLAccts = new DataTable();
da10.Fill(dtLLAccts);
ListView lvOtherAccts = new ListView();
lvOtherAccts.DataSource = dtLLAccts;
lvOtherAccts.DataBind();
aspx:
<asp:ListView ID="lvOtherAccts" runat="server" DataSourceID="dtLLAccts" ItemPlaceholderID=
"itemPlaceHolder">
<LayoutTemplate>
<table>
<tr>
<th id="Th1" runat="server">
Account
</th>
<th id="Th2" runat="server">
Service Address
</th>
<th id="Th3" runat="server">
City
</th>
<th id="Th4" runat="server">
Last Bill Date
</th>
<th id="Th5" runat="server">
Billed Balance Due
</th>
</tr>
<tr ID="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="AcctNbr" runat="server" Text='<%#Eval("acct_nbr")%>' />
</td>
<td>
<asp:Label ID="SvcAddr" runat="server" Text='<%#Eval("svc_addr")%>' />
</td>
<td>
<asp:Label ID="SvcCity" runat="server" Text='<%#Eval("svc_city")%>' />
</td>
<td>
<asp:Label ID="BillDate" runat="server" Text='<%#Eval("billing_date")%>' />
</td>
<td style="text-align: right;">
<asp:Label ID="Balance" runat="server" Text='<%# Eval("w_bal", "{0:C2}") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Upvotes: 0
Views: 215
Reputation: 1714
In your code, you have the line:
ListView lvOtherAccts = new ListView();
Comment that out. You don't need to create a new instance of the Listview as it is already in your aspx page.
Upvotes: 1