crud operation in asp.net || crud in asp.net webforms || crud in c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EmployeeCRUDoperations.Model
{
public class Employee
{
public int Id { get; set; }
public string EmpName { get; set; }
public string Position { get; set; }
public decimal Salary { get; set; }
}
}
Above File is EmployeeCRUDoperations\Model\Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace EmployeeCRUDoperations.Model
{
public class EmployeeDAL
{
private readonly string _connectionString = ConfigurationManager.ConnectionStrings["EmpConnection"].ConnectionString;
public List<Employee> GetEmployees()
{
List<Employee> employees = new List<Employee>();
try
{
using (SqlConnection con = new SqlConnection(_connectionString))
{
SqlCommand cmd = new SqlCommand("select * from employee", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Employee emp = new Employee
{
Id = Convert.ToInt32(reader["Id"]),
EmpName = reader["EmpName"].ToString(),
Position = reader["Position"].ToString(),
Salary = Convert.ToDecimal(reader["Salary"])
};
employees.Add(emp);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
}
return employees;
}
// Create Method
public int AddEmp(Employee emp)
{
try
{
using (SqlConnection con = new SqlConnection(_connectionString))
{
SqlCommand cmd = new SqlCommand("Insert into employee(EmpName,Position,Salary) values(@EmpName,@Position,@Salary)",con);
con.Open();
cmd.Parameters.AddWithValue("@EmpName",emp.EmpName);
cmd.Parameters.AddWithValue("@Position",emp.Position);
cmd.Parameters.AddWithValue("@Salary", emp.Salary);
return Convert.ToInt32(cmd.ExecuteScalar());
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
}
return 0;
}
// Update Method
public void UpdateEmp(Employee emp)
{
try
{
using(SqlConnection con = new SqlConnection(_connectionString))
{
SqlCommand cmd = new SqlCommand("Update employee set EmpName=@EmpName,Position=@Position,Salary=@Salary where Id=@Id",con);
con.Open();
cmd.Parameters.AddWithValue("@EmpName",emp.EmpName);
cmd.Parameters.AddWithValue("@Position",emp.Position);
cmd.Parameters.AddWithValue("@Salary",emp.Salary);
cmd.Parameters.AddWithValue("@Id",emp.Id);
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
}
}
// Delete Method
public void DeleteEmp(int empId)
{
try
{
using(SqlConnection con = new SqlConnection(_connectionString))
{
SqlCommand cmd = new SqlCommand("delete from employee where Id=@Id",con);
con.Open();
cmd.Parameters.AddWithValue("@Id",empId);
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
}
}
// Get data by Id
public Employee GetEmpById(int empId)
{
Employee emp = null;
try
{
using(SqlConnection con = new SqlConnection(_connectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from employee where Id=@Id",con);
cmd.Parameters.AddWithValue("@Id",empId);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
emp = new Employee();
emp.Id = Convert.ToInt32(reader["Id"]);
emp.EmpName = reader["EmpName"].ToString();
emp.Position = reader["Position"].ToString();
emp.Salary = Convert.ToDecimal(reader["Salary"]);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
}
return emp;
}
}
}
Above File is EmployeeCRUDoperations\Model\EmployeeDAL.cs
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="EmpPage.aspx.cs" Inherits="EmployeeCRUDoperations.EmpPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<div class="container">
<div class="row">
<div class="col-md-6">
<asp:GridView ID="GridViewEmployees" runat="server" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#333333" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black">
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID"/>
<asp:BoundField DataField="EmpName" HeaderText="EmployeeName"/>
<asp:BoundField DataField="Position" HeaderText="Position"/>
<asp:BoundField DataField="Salary" HeaderText="Salary"/>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="Gray" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
</div>
<div class="col-md-6">
EmployeeId:<asp:TextBox ID="txtId" runat="server" AutoPostBack="True" OnTextChanged="txtId_TextChanged" CssClass="form-control"></asp:TextBox><br/>
EmployeeName:<asp:TextBox ID="txtEmpName" runat="server" CssClass="form-control"></asp:TextBox><br/>
Position:<asp:TextBox ID="txtPosition" runat="server" CssClass="form-control"></asp:TextBox><br/>
Salary:<asp:TextBox ID="txtSalary" runat="server" CssClass="form-control"></asp:TextBox><br/>
<asp:Button ID="btnAdd" runat="server" Text="Add Employee" OnClick="btnAdd_Click" CssClass="btn btn-primary"/>
<asp:Button ID="btnUpdate" runat="server" Text="Update Employee" OnClick="btnUpdate_Click" CssClass="btn btn-warning"/>
<asp:Button ID="btnDelete" runat="server" Text="Delete Employee" OnClick="btnDelete_Click" CssClass="btn btn-danger"/>
</div>
</div>
</div>
</asp:Content>
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment