Table:
Class File:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace nsemployees
{
public interface intemp
{
Int32 eno { get; set; }
string ename { get; set; }
string eadd { get; set; }
Int32 esal { get; set; }
}
public class clsempprp : intemp
{
private Int32 prveno, prvesal;
private string prvename, prveadd;
public string eadd
{
get
{
return prveadd;
}
set
{
prveadd = value;
}
}
public string ename
{
get
{
return prvename;
}
set
{
prvename = value;
}
}
public int eno
{
get
{
return prveno;
}
set
{
prveno = value;
}
}
public int esal
{
get
{
return prvesal;
}
set
{
prvesal = value;
}
}
}
public abstract class clscon
{
protected SqlConnection con = new SqlConnection();
public clscon()
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
}
}
public class clsemp:clscon
{
public void save_rec(clsempprp p)
{
if(con.State==ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("insemp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = p.eno;
cmd.Parameters.Add("@en", SqlDbType.VarChar,50).Value = p.ename;
cmd.Parameters.Add("@ed", SqlDbType.VarChar,50).Value = p.eadd;
cmd.Parameters.Add("@es", SqlDbType.Int).Value = p.esal;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
public void update_rec(clsempprp p)
{
if(con.State==ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("updemp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = p.eno;
cmd.Parameters.Add("@en", SqlDbType.VarChar,50).Value = p.ename;
cmd.Parameters.Add("@ed", SqlDbType.VarChar,50).Value = p.eadd;
cmd.Parameters.Add("@es", SqlDbType.Int).Value = p.esal;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
public void delete_rec(clsempprp p)
{
if(con.State==ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("delemp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = p.eno;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
public List<clsempprp>disp_rec()
{
if(con.State==ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("dispemp", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
List<clsempprp> obj = new List<clsempprp>();
while (dr.Read())
{
clsempprp k = new clsempprp();
k.eno = Convert.ToInt32(dr[0]);
k.ename = dr[1].ToString();
k.eadd = dr[2].ToString();
k.esal = Convert.ToInt32(dr[3]);
obj.Add(k);
}
dr.Close();
cmd.Dispose();
con.Close();
return obj;
}
public List<clsempprp>find_rec(Int32 eno)
{
if(con.State==ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("findemp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@eno", SqlDbType.Int).Value = eno;
SqlDataReader dr = cmd.ExecuteReader();
List<clsempprp> obj = new List<clsempprp>();
if(dr.HasRows)
{
dr.Read();
clsempprp k = new clsempprp();
k.eno = Convert.ToInt32(dr[0]);
k.ename = dr[1].ToString();
k.eadd = dr[2].ToString();
k.esal = Convert.ToInt32(dr[3]);
obj.Add(k);
}
dr.Close();
cmd.Dispose();
con.Close();
return obj;
}
}
}
Default.aspx
// Add object Data Source and Configure the object data source
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Emp No
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
Name
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
Address
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<br />
Salary
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<br />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="disp_rec" TypeName="nsemployees.clsemp"></asp:ObjectDataSource>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" DataSourceID="ObjectDataSource1" DataTextField="ename" DataValueField="eno" Height="187px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged1" Width="162px"></asp:ListBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click1" Text="Update" />
<asp:Button ID="Delete" runat="server" OnClick="Delete_Click" Text="Delete" />
<asp:Button ID="Clear" runat="server" OnClick="Clear_Click" Text="Clear" />
</div>
</form>
</body>
</html>
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
nsemployees.clsempprp objprp = new nsemployees.clsempprp();
nsemployees.clsemp obj = new nsemployees.clsemp();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
objprp.eno = Convert.ToInt32(TextBox1.Text);
objprp.ename = TextBox2.Text;
objprp.eadd = TextBox3.Text;
objprp.esal = Convert.ToInt32(TextBox4.Text);
obj.save_rec(objprp);
ListBox1.DataBind();
clear_rec();
}
private void clear_rec()
{
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
TextBox3.Text = string.Empty;
TextBox4.Text = string.Empty;
TextBox1.Focus();
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
List<nsemployees.clsempprp> k;
k = obj.find_rec(Convert.ToInt32(ListBox1.SelectedValue));
TextBox1.Text = k[0].eno.ToString();
TextBox2.Text = k[0].ename;
TextBox3.Text = k[0].eadd;
TextBox4.Text = k[0].esal.ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
objprp.eno = Convert.ToInt32(TextBox1.Text);
objprp.ename = TextBox2.Text;
objprp.eadd = TextBox3.Text;
objprp.esal = Convert.ToInt32(TextBox4.Text);
obj.update_rec(objprp);
ListBox1.DataBind();
clear_rec();
}
protected void Button3_Click(object sender, EventArgs e)
{
objprp.eno = Convert.ToInt32(TextBox1.Text);
obj.delete_rec(objprp);
ListBox1.DataBind();
clear_rec();
}
protected void Button4_Click(object sender, EventArgs e)
{
clear_rec();
}
}
Ouput: