CRUD Operation
Design
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;
public partial class CRUD_Operation : System.Web.UI.Page
{
protected void Create_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
con.ConnectionString = @"Data Source=DESKTOP-5H5O8UP\SQLEXPRESS1;Initial Catalog=BusBookingDB;Integrated Security=True";
con.Open();
cmd.Connection = con;
cmd.CommandText = "insert into TblCustomer values(" + txtCustomerID.Text + ",'" + txtCustomerName.Text + "','" + txtAddress.Text + "','" + txtEmail.Text + "','" + txtContact.Text + "')";
cmd.ExecuteNonQuery();
Response.Write("Record inserted");
}
protected void Delete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
con.ConnectionString = @"Data Source=DESKTOP-5H5O8UP\SQLEXPRESS1;Initial Catalog=BusBookingDB;Integrated Security=True";
con.Open();
cmd.Connection = con;
cmd.CommandText = "Delete From TblCustomer Where CustomerID =" + txtCustomerID.Text + "";
cmd.ExecuteNonQuery();
Response.Write("Record Deleted");
}
protected void Search_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
con.ConnectionString = @"Data Source=DESKTOP-5H5O8UP\SQLEXPRESS1;Initial Catalog=BusBookingDB;Integrated Security=True";
con.Open();
cmd.Connection = con;
cmd.CommandText = "Select * from TblCustomer where CustomerID=" + txtCustomerID.Text + "";
cmd.ExecuteNonQuery();
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{
txtCustomerID.Text = dr[0].ToString();
txtCustomerName.Text = dr[1].ToString();
txtAddress.Text = dr[2].ToString();
txtEmail.Text = dr[3].ToString();
txtContact.Text = dr[4].ToString();
}
Response.Write("Record Search");
}
protected void Update_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
con.ConnectionString = @"Data Source=DESKTOP-5H5O8UP\SQLEXPRESS1;Initial Catalog=BusBookingDB;Integrated Security=True";
con.Open();
cmd.Connection = con;
cmd.CommandText = "Update TblCustomer Set CustomerName = '" + txtCustomerName.Text +"' Where CustomerID =" + txtCustomerID.Text + "";
cmd.ExecuteNonQuery();
Response.Write("Record Updated");
}
}
Output