Consider a simple scenario where we are going to add just course names into the database using three layered architecture.
First of all create a table in the database with columns
CourseID bigint, (Primary key, auto increment)Now open visual studio 2008 and create a new website by:
CourseDesc Varchar(100)
File-->New-->Web SiteAfter that create the connection with the database by adding the connection string in the web.config file as follows:
Expand|Select|Wrap|Line Numbers
- <appSettings>
- <add key="ConnString" value="server=MUDASSIR;database=dbCourse;Integrated Security=true;"></add>
- </appSettings>
- <connectionStrings>
- <remove name="LocalSqlServer" />
- <remove name="LocalMySqlServer" />
- </connectionStrings>
Expand|Select|Wrap|Line Numbers
- <html>
- <body>
- <table>
- <tr>
- <td>
- <asp:Label ID="lblName" Text="Course Name" runat="server"></asp:Label>
- </td>
- <td>
- <asp:TextBox ID="txtName" runat="server" BorderStyle="Solid" BorderWidth="1px" ></asp:TextBox>
- <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtName" ErrorMessage="*" SetFocusOnError="true"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td colspan="2" height="50px"></td>
- </tr>
- <tr>
- <td colspan="2">
- <asp:Button ID="btnAdd" Text="Add Course" runat="server" CausesValidation="true" OnClick="btnAdd_Click" />
- <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="false" OnClick="btnCancel_Click" />
- </td>
- </tr>
- </table>
- </body>
- </html>
Right click on the App_Code in the Solution explorer and add 2 new folders named
DAL
and BAL
. The DAL folder will contain the classes that will be interacting with the database directly and the BAL folder will contain the classes that will work as bridge between the DAL and the Presentation layer(front end).In the App_Code, right click on DAL and select Add New Item and Select Class. Name it
CoursesTransaction.cs
:This Class will look like this:
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Data;
- using System.Configuration;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Data.SqlClient;
- /// <summary>
- /// Summary description for CoursesTransaction
- /// </summary>
- public class CoursesTransaction
- {
- #region variables
- public Int64 CourseID { get; set; }
- public string CourseDesc { get; set; }
- #endregion
- public CoursesTransaction()
- {
- //
- // TODO: Add constructor logic here
- //
- }
- public void InsertCourse()
- {
- SqlConnection Conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
- SqlCommand cmd = new SqlCommand("[dbo].[SP_CoursesInsertUpdate]", Conn);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@CourseDesc", CourseDesc);
- try
- {
- Conn.Open();
- cmd.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- finally {
- Conn.Close();
- }
- }
- }
Right click on the BAL folder and select Add New Item and Add Class. Name it
CoursesTransactionBAL.cs
.This class will be like this:
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Data;
- using System.Configuration;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- /// <summary>
- /// Summary description for CoursesTransactionBAL
- /// </summary>
- public class CoursesTransactionBAL
- {
- #region variables
- public Int64 CourseID { get; set; }
- public string CourseDesc { get; set; }
- #endregion
- CoursesTransaction obj = new CoursesTransaction();
- DataTable dt = new DataTable();
- public CoursesTransactionBAL()
- {
- //
- // TODO: Add constructor logic here
- //
- }
- public void Insert()
- {
- obj.CourseDesc = CourseDesc;
- obj.InsertCourse();
- }
- }
Switch to code behind file of the AddCourse.aspx
and create an object of the Class like this:
Expand|Select|Wrap|Line Numbers
- CoursesTransactionBAL obj = new CoursesTransactionBAL();
Expand|Select|Wrap|Line Numbers
- private void Insert()
- {
- obj.CourseDesc = txtName.Text;
- obj.Insert();
- }
Expand|Select|Wrap|Line Numbers
- protected void btnAdd_Click(object sender, EventArgs e)
- {
- Insert();
- ClientScript.RegisterStartupScript(Page.GetType(), "OnLoad", "Alert();", true);
- }
Chillas
Regards:
Mudassir