Complete CRUD Operations in Asp.Net C# with SQL | CRUD in Asp.Net C#
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.Configuration;
using System.Data;
using System.Drawing;
namespace CRUDApp
{
public partial class _Default : Page
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetEmployeeList();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(cs);
int empid = int.Parse(TextBox1.Text);
string empname = TextBox2.Text;
string city = DropDownList1.SelectedValue;
string sex = RadioButtonList1.SelectedValue;
string contact = TextBox6.Text;
double age = double.Parse(TextBox3.Text);
DateTime jdate = DateTime.Parse(TextBox5.Text);
con.Open();
SqlCommand command = new SqlCommand("Insert into EmployeeSetup_Tab values ('" + empid + "','" + empname + "','" + city + "','" + age + "','" + sex + "','" + jdate + "','" + contact + "')", con);
//SqlCommand command = new SqlCommand("Insert into EmployeeSetup_Tab values ('102','Rohan Shah','Ahmedabad','54','Male','5/8/2021','8966541123')", con);
command.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully saved');", true);
GetEmployeeList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
void GetEmployeeList()
{
SqlConnection con = new SqlConnection(cs);
SqlCommand command = new SqlCommand("select * from EmployeeSetup_Tab", con);
SqlDataAdapter sda = new SqlDataAdapter(command);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
int empid = int.Parse(TextBox1.Text);
string empname = TextBox2.Text;
string city = DropDownList1.SelectedValue;
string sex = RadioButtonList1.SelectedValue;
string contact = TextBox6.Text;
double age = double.Parse(TextBox3.Text);
DateTime jdate = DateTime.Parse(TextBox5.Text);
con.Open();
SqlCommand command = new SqlCommand("update EmployeeSetup_Tab set EmpName = '" + empname + "', City = '" + city + "', Age = '" + age + "', Sex = '" + sex + "',JoiningDate = '" + jdate + "', Contact = '" + contact + "' where EmpID = '"+ empid +"'", con);
command.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Updated');", true);
GetEmployeeList();
}
protected void Button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
int empid = int.Parse(TextBox1.Text);
con.Open();
SqlCommand command = new SqlCommand("delete from EmployeeSetup_Tab where EmpID = '" + empid + "'", con);
command.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Deleted');", true);
GetEmployeeList();
}
protected void Button4_Click(object sender, EventArgs e)
{
int empid = int.Parse(TextBox1.Text);
SqlConnection con = new SqlConnection(cs);
SqlCommand command = new SqlCommand("select * from EmployeeSetup_Tab where EmpID = '"+empid+"'", con);
SqlDataAdapter sda = new SqlDataAdapter(command);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button5_Click(object sender, EventArgs e)
{
GetEmployeeList();
}
}
}
Above File is CRUDApp\Default.aspx.cs
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CRUDApp._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div style="padding:15px">
<table class="w-100">
<tr>
<td colspan="2" style="font-size: x-large; font-weight: bold; color: #333333">Complete CRUD with ASP.NET with SQL</td>
</tr>
<tr>
<td style="height: 22px; width: 313px"></td>
<td style="height: 22px"></td>
</tr>
<tr>
<td style="width: 313px">
Employee ID</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Font-Size="Medium" Width="200px"></asp:TextBox>
<asp:Button ID="Button4" runat="server" Text="GET" Width="100px" Font-Bold="True" ForeColor="White" BackColor="#333333" OnClick="Button4_Click"/>
</td>
</tr>
<tr>
<td style="width: 313px">
<asp:Label ID="Label2" runat="server" Font-Size="Medium" Text="Employee Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Font-Size="Medium" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 313px">
<asp:Label ID="Label3" runat="server" Font-Size="Medium" Text="Employee City"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" Font-Size="Medium" Width="200px">
<asp:ListItem>Chicago</asp:ListItem>
<asp:ListItem>New York</asp:ListItem>
<asp:ListItem>Los Angeles</asp:ListItem>
<asp:ListItem>Seattle</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 313px">
<asp:Label ID="Label4" runat="server" Font-Size="Medium" Text="Employee Age"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" Font-Size="Medium" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 313px">
<asp:Label ID="Label5" runat="server" Font-Size="Medium" Text="Employee Sex"></asp:Label>
</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td style="width: 313px">
<asp:Label ID="Label6" runat="server" Font-Size="Medium" Text="Joining Date"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server" Font-Size="Medium" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 313px">
<asp:Label ID="Label7" runat="server" Font-Size="Medium" Text="Contact"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox6" runat="server" Font-Size="Medium" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="Button1" runat="server" Text="Insert" Width="100px" Font-Bold="True" ForeColor="White" BackColor="#333333" OnClick="Button1_Click"/>
<asp:Button ID="Button2" runat="server" Text="Update" Width="100px" Font-Bold="True" ForeColor="White" BackColor="#333333" OnClick="Button2_Click"/>
<asp:Button ID="Button3" runat="server" Text="Delete" Width="100px" Font-Bold="True" ForeColor="White" BackColor="#333333" OnClick="Button3_Click" OnClientClick="return confirm('Are you sure to delete?');"/>
<asp:Button ID="Button5" runat="server" Text="Load" Width="100px" Font-Bold="True" ForeColor="White" BackColor="#333333" OnClick="Button5_Click"/>
</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" Width="1119px">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="#666666" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
</div>
</asp:Content>
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment