What is N-Tier Logic (Presentation,Business & Data Access Layer) in C#

.................................................................DAL........................................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
public DAL()
{
//
// TODO: Add constructor logic here
//
}

 
    public int recordInsert(string name,string pwd,int age,decimal salary,int mobile, string email,string image)
    {
        try
        {
            SqlCommand cmd = new SqlCommand("sp_recordinsert",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@name",name);
            cmd.Parameters.AddWithValue("@pwz",pwd);
            cmd.Parameters.AddWithValue("@age", age);
            cmd.Parameters.AddWithValue("@salary", salary);
            cmd.Parameters.AddWithValue("@mobile", mobile);
            cmd.Parameters.AddWithValue("@email", email);
            cmd.Parameters.AddWithValue("@image",image);
            con.Open();
            return cmd.ExecuteNonQuery();
         

        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
         con.Close();
        }

    }

    public int recordUpdate(int id, string name, string pwd, int age, decimal salary, int mobile, string email,string image)
    {
        try
        {
            SqlCommand cmd=new SqlCommand("sp_recordupdate",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@name",name);
            cmd.Parameters.AddWithValue("@pwz", pwd);
            cmd.Parameters.AddWithValue("@age", age);
            cmd.Parameters.AddWithValue("@salary", salary);
            cmd.Parameters.AddWithValue("@mobile", mobile);
            cmd.Parameters.AddWithValue("@email", email);
            cmd.Parameters.AddWithValue("@id", id);
            cmd.Parameters.AddWithValue("@image",image);
            con.Open();
            return cmd.ExecuteNonQuery();
         
        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
            con.Close();
        }
    }

    public DataSet recordSelect()
    {
        try
        {
            SqlDataAdapter da=new SqlDataAdapter("sp_recordselect",con);
            DataSet ds=new DataSet();
            da.Fill(ds);
            return ds;
        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
        }
    }

    public int recordDelete(int id)
    {
     
      try
        {
            SqlCommand cmd = new SqlCommand("sp_recordDetete",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id",id);
            con.Open();
            return cmd.ExecuteNonQuery();
       

        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
            con.Close();
        }
    }

    public DataSet getImage(int id)
    {
        try
        {
         
            SqlDataAdapter da = new SqlDataAdapter("select*from userRecord where id='"+id+"'", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
             
       
        }
     
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
         
        }
    }
}
...................................................................BAL................................................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for BAL
/// </summary>
public class BAL
{
public BAL()
{
//
// TODO: Add constructor logic here
//
}
    public DataSet retriveRecord()
    {
        DAL ob = new DAL();
        try
        {
         
            return ob.recordSelect();
        }

        catch(Exception ex)
        {
            throw;
        }
        finally
        {
            ob = null;
        }
    }
    public int insertRecord_BAL(string name, string pwd, int age, decimal salary, int mobile, string email ,string image)
    {
        DAL ob = new DAL();
        try
        {
            return ob.recordInsert(name,  pwd,  age,  salary,  mobile,  email ,image);
        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
        }
    }
    public int deleteRecord(int id)
    {
        DAL ob = new DAL();
        try
        {
            return ob.recordDelete(id);
        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {

        }
    }

    public int updateRecord( int id,string name,string pwd,int  age, decimal salary,int mobile, string email,string image)
    {
        DAL ob = new DAL();
        try
        {
            return ob.recordUpdate(id,name,pwd,age,salary,mobile,email,image);
        }
        catch(Exception ex)

        {
            throw;
        }
        finally
        {
        }
    }

    public DataSet find_image(int id)
    {
        DAL ob = new DAL();
        try
        {
            return ob.getImage(id);
        }
        catch (Exception ex)
        {
            throw;
        }
        finally
        {
        }
    }
}
......................................................................UI...................................................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            showRecord();
        }
    }


    protected void showRecord()
    {
        BAL ob=new BAL();
        try
        {
            GridView1.DataSource = ob.retriveRecord();
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            throw;
        }
            finally
        {

        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        BAL ob=new BAL();
        try
        {
            int result = 0;
            int index=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            result = ob.deleteRecord(index);
            if(result>0)
            {
                Response.Write("<script>alert('Successfully deleted')</script>");
                showRecord(); ;
            }
            else
            {
                Response.Write("<script>alert('Not deleted')</script>");
            }
        }
        catch(Exception ex)
        {
            throw;

        }
        finally
        {

        }

       
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        BAL ob = new BAL();
        try
        {
            int result = 0;

            string name = TextBox1.Text;
            string pwd = TextBox2.Text;
            int age = Convert.ToInt32(TextBox4.Text);
            decimal salary = Convert.ToDecimal(TextBox5.Text);
            Int32 mobile = Convert.ToInt32(TextBox6.Text);
            string email = TextBox7.Text;
            string imagename = FileUpload1.FileName;
            string path = Server.MapPath(" ");
            string str = path + "/" + "image" + "/" + imagename;
            FileUpload1.PostedFile.SaveAs(str);
            string s = "image"+ "/" + imagename;
            result = ob.insertRecord_BAL(name, pwd, age, salary, mobile, email,s);
            if (result > 0)
            {
                Response.Write("<script>alert('Successfully saved')</script>");
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox3.Text = "";
                TextBox4.Text = "";
                TextBox5.Text = "";
                TextBox6.Text = "";
                TextBox7.Text = "";
                showRecord();

            }
            else
            {
                Response.Write("<script>alert('Not saved')</script>");
            }

        }
        catch (Exception ex)
        {
        }
        finally
        {
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

        GridView1.EditIndex = -1;
        showRecord();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //BAL ob = new BAL();
        //ImageButton lb = (ImageButton)GridView1.FindControl("ImageButton1") as ImageButton;
        //ViewState["imageName"] = lb.ImageUrl;
        GridView1.EditIndex = e.NewEditIndex;
        showRecord();
     
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.EditIndex = -1;
        showRecord();

    }
    //public void DeletePysicalFiles(int id, string fileName)
    //{


    //    System.IO.File.Delete(Request.PhysicalApplicationPath + "Upload/" + fileName);

    //}
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        BAL ob=new BAL();
        try
        {
            int result = 0;
            int index = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox8") as TextBox;
            TextBox pwd = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox9") as TextBox;
            TextBox age = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox10") as TextBox;
            TextBox salary = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox11") as TextBox;
            TextBox mobile = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox12") as TextBox;
            TextBox email = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox13") as TextBox;
            FileUpload fl = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload2") as FileUpload;
            ImageButton img=(ImageButton)GridView1.Rows[e.RowIndex].FindControl("ImageButton1") as   ImageButton;
         
            DataSet ds = new DataSet();
            ds = ob.find_image(index);
            string st=ds.Tables[0].Rows[0]["image"].ToString();

            System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath("image\\") + st);
            if (fi.Exists)
            {
                fi.Delete();
            }
            string imagename = fl.FileName;
            string path = Server.MapPath(" ");
            string str = path + "/" + "image" + "/" + imagename;
            fl.PostedFile.SaveAs(str);
            result = ob.updateRecord(index, name.Text, pwd.Text, Convert.ToInt32(age.Text), Convert.ToDecimal(salary.Text), Convert.ToInt32(mobile.Text), email.Text,fl.FileName);
            showRecord();
            if (result > 0)
            {
                Response.Redirect("Default4.aspx?id="+index);
                //Response.Write("<script>alert('Sucessfully updated')</script>");

             
            }
            else
            {
                Response.Write("<script>alert('Not updated')</script>");
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
         
        }
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridView1.PageIndex = -1;
        showRecord();
    }
}
...................................................................................................................................................................
Share:

Registration form url with JQuery Validation

http://www.webcodeexpert.com/2013/06/example-to-implement-jquery-form.html
Share:

Saturday, 30 May 2015

What is N-Tier Logic (Presentation,Business & Data Access Layer) in C#

.................................................................DAL........................................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
/// <summary>
/// Summary description for DAL
/// </summary>
public class DAL
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
public DAL()
{
//
// TODO: Add constructor logic here
//
}

 
    public int recordInsert(string name,string pwd,int age,decimal salary,int mobile, string email,string image)
    {
        try
        {
            SqlCommand cmd = new SqlCommand("sp_recordinsert",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@name",name);
            cmd.Parameters.AddWithValue("@pwz",pwd);
            cmd.Parameters.AddWithValue("@age", age);
            cmd.Parameters.AddWithValue("@salary", salary);
            cmd.Parameters.AddWithValue("@mobile", mobile);
            cmd.Parameters.AddWithValue("@email", email);
            cmd.Parameters.AddWithValue("@image",image);
            con.Open();
            return cmd.ExecuteNonQuery();
         

        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
         con.Close();
        }

    }

    public int recordUpdate(int id, string name, string pwd, int age, decimal salary, int mobile, string email,string image)
    {
        try
        {
            SqlCommand cmd=new SqlCommand("sp_recordupdate",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@name",name);
            cmd.Parameters.AddWithValue("@pwz", pwd);
            cmd.Parameters.AddWithValue("@age", age);
            cmd.Parameters.AddWithValue("@salary", salary);
            cmd.Parameters.AddWithValue("@mobile", mobile);
            cmd.Parameters.AddWithValue("@email", email);
            cmd.Parameters.AddWithValue("@id", id);
            cmd.Parameters.AddWithValue("@image",image);
            con.Open();
            return cmd.ExecuteNonQuery();
         
        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
            con.Close();
        }
    }

    public DataSet recordSelect()
    {
        try
        {
            SqlDataAdapter da=new SqlDataAdapter("sp_recordselect",con);
            DataSet ds=new DataSet();
            da.Fill(ds);
            return ds;
        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
        }
    }

    public int recordDelete(int id)
    {
     
      try
        {
            SqlCommand cmd = new SqlCommand("sp_recordDetete",con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id",id);
            con.Open();
            return cmd.ExecuteNonQuery();
       

        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
            con.Close();
        }
    }

    public DataSet getImage(int id)
    {
        try
        {
         
            SqlDataAdapter da = new SqlDataAdapter("select*from userRecord where id='"+id+"'", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
             
       
        }
     
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
         
        }
    }
}
...................................................................BAL................................................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for BAL
/// </summary>
public class BAL
{
public BAL()
{
//
// TODO: Add constructor logic here
//
}
    public DataSet retriveRecord()
    {
        DAL ob = new DAL();
        try
        {
         
            return ob.recordSelect();
        }

        catch(Exception ex)
        {
            throw;
        }
        finally
        {
            ob = null;
        }
    }
    public int insertRecord_BAL(string name, string pwd, int age, decimal salary, int mobile, string email ,string image)
    {
        DAL ob = new DAL();
        try
        {
            return ob.recordInsert(name,  pwd,  age,  salary,  mobile,  email ,image);
        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {
        }
    }
    public int deleteRecord(int id)
    {
        DAL ob = new DAL();
        try
        {
            return ob.recordDelete(id);
        }
        catch(Exception ex)
        {
            throw;
        }
        finally
        {

        }
    }

    public int updateRecord( int id,string name,string pwd,int  age, decimal salary,int mobile, string email,string image)
    {
        DAL ob = new DAL();
        try
        {
            return ob.recordUpdate(id,name,pwd,age,salary,mobile,email,image);
        }
        catch(Exception ex)

        {
            throw;
        }
        finally
        {
        }
    }

    public DataSet find_image(int id)
    {
        DAL ob = new DAL();
        try
        {
            return ob.getImage(id);
        }
        catch (Exception ex)
        {
            throw;
        }
        finally
        {
        }
    }
}
......................................................................UI...................................................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            showRecord();
        }
    }


    protected void showRecord()
    {
        BAL ob=new BAL();
        try
        {
            GridView1.DataSource = ob.retriveRecord();
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            throw;
        }
            finally
        {

        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        BAL ob=new BAL();
        try
        {
            int result = 0;
            int index=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            result = ob.deleteRecord(index);
            if(result>0)
            {
                Response.Write("<script>alert('Successfully deleted')</script>");
                showRecord(); ;
            }
            else
            {
                Response.Write("<script>alert('Not deleted')</script>");
            }
        }
        catch(Exception ex)
        {
            throw;

        }
        finally
        {

        }

       
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        BAL ob = new BAL();
        try
        {
            int result = 0;

            string name = TextBox1.Text;
            string pwd = TextBox2.Text;
            int age = Convert.ToInt32(TextBox4.Text);
            decimal salary = Convert.ToDecimal(TextBox5.Text);
            Int32 mobile = Convert.ToInt32(TextBox6.Text);
            string email = TextBox7.Text;
            string imagename = FileUpload1.FileName;
            string path = Server.MapPath(" ");
            string str = path + "/" + "image" + "/" + imagename;
            FileUpload1.PostedFile.SaveAs(str);
            string s = "image"+ "/" + imagename;
            result = ob.insertRecord_BAL(name, pwd, age, salary, mobile, email,s);
            if (result > 0)
            {
                Response.Write("<script>alert('Successfully saved')</script>");
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox3.Text = "";
                TextBox4.Text = "";
                TextBox5.Text = "";
                TextBox6.Text = "";
                TextBox7.Text = "";
                showRecord();

            }
            else
            {
                Response.Write("<script>alert('Not saved')</script>");
            }

        }
        catch (Exception ex)
        {
        }
        finally
        {
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

        GridView1.EditIndex = -1;
        showRecord();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //BAL ob = new BAL();
        //ImageButton lb = (ImageButton)GridView1.FindControl("ImageButton1") as ImageButton;
        //ViewState["imageName"] = lb.ImageUrl;
        GridView1.EditIndex = e.NewEditIndex;
        showRecord();
     
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.EditIndex = -1;
        showRecord();

    }
    //public void DeletePysicalFiles(int id, string fileName)
    //{


    //    System.IO.File.Delete(Request.PhysicalApplicationPath + "Upload/" + fileName);

    //}
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        BAL ob=new BAL();
        try
        {
            int result = 0;
            int index = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox8") as TextBox;
            TextBox pwd = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox9") as TextBox;
            TextBox age = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox10") as TextBox;
            TextBox salary = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox11") as TextBox;
            TextBox mobile = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox12") as TextBox;
            TextBox email = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox13") as TextBox;
            FileUpload fl = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload2") as FileUpload;
            ImageButton img=(ImageButton)GridView1.Rows[e.RowIndex].FindControl("ImageButton1") as   ImageButton;
         
            DataSet ds = new DataSet();
            ds = ob.find_image(index);
            string st=ds.Tables[0].Rows[0]["image"].ToString();

            System.IO.FileInfo fi = new System.IO.FileInfo(Server.MapPath("image\\") + st);
            if (fi.Exists)
            {
                fi.Delete();
            }
            string imagename = fl.FileName;
            string path = Server.MapPath(" ");
            string str = path + "/" + "image" + "/" + imagename;
            fl.PostedFile.SaveAs(str);
            result = ob.updateRecord(index, name.Text, pwd.Text, Convert.ToInt32(age.Text), Convert.ToDecimal(salary.Text), Convert.ToInt32(mobile.Text), email.Text,fl.FileName);
            showRecord();
            if (result > 0)
            {
                Response.Redirect("Default4.aspx?id="+index);
                //Response.Write("<script>alert('Sucessfully updated')</script>");

             
            }
            else
            {
                Response.Write("<script>alert('Not updated')</script>");
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
         
        }
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridView1.PageIndex = -1;
        showRecord();
    }
}
...................................................................................................................................................................

Thursday, 28 May 2015

Popular

Total Pageviews

Archive