ASP .net CRUD Operations using stored procedures | CRUD in ASP.Net | Winsoft Technologies
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace CRUD_ASP_WithSP
{
public partial class WebForm1 : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;
private void ShowEmployeeData()
{
try
{
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand("prc_ShowEmpData", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void Page_Load(object sender, EventArgs e)
{
ShowEmployeeData();
}
private void InsertEmployeeData()
{
try
{
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand("prc_InsertEmpData", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@empid",txtEmpId.Text);
cmd.Parameters.AddWithValue("@ename",txtEmpName.Text);
cmd.Parameters.AddWithValue("@salary",txtEmpSalary.Text);
int result = cmd.ExecuteNonQuery();
if (result==1)
{
lblMessage.Text ="Record Saved Successfully";
}
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
InsertEmployeeData();
ShowEmployeeData();
}
private void UpdateEmployeeData()
{
try
{
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand("prc_UpdateEmpData", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@empid",txtEmpId.Text);
cmd.Parameters.AddWithValue("@ename", txtEmpName.Text);
cmd.Parameters.AddWithValue("@salary", txtEmpSalary.Text);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
lblMessage.Text = "Record Updated Successfully";
}
else
{
lblMessage.Text = "Record Not Updated..";
}
con.Close();
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
UpdateEmployeeData();
ShowEmployeeData();
}
private void DeleteEmployeeData()
{
try
{
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand("prc_DeleteEmpData", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@empid", txtEmpId.Text);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
lblMessage.Text = "Record Deleted Successfully";
}
else
{
lblMessage.Text = "Record Not Deleted..";
}
con.Close();
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
DeleteEmployeeData();
ShowEmployeeData();
}
}
}
Above File is CRUD_ASP_WithSP\WebForm1.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CRUD_ASP_WithSP.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
Employee CRUD operations using Stored Procedures
<table class="auto-style1">
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Employee ID</td>
<td>
<asp:TextBox ID="txtEmpId" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Name of Employee</td>
<td>
<asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Salary</td>
<td>
<asp:TextBox ID="txtEmpSalary" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" Height="45px" Width="96px" OnClick="btnSave_Click"/>
<asp:Button ID="btnUpdate" runat="server" Text="Update" Height="45px" Width="96px" OnClick="btnUpdate_Click"/>
<asp:Button ID="btnDelete" runat="server" Text="Delete" Height="45px" Width="96px" OnClick="btnDelete_Click"/>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" Height="255px" Width="433px">
<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="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment