CRUD opearations basic Example on ado.net in asp.net C#
Employee.cs
......................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CRUD_Operations
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
}
}
EmployeeDb.CS
......................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace CRUD_Operations
{
public class EmploeeDb
{
public List<Employee> Data { get; set; }
public int Insert(Employee emp)
{
SqlConnection con = new SqlConnection("data source=192.168.200.77;database=Employee;user id=sa;password=indi123");
con.Open();
SqlCommand cmd = new SqlCommand("insert into employe values ("+emp.Id+",'"+emp.Name+"','"+emp.Location+"')",con);
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
public DataTable Details()
{
SqlConnection con = new SqlConnection("data source=192.168.200.77;database=Employee;user id=sa;password=indi123");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Employe", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public int Update(Employee emp)
{
SqlConnection con = new SqlConnection("data source=192.168.200.77;database=Employee;user id=sa;password=indi123");
con.Open();
SqlCommand cmd = new SqlCommand("Update Employe set Name='"+emp.Name+"',Location='"+emp.Location+"' where Id="+emp.Id+"", con);
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
public int Delete(Employee emp)
{
SqlConnection con = new SqlConnection("data source=192.168.200.77;database=Employee;user id=sa;password=indi123");
con.Open();
SqlCommand cmd = new SqlCommand("delete from employe where Id=" + emp.Id + "", con);
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
}
}
Home.Aspx
.................................
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="CRUD_Operations.Home" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scm" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="my" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblId" runat="server" Text="Id"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblLocation" runat="server" Text="Location"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtLocation" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</td>
<td>
<asp:Button ID="btnDispaly" runat="server" Text="Display" OnClick="btnDispaly_Click" />
</td>
<td>
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
</td>
<td>
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
</td>
</tr>
</table>
<asp:GridView ID="gvData" runat="server"></asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
......................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CRUD_Operations
{
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
}
}
EmployeeDb.CS
......................................................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace CRUD_Operations
{
public class EmploeeDb
{
public List<Employee> Data { get; set; }
public int Insert(Employee emp)
{
SqlConnection con = new SqlConnection("data source=192.168.200.77;database=Employee;user id=sa;password=indi123");
con.Open();
SqlCommand cmd = new SqlCommand("insert into employe values ("+emp.Id+",'"+emp.Name+"','"+emp.Location+"')",con);
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
public DataTable Details()
{
SqlConnection con = new SqlConnection("data source=192.168.200.77;database=Employee;user id=sa;password=indi123");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Employe", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
public int Update(Employee emp)
{
SqlConnection con = new SqlConnection("data source=192.168.200.77;database=Employee;user id=sa;password=indi123");
con.Open();
SqlCommand cmd = new SqlCommand("Update Employe set Name='"+emp.Name+"',Location='"+emp.Location+"' where Id="+emp.Id+"", con);
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
public int Delete(Employee emp)
{
SqlConnection con = new SqlConnection("data source=192.168.200.77;database=Employee;user id=sa;password=indi123");
con.Open();
SqlCommand cmd = new SqlCommand("delete from employe where Id=" + emp.Id + "", con);
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
}
}
Home.Aspx
.................................
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="CRUD_Operations.Home" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scm" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="my" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblId" runat="server" Text="Id"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblLocation" runat="server" Text="Location"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtLocation" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</td>
<td>
<asp:Button ID="btnDispaly" runat="server" Text="Display" OnClick="btnDispaly_Click" />
</td>
<td>
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />
</td>
<td>
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
</td>
</tr>
</table>
<asp:GridView ID="gvData" runat="server"></asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Home.Aspx.CS
.................................
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CRUD_Operations
{
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
Employee obj = new Employee();
obj.Id = int.Parse(txtId.Text.Trim());
obj.Name = txtName.Text.Trim();
obj.Location = txtLocation.Text.Trim();
EmploeeDb objpractice = new EmploeeDb();
int i = objpractice.Insert(obj);
if(i>0)
{
txtId.Text = txtName.Text = txtLocation.Text = string.Empty;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "Employee Id=" + txtId.Text + " Details Saved successfully", true);
}
else
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "Failed Details", true);
}
}
protected void btnDispaly_Click(object sender, EventArgs e)
{
EmploeeDb objpractice = new EmploeeDb();
DataTable i = objpractice.Details();
gvData.DataSource = i;
gvData.DataBind();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
Employee obj = new Employee();
obj.Id = int.Parse(txtId.Text.Trim());
obj.Name = txtName.Text.Trim();
obj.Location = txtLocation.Text.Trim();
EmploeeDb objpractice = new EmploeeDb();
int i = objpractice.Update(obj);
if (i > 0)
{
txtId.Text = txtName.Text = txtLocation.Text = string.Empty;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "Employee Id=" + txtId.Text + " Details Saved successfully", true);
}
else
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "Failed Details", true);
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
Employee obj = new Employee();
obj.Id = int.Parse(txtId.Text.Trim());
obj.Name = txtName.Text.Trim();
obj.Location = txtLocation.Text.Trim();
EmploeeDb objpractice = new EmploeeDb();
int i = objpractice.Delete(obj);
if (i > 0)
{
txtId.Text = txtName.Text = txtLocation.Text = string.Empty;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "Employee Id=" + txtId.Text + " Details deleted successfully", true);
}
else
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "deletion failed", true);
}
}
}
}
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CRUD_Operations
{
public partial class Home : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
Employee obj = new Employee();
obj.Id = int.Parse(txtId.Text.Trim());
obj.Name = txtName.Text.Trim();
obj.Location = txtLocation.Text.Trim();
EmploeeDb objpractice = new EmploeeDb();
int i = objpractice.Insert(obj);
if(i>0)
{
txtId.Text = txtName.Text = txtLocation.Text = string.Empty;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "Employee Id=" + txtId.Text + " Details Saved successfully", true);
}
else
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "Failed Details", true);
}
}
protected void btnDispaly_Click(object sender, EventArgs e)
{
EmploeeDb objpractice = new EmploeeDb();
DataTable i = objpractice.Details();
gvData.DataSource = i;
gvData.DataBind();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
Employee obj = new Employee();
obj.Id = int.Parse(txtId.Text.Trim());
obj.Name = txtName.Text.Trim();
obj.Location = txtLocation.Text.Trim();
EmploeeDb objpractice = new EmploeeDb();
int i = objpractice.Update(obj);
if (i > 0)
{
txtId.Text = txtName.Text = txtLocation.Text = string.Empty;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "Employee Id=" + txtId.Text + " Details Saved successfully", true);
}
else
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "Failed Details", true);
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
Employee obj = new Employee();
obj.Id = int.Parse(txtId.Text.Trim());
obj.Name = txtName.Text.Trim();
obj.Location = txtLocation.Text.Trim();
EmploeeDb objpractice = new EmploeeDb();
int i = objpractice.Delete(obj);
if (i > 0)
{
txtId.Text = txtName.Text = txtLocation.Text = string.Empty;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "Employee Id=" + txtId.Text + " Details deleted successfully", true);
}
else
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Script", "deletion failed", true);
}
}
}
}
Comments
Post a Comment