I’m getting the following error, ever since I moved my DataTable to a different function.
Both DataSource and DataSourceID are defined on 'TestView'. Remove one definition.
By all means if you have tips on style / standards, I welcome that as well, I am very new to aspx coding. Any idea what could be causing this? It wasn’t happening before when it wasn’t its own function. Is it a local variable problem?
Here’s the code for the code that binds and is highlighted in the error:
testDAO tda = new testDAO();
DataTable testTable = new DataTable("TestView");
testTable = tda.GetTestTable();
TestView.DataSource = testTable;
TestView.DataBind();
Here’s the code for the GetTestTable():
public DataTable GetTestTable()
{
DataTable testTable = new DataTable("TestView");
testTable.Columns.Add("testdata", typeof(String));
testTable.Columns.Add("user", typeof(String));
testTable.Columns.Add("date", typeof(String));
DataRow arow = testTable.NewRow();
arow["testdata"] = "test data";
arow["user"] = "System";
arow["date"] = "";
testTable.Rows.Add(arow);
return testTable;
}
List view in aspx file:
<asp:ListView ID="TestView" runat="server">
<ItemTemplate>
<tr id="row" runat="server" class='<%# Container.DataItemIndex % 2 == 0 ? "row" : "altrow" %>'>
<td align="left">
<%# Eval("testdata") %>
</td>
<td align="center">
<%# Eval("user") %>
</td>
<td align="center">
<%# Eval("date") %>
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table class="system">
<tr>
<th>
Test Data
</th>
<th>
User
</th>
<th>
Date
</th>
</tr>
<tr id="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
</asp:ListView>
I think its due to some Naming conflicts Try renaming Either your DataTable name or ListView ID to somthing else like
TestListView. Also you have an extra line for creating a NewDataTablethats a redundency as you are assigning the table returned from methodGetTestTableEdit
If you are still having some problems then some where else you might be assigning a data source by setting DataSourceID to some data source provides, then do somthing like this:
setting DataSourceID to null will definitely solve the problem..