Insertion of records in database using 3-tier architecture in asp.net with c#

In this tutorial we will discuss about how to insert records in database using 3-tier architecture in asp.net with c#. For beginners I strongly recommend to read this tutorial 3-Tier Architecture in asp.net using c# first to get basics of 3-tier architecture in asp.net. So let’s start.
3-Tier Architecture in asp.net

Insertion of records in database using 3-tier architecture in asp.net with c#

insert-records.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="insert-records.aspx.cs" Inherits="insert-records" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Insertion of records in database using 3-tier architecture in asp.net with c#</title>
</head>
<body>
<form id="form1" runat="server">
  <table width="600px" cellspacing="2" cellpadding="2" border="1" style="font-family:verdana;font-size:12px;">
    <tbody>
      <tr>
        <td colspan="2" align="left"><strong> User Feedback Form</strong></td>
      </tr>
      <tr>
        <td align="left" width="20%"> First Name </td>
        <td><asp:TextBox ID="txtFirstName" runat="server" Font-Size="Small" Width="250px"></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td align="left"> Last Name </td>
        <td align="left"><asp:TextBox ID="txtLastName" runat="server" Font-Size="Small" Width="250px"></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td> Feedback </td>
        <td><asp:TextBox ID="txtFeedback" runat="server" TextMode="MultiLine" Rows="12" Columns="50"></asp:TextBox>
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td align="left"><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"/>
        </td>
      </tr>
    </tbody>
  </table>
</form>
</body>
</html>
As you have seen in code and image that we have two textboxes, one textarea and one button in our .aspx page. User will fill the form and click the button to submit his/her feedback. At button’s click event in code behind I have written all the logic to insert records in database using 3-tier architecture in asp.net with c#. Remember I have not performed any client side or server side validation on my form fields because I want to make code simple and clear for you and just want to focus on topic, so let’s have a look over its code behind file

insert-records.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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 BusinessLayer;//Donot forget to import this namespace


public partial class insert-records : System.Web.UI.Page
{
BusFeedback _objFeedback = new BusFeedback ();
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSubmit_Click(object sender, EventArgs e)
    {
_objFeedback.InsertFeedback(txtFirstName.Text, txtLastName.Text, txtFeedback.Text);
 //If records successfully entered then redirect to feedback page
//Response.Redirect("view-feedback.aspx");
    }

}



I have declared the object of my bus BusFeedback that I am going to create after this explanation. The object that I declared is _objFeedback then on click event of asp:button I made call to the function written in BusFeedback, that will insert records in database and that’s it. I could write insertion function in BusComments that I created in my previous tutorial but for better understanding I am creating separate bus for insertion of records. I will recommend you to create one bus for one module and write all functions in it. Every module should have its own bus, it is better approach. Now let’s have a look over the bus.

For beginners I again ask you to read the basics of 3-tier architecture first by reading the tutorial 3-Tier Architecture in asp.net using c# in which I went in very detail about the benefits of 3-tier architecture, how to create business layer, how to create data access layer, how to create bus and write function in it and then how to call the function written in bus from .aspx page.

BusFeedback.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLayer;//Donot forget to import this

namespace BusinessLayer
{
public class BusFeedback
{
DbAccess _dbAccess = new DbAccess();
private DataSet _FeedbackDS = new DataSet();
public DataSet FeedbackDS
{
get
{
return _FeedbackDS;
}
set
{
_FeedbackDS = value;
}
}

  //Function to insert data in database, in this example I used MS SQL SERVER    
  public void InsertFeedback(string firstName, string lastName, string feedback)
        {

            try
            {
                string str = "select max(id) as max_id from web_tbl_feedback";
                int maxVal = (_dbAccess.returnint32(str));
                string id = Convert.ToString(maxVal);
                if (string.IsNullOrEmpty(id))
                {
                    maxVal = 1;
                }
                else
                {
                    maxVal++;
                }

                string strInsert = "insert into web_tbl_feedback(id,firstname,lastname,feedback) values('" + maxVal + "','" + firstName + "','" + lastName + "','" + feedback + "')";
                _dbAccess.executeQuery(strInsert);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

}

}

As far as DataAccessLayer namespace and its bus DbAccess.cs is concered, if you are developing new project then you will have to create business layer with its bus busFeedback, DataAccessLayer with its bus DbAccess but if you are using the same project that we developed during previous tutorial then only you have to put BusFeedback in your BusinessLayer folder and that’s it, there is no need to create any DbAccess bus again because one project has only one DbAccess bus but on other end one project may have lot of buses in BusinessLayer folder because those buses depend over modules and DbAccess bus of DataAccessLayer depends over complete project.

So that’s it. Keep in touch as very soon I will write tutorial about deletion and updation of records using 3-tier architecture in asp.net with c#.

Happy Coding!!!

0 comments: