CRUD Operation using Connected Oriented Model

Design
CRUD OPERATION

using System;
using System.Data.SqlClient;

public partial class frmTest : System.Web.UI.Page
{
  
    private void ShowEmp()
    {
        SqlConnection con = new SqlConnection(@"Data Source=Utpal;Initial Catalog=Utpal_DB;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("Select * from tblEmployee", con);
        con.Open();
        SqlDataReader sdr = cmd.ExecuteReader();
        //con.Close();
        if (sdr.HasRows)
        {
            gridEmp.DataSource = sdr;
            gridEmp.DataBind();
        }
        else {
            Response.Write("<script>alert('Sorry no record to display ....')</script>");
        }
        con.Close();
    }
    protected void btnShow_Click(object sender, EventArgs e)
    {
        ShowEmp();
    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=Utpal;Initial Catalog=Utpal_DB;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("insert into tblEmployee(EmpName,EmpSalary,EmpDoj)values('"+txtEmpName.Text+"',"
            +Convert.ToDecimal(txtEmpSalary.Text)+",'"+txtEmpDoj.Text+"')",con);
        con.Open();
        int temp= cmd.ExecuteNonQuery();
        con.Close();
        if (temp == 1)
        {
            ShowEmp();
        }
        else {
            Response.Write("<script>alert('Sorry Record not inserted')</script>");
        }
   }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=Utpal;Initial Catalog=Utpal_DB;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("update tblEmployee set EmpName = '" + txtEmpName.Text + "', EmpSalary= "
            + Convert.ToDecimal(txtEmpSalary.Text) + " , EmpDoj= '" + txtEmpDoj.Text + "' where pkEmpId= " + txtEmpId.Text+" ", con);
        con.Open();
        int temp=cmd.ExecuteNonQuery();
        con.Close();     
        if (temp == 1)
        {
            ShowEmp();
        }
        else
        {
            Response.Write("<script>alert('Sorry Record not inserted')</script>");
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=Utpal;Initial Catalog=Utpal_DB;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("delete from tblEmployee where pkEmpId= " + txtEmpId.Text + " ", con);
        con.Open();
        int temp=cmd.ExecuteNonQuery();
        con.Close();
        //ShowEmp();
        if (temp == 1)
        {
            ShowEmp();
        }
        else
        {
            Response.Write("<script>alert('Sorry Record not inserted')</script>");
        }
    }
}

No comments:

Post a Comment

Xamarin Android Project App

Xamarin Android Project App 1. Xamarin -- Make a CALCULATOR Android App   https://drive.google.com/open?id=0B8OPCXLrtKPmWC1FWWtFM2lraVk...