Explanation:
The TypeName of ObjectDataSource specifies the Business Logic for the ObjectDataSource. Here we will define all the desired methods. Which we will later implement in our Data Presentation Control.
using System;
using System.Configuration;
using System.Web;
using System.Diagnostics;
public class AuthorsDB
{
public AuthorsDB() { }
public static System.Data.DataSet GetAuthorsByState(string state)
{
string connectionString = ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(connectionString);
string queryString;
if (state != null)
queryString = "SELECT au_id, au_fname, au_lname, state FROM [authors] WHERE ([authors].[state] = @state)";
else
queryString = "SELECT au_id, au_fname, au_lname, state FROM [authors]";
System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
System.Data.IDataParameter dbParam_state = new System.Data.SqlClient.SqlParameter();
if (state != null)
{
dbParam_state.ParameterName = "@state";
dbParam_state.Value = state;
dbParam_state.DbType = System.Data.DbType.StringFixedLength;
dbCommand.Parameters.Add(dbParam_state);
}
System.Data.IDbDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter();
dataAdapter.SelectCommand = dbCommand;
System.Data.DataSet dataSet = new System.Data.DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
}
public static System.Data.DataSet GetStates()
{
string connectionString = ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(connectionString);
string queryString = "SELECT DISTINCT [authors].state FROM [authors]";
System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
System.Data.IDbDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter();
dataAdapter.SelectCommand = dbCommand;
System.Data.DataSet dataSet = new System.Data.DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
}
public static int UpdateAuthor (string au_id, string au_lname, string au_fname, string state)
{
string connectionString = ConfigurationManager.ConnectionStrings["Pubs"].ConnectionString;
System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(connectionString);
string queryString = "UPDATE [authors] SET [au_lname]=@au_lname, [au_fname]=@au_fname, [state]=@state WHERE ([authors].[au_id] = @au_id)";
System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
System.Data.IDataParameter dbParam_au_id = new System.Data.SqlClient.SqlParameter();
dbParam_au_id.ParameterName = "@au_id";
dbParam_au_id.Value = au_id;
dbParam_au_id.DbType = System.Data.DbType.String;
dbCommand.Parameters.Add(dbParam_au_id);
System.Data.IDataParameter dbParam_au_lname = new System.Data.SqlClient.SqlParameter();
dbParam_au_lname.ParameterName = "@au_lname";
dbParam_au_lname.Value = au_lname;
dbParam_au_lname.DbType = System.Data.DbType.String;
dbCommand.Parameters.Add(dbParam_au_lname);
System.Data.IDataParameter dbParam_au_fname = new System.Data.SqlClient.SqlParameter();
dbParam_au_fname.ParameterName = "@au_fname";
dbParam_au_fname.Value = au_fname;
dbParam_au_fname.DbType = System.Data.DbType.String;
dbCommand.Parameters.Add(dbParam_au_fname);
System.Data.IDataParameter dbParam_state = new System.Data.SqlClient.SqlParameter();
dbParam_state.ParameterName = "@state";
dbParam_state.Value = state;
dbParam_state.DbType = System.Data.DbType.StringFixedLength;
dbCommand.Parameters.Add(dbParam_state);
int rowsAffected = 0;
dbConnection.Open();
try
{
rowsAffected = dbCommand.ExecuteNonQuery();
}
finally
{
dbConnection.Close();
}
return rowsAffected;
}
}
Now, see how to access the Business layer in Data psentation Control (GridView in this particular example).
<%@ Page Language="C#" Debug="true" %>
<html>
<head runat="server">
<title>GridView Bound to Data Access Layertitle>
head>
<body>
<form id="form1" runat="server">
AutoPostBack="True" DataTextField="state" DataValueField="state" />
SelectMethod="GetStates"/>
<br />
<br />
AllowPaging="True" AllowSorting="True" DataKeyNames="au_id">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField HeaderText="ID" DataField="au_id" SortExpssion="au_id" ReadOnly="true" />
<asp:BoundField HeaderText="Name" DataField="au_fname" SortExpssion="au_fname" />
<asp:BoundField HeaderText="LastName" DataField="au_lname" SortExpssion="au_lname" />
<asp:BoundField HeaderText="State" DataField="state" SortExpssion="state" />
Columns>
asp:GridView>
SelectMethod="GetAuthorsByState" UpdateMethod="UpdateAuthor" OldValuesParameterFormatString="{0}">
<SelectParameters>
<asp:ControlParameter Name="state" PropertyName="SelectedValue" ControlID="DropDownList1"/>
SelectParameters>
asp:ObjectDataSource>
form>
body>
html>
|