Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

CRUD.aspx.

cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace CRUDWebAppplication
{
public partial class CRUD : System.Web.UI.Page
{
DataAccessObject dao = new DataAccessObject();
protected void Page_Load(object sender, EventArgs e)
{

protected void btnSearch_Click(object sender, EventArgs e)


{
string[] data = dao.findDetail(textMobile.Text);
if (data.Length > 0)
{
textName.Text = data[0];
textEmail.Text = data[1];
Department.Text = data[2];
Designation.Text = data[3];
}
else
{
Message.Text = "Record Not Found";
}
}

protected void btnUpdate_Click(object sender, EventArgs e)


{
string name = textName.Text;
string mobile = textMobile.Text;
string email = textEmail.Text;
string dept = Department.SelectedValue;
string design = Department.SelectedValue;

int x = dao.upadateDetails(name, mobile, email, dept, design);


if (x == 1)
{
Message.Text = "Details Successfully Updated";
}
else
{
Message.Text = "Details Not Updated";
}
}

protected void btnDelete_Click(object sender, EventArgs e)


{
int x = dao.deleteDetails(textMobile.Text);
if (x == 1)
{
Message.Text = "Details Successfully Deleted";
}
else
{
Message.Text = "Details Not Deleted";
}
}

protected void btnSubmit_Click(object sender, EventArgs e)


{
string name = textName.Text;
string mobile = textMobile.Text;
string email = textEmail.Text;
string gender = "";
if(Male.Checked)
{
gender = "Male";
}
else
{
gender = "Female";
}
string dept = Department.SelectedValue;
string design = Department.SelectedValue;

int x = dao.insertDetails(name, mobile, email, gender, dept, design);


if(x==1)
{
Message.Text = "Details Successfully Submitted";
}
else
{
Message.Text = "Details Not Submitted";
}
}

protected void btnView_Click(object sender, EventArgs e)


{
DataTable dt = new DataTable();
dt = dao.viewDetails();
DataList1.DataSource = dt;
DataList1.DataBind();
}
}
}

DataAccessObject.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace CRUDWebAppplication
{
public class DataAccessObject
{
SqlConnection connection;
SqlCommand command;
SqlDataReader reader;
SqlDataAdapter adapter;
DataTable table;

public DataAccessObject()
{
connection = new
SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString());

public int insertDetails(string name, string mobile, string email, string


gender, string dept, string design)
{

connection.Open();

command = new SqlCommand("insert into Employee


values(@empName,@empPhNo,@empEmail,@empGender,@empDept,@empDesign)", connection);

command.Parameters.AddWithValue("@empName", name);
command.Parameters.AddWithValue("@empPhNo", mobile);
command.Parameters.AddWithValue("@empEmail", email);
command.Parameters.AddWithValue("@monthName", gender);
command.Parameters.AddWithValue("@empDept", dept);
command.Parameters.AddWithValue("@empDesign", design);

int y = command.ExecuteNonQuery();
return y;
}

public int deleteDetails(string mobile)


{

connection.Close();
connection.Open();
command = new SqlCommand("Delete From Employee Where MobNumber=
@mobile", connection);
command.Parameters.AddWithValue("@mobile", mobile);
int x = command.ExecuteNonQuery();
return x;
}
public DataTable viewDetails()
{
connection.Close();
connection.Open();
adapter = new SqlDataAdapter("select * from Employee", connection);
table = new DataTable();
adapter.Fill(table);
return table;
}
public string[] findDetail(string mobile)
{

connection.Close();
connection.Open();
command = new SqlCommand("select * from Employee Where
MobNumber=@mobile", connection);
command.Parameters.AddWithValue("@mobile", mobile);
reader = command.ExecuteReader();

string[] data = new string[4];

if (reader.Read())
{
data[0] = reader[0].ToString();
data[1] = reader[2].ToString();
data[2] = reader[4].ToString();
data[3] = reader[5].ToString();
}
else
{
data = new string[0];
}
return data;
}
public int upadateDetails(string name, string mobile, string email, string
dept, string design)
{
connection.Close();
connection.Open();

command = new SqlCommand("update Employee set


Name=@employeename,Email=@email,Department=@department,Designation=@designation
Where MobNumber=@mobile", connection);

command.Parameters.AddWithValue("@employeename", name);
command.Parameters.AddWithValue("@email", email);
command.Parameters.AddWithValue("@mobile", mobile);
command.Parameters.AddWithValue("@department", dept);
command.Parameters.AddWithValue("@designation", design);

int y = command.ExecuteNonQuery();
return y;

}
}

web.config

<configuration>
<connectionStrings>
<add name="dbConnection" connectionString="Data Source=(localdb)\
MSSQLLocalDB;database=sampldb;Integrated security=true"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

You might also like