Thursday, March 5, 2009

Using the SqlDataSource in code

System.Web.UI.WebControls.SqlDataSource sqlDataSource = new System.Web.UI.WebControls.SqlDataSource();
sqlDataSource.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringKey"].ToString();
sqlDataSource.SelectCommand="storedProcedureName";
sqlDataSource.SelectCommandType = System.Web.UI.WebControls.SqlDataSourceCommandType.StoredProcedure;

sqlDataSource.SelectParameters.Add("ParamName", TypeCode.Int32, "1");

System.Collections.IEnumerable data = sqlDataSource.Select(new System.Web.UI.DataSourceSelectArguments());

System.Data.DataView dataView = (System.Data.DataView)data;

foreach (System.Data.DataRowView dataRow in dataView)
{
    //dataRow["ColumnName"]...
}