Gridview ObjectDataSource binding with multiple tiers
Sunday, September 28th, 2008Binding a DataSource control to a gridview is a understandable way of grabbing data from your database and presenting it to the end user. However when building enterprise style applications it is often best to use a multi layer approach, usually consisting of a user layer, business logic layer, and data layer. Having a datasource directly connected and pulling data from the database does not really make as much sense in this style application especially if there is some logic that needs to be performed in the business layer before hand. This is where the value of the ObjectDataSource comes in. It allows you to use objects as your data source, providing you with the task of coding the functionality for pulling down the data.
What I will demonstrate below will be a simple use of a ObjectDataSource for simply pulling data from the database using the “select method” attribute.
Here is what our user layer source will look like.
<asp:ObjectDataSource
TypeName="UserLib.User"
SelectMethod="GetAll"
ID="odsUsers"
runat="server"
/>
<asp:GridView ID="gvUsers" DataSourceID="odsUsers" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Username">
<ItemTemplate>
<asp:Label ID="lblUsernameRow" runat="server" Text='<%# Bind("username") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblUsernameRow" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
So lets pull this apart. We define a object data source, specifying two important attributes “select method” and “typename”. The select method specifies the method which we want to call on our object to get our data. This method must return an object of IEnumerable or ICollection, so we can return a DataSet. The typename specifies the object that we will call the method on.
In the gridview we bind the datasource by specifying the “datasourceID” attribute and giving it the ID of our object datasource.
public DataSet GetAll()
{
DataSet ds = null;
try
{
UserData.User data = new UserData.User();
ds = data.GetAll();
}
catch (Exception ex)
{
throw ex;
}
return ds;
}
Above is our business logic layer. Theres really no logic here, all we are doing is calling to our data layer to grab the data from the database and load it into a dataset for us. When we receive the dataset from the data layer we will then pass this dataset back to the user layer to be binded to the gridview.
public DataSet GetAll()
{
SqlConnection conn = null;
SqlDataAdapter da = null;
SqlCommand cmd = null;
DataSet ds = null;
try
{
conn = new SqlConnection(CONN_STRING);
conn.Open();
ds = new DataSet();
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "spGetAllUsers";
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
da.Fill(ds, "users");
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (conn != null && conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
if (cmd != null)
cmd.Dispose();
if (da != null)
da.Dispose();
}
return ds;
}
Here is our data layer method which does all of our database interactions.
Today I only demonstrated the select method, but updates, deletes and inserts can also be setup and performed by the ObjectDataSource making it a very powerful and customizable way of interacting with the database.