0% found this document useful (0 votes)
35 views5 pages

Maruthupandi (25.08.2022)

This document contains code for performing CRUD (create, read, update, delete) operations on employee data stored in a SQL database. It includes a DataAccessObject class with methods like insertDetails(), deleteDetails(), findDetail(), and updateDetails() that execute SQL commands. These methods are called by event handler methods on a CRUD.aspx.cs page, like btnSubmit_Click() for creating a new record. The web.config file contains the connection string for connecting to the SQL database.

Uploaded by

Maruthu pandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views5 pages

Maruthupandi (25.08.2022)

This document contains code for performing CRUD (create, read, update, delete) operations on employee data stored in a SQL database. It includes a DataAccessObject class with methods like insertDetails(), deleteDetails(), findDetail(), and updateDetails() that execute SQL commands. These methods are called by event handler methods on a CRUD.aspx.cs page, like btnSubmit_Click() for creating a new record. The web.config file contains the connection string for connecting to the SQL database.

Uploaded by

Maruthu pandi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy