Reputation:
I've got a Datatable with a Sql Connection and am trying to bind that to a GridView, however I get the error that the type specified in the TypeName property could not be found. My datatable code looks like this :
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "TestConnectionString";
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = "SELECT* FROM TestTable";
DataTable data = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(data);
GridView1.DataSource = data;
}
and the code in the .aspx :
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" >
<Columns>
<asp:BoundField DataField="Column1" HeaderText="Column1" ReadOnly="True" SortExpression="Column1" >
<HeaderStyle Width="450px" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Column2" HeaderText="Column2" ReadOnly="True" SortExpression="Column2" >
<ItemStyle Width="400px" HorizontalAlign="Center" />
</asp:BoundField>
</Column>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="DataTable" TypeName="data"></asp:ObjectDataSource>
Suggestions on what I've done wrong?
Upvotes: 1
Views: 2559
Reputation: 31198
That's not how the ObjectDataSource
control is meant to be used.
If you just want to bind the grid on the first page load, then remove the ObjectDataSource
control, remove the ObjectDataSource1_Selecting
event handler, remove the DataSourceID
property from the GridView
, and just bind the grid when the page loads:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
using (SqlConnection conn = new SqlConnection("your connection string here"))
using (SqlCommand command = new SqlCommand("SELECT * FROM TestTable", conn))
{
DataTable data = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(data);
GridView1.DataSource = data;
GridView1.DataBind();
}
}
If you want to use the ObjectDataSource
control, then give it a valid TypeName
and SelectMethod
, remove the ObjectDataSource1_Selecting
event handler, and have the SelectMethod
return the data:
public static class YourDataSource
{
public static DataTable LoadSomeData()
{
using (SqlConnection conn = new SqlConnection("your connection string here"))
using (SqlCommand command = new SqlCommand("SELECT * FROM TestTable", conn))
{
DataTable data = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(data);
return data;
}
}
}
Markup:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="YourDataSource"
SelectMethod="LoadSomeData"
/>
If you're using .NET 4.5, you could also use "model binding" to load the data from the code-behind without using a data-source control:
public DataTable LoadSomeData()
{
using (SqlConnection conn = new SqlConnection("your connection string here"))
using (SqlCommand command = new SqlCommand("SELECT * FROM TestTable", conn))
{
DataTable data = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(data);
return data;
}
}
Markup:
<asp:GridView ID="GridView1" runat="server"
AllowSorting="True"
AutoGenerateColumns="False"
SelectMethod="LoadSomeData"
>
<Columns>
...
</Columns>
</asp:GridView>
Upvotes: 2