Crud Operations in asp.net

Advertisement
In this article I will show you how to Insert, update,Delete,Retrieve,Clear in asp.net.

Craete Sql like this

USE [Emp]
GO


SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[EmpDetails](
 [EmpId] [nchar](10) NOT NULL,
 [EmpName] [nvarchar](50) NOT NULL,
 [EmpDesignation] [nvarchar](50) NOT NULL,
 [EmpAge] [nchar](10) NOT NULL,
 [EmpPhoneNo] [nchar](10) NOT NULL,
 [EmpAddress] [nvarchar](50) NOT NULL
) ON [PRIMARY]

GO




Create a project as shown in the snippet.



Design UI Screen

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CRUD.ascx.cs" Inherits="CRUDFORM.UserControl.CRUD" %>



EmpId :
EmpName:
EmpDesignation:
EmpAge:
EmpPhoneNo:
EmpAddress:


Create BO.cs as Shown Below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CRUDFORM.BO
{
    public class Bo
    {
        string _EmpId; 
        string _EmpName;
        string _EmpDesignation;
        string _EmpAge;

        string _EmpPhoneNo;
        string _EmpAddress;

        public string EmpId
        {
            get
            {
                return _EmpId;
            }

            set
            {

                _EmpId = value;
            }


       

        }


        public string EmpName
        {
            get
            {
                return _EmpName;
            }

            set
            {

                _EmpName = value;
            }




        }




        public string EmpDesignation
        {
            get
            {
                return _EmpDesignation;
            }

            set
            {

                _EmpDesignation = value;
            }

        }

        public string EmpAge
        {
            get
            {
                return _EmpAge;
            }

            set
            {

                _EmpAge = value;
            }

        }

        public string EmpPhoneNo
        {
            get
            {
                return _EmpPhoneNo;
            }

            set
            {

                _EmpPhoneNo = value;
            }

        }

        public string EmpAddress
        {
            get
            {
                return _EmpAddress;
            }

            set
            {

                _EmpAddress = value;
            }

        }
    }
}


Create CRUD.ascx.cs as Shown Below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;
using System.Configuration;

 


namespace CRUDFORM.UserControl
{
    public partial class CRUD : System.Web.UI.UserControl
    {

        SqlConnection con;
        SqlCommand cmd;

   
        
     
      
        public void Page_Load(object sender, EventArgs e)
        {

           

        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {


            con = new SqlConnection("Data Source= ABC; Integrated Security=true;Initial Catalog= Emp;User ID=sa; Password=Password1234;");
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            
            cmd.CommandText = "INSERT INTO EmpDetails VALUES('" + txtEmpId.Text + "', '" + txtEmpName.Text + "','" + txtEmpDesignation.Text + "','" + txtEmpAge.Text + "','" + txtEmpPhoneNo.Text + "','"+ txtEmpAddress.Text +"')";
          
            cmd.Connection = con;

            try
            {
                con.Open();

                cmd.ExecuteNonQuery();

            }

            catch (Exception ex)
            {
                throw ex;

            }

            finally
            {



                con.Close();

            }
           

        

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {



            con = new SqlConnection("Data Source= ABC; Integrated Security=true;Initial Catalog= Emp;User ID=sa; Password=Password1234;");
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;

            cmd.CommandText = "UPDATE EmpDetails SET EmpName = '" + txtEmpName.Text + "',EmpDesignation = '" + txtEmpDesignation.Text + "',EmpAge = '" + txtEmpAge.Text + "',EmpPhoneNo = '" + txtEmpPhoneNo.Text + "',EmpAddress = '" + txtEmpAddress.Text + "' WHERE EmpId = '" + txtEmpId.Text + "'";
        
            cmd.Connection = con;

            try
            {
                con.Open();
           
                cmd.ExecuteNonQuery();
               

            }

            catch (Exception ex)
            {
                throw ex;

            }

            finally
            {



                con.Close();

            }
           

        

        }

        protected void BtnDelete_Click(object sender, EventArgs e)
        {

            con = new SqlConnection("Data Source= ABC; Integrated Security=true;Initial Catalog= Emp;User ID=sa; Password=Password1234;");
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;

            cmd.CommandText = "DELETE FROM EmpDetails WHERE EmpId = '" + txtEmpId.Text + "'";

            cmd.Connection = con;

            try
            {
                con.Open();

                cmd.ExecuteNonQuery();


            }

            catch (Exception ex)
            {
                throw ex;

            }

            finally
            {



                con.Close();

            }
        }

        protected void BtnRetrive_Click(object sender, EventArgs e)
        {
            string str;
            con = new SqlConnection("Data Source= ABC; Integrated Security=true;Initial Catalog= Emp;User ID=sa; Password=Password1234;");
            con.Open();


            str = "select * from EmpDetails where EmpId='" +txtEmpId.Text.Trim() + "'";

            cmd = new SqlCommand(str,con);

            SqlDataReader reader = cmd.ExecuteReader();

              if (reader.Read())
  {
  txtEmpName.Text=reader["EmpName"].ToString();
  txtEmpDesignation.Text = reader["EmpDesignation"].ToString();
  txtEmpAge.Text = reader["EmpAge"].ToString();
  txtEmpPhoneNo.Text = reader["EmpPhoneNo"].ToString();
  txtEmpAddress.Text = reader["EmpAddress"].ToString();

  reader.Close();
  con.Close();
  }
  }

        protected void BtnClear_Click(object sender, EventArgs e)
        {
            txtEmpId.Text = "";
            txtEmpName.Text = "";
            txtEmpDesignation.Text = "";

            txtEmpAge.Text = "";

            txtEmpPhoneNo.Text = "";
            txtEmpAddress.Text = "";
        }
           


        }

        }





write Connecting string code in Web.config

    
  

OutPut
Examples:
Insert,Update,Delete,Retrieve Using Stored Procedure
Advertisements
SHARE

Unknown

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

1 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.

    ReplyDelete