File: ProductDataSource.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.UI.WebControls;
namespace CustomControl.Samples
{
public class ProductDataSource : ObjectDataSource
{
public ProductDataSource()
{
this.TypeName = "CustomControl.Samples.ProductsComponent";
this.SelectMethod = "GetProducts";
}
}
public class ProductsComponent
{
private readonly string _conString;
public SqlDataReader GetProducts()
{
SqlConnection con = new SqlConnection(_conString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT Title,Director,DateReleased FROM Products"; // Execute command
con.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
public ProductsComponent()
{
_conString = WebConfigurationManager.ConnectionStrings["Products"]. ConnectionString;
}
}
}
File: Web.config
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
File: Default.aspx
<%@ Page Language="C#" %>
<%@ Register TagPrefix="custom" Namespace="CustomControl.Samples" %>
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
Show Product DataSource