ASP.NET CRUD OPERATION
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;
public partial class _Default : System.Web.UI.Page
{
private SqlConnection con;
private void connection()
{
string constring = WebConfigurationManager.
ConnectionStrings["connstring"].ToString();
con = new SqlConnection(constring);
}
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
gvdisp();
}
}
protected void btninsert_Click(object sender, EventArgs e)
{
try
{
connection();
con.Open();
bool rescheckrecord = CheckRecord(txtname.Text, txtcity.Text);
if (rescheckrecord == false)
{
con.Close();
Response.Write("<font color='red'>Record All Ready Exits</font><br/><hr/>");
txtname.Text = "";
txtcity.Text = "";
}
else
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_InsertRecord", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@studname", txtname.Text);
cmd.Parameters.AddWithValue("@studcity", txtcity.Text);
int res = cmd.ExecuteNonQuery();
if (res > 0)
{
con.Close();
Response.Write("<script>alert('Insert Record Successfully');</script>");
txtname.Text = "";
txtcity.Text = "";
gvdisp();
}
}
}
catch (Exception ex)
{
con.Close();
Response.Write("<script>alert(" + ex.Message + ");</script>");
}
}
public bool CheckRecord(string nm, string city)
{
connection();
con.Open();
SqlCommand cmd = new SqlCommand("sp_GetSpecificRecord", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@studname", nm);
cmd.Parameters.AddWithValue("@studcity", city);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
dr.Close();
con.Close();
return false;
}
else
{
dr.Close();
con.Close();
return true;
}
}
public void gvdisp()
{
connection();
con.Open();
SqlCommand cmd = new SqlCommand("sp_GetAllRecord", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
gvstudent.DataSource = dt;
gvstudent.DataBind();
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
gvstudent.EditIndex = e.NewEditIndex;
gvdisp();
}
protected void OnRowCancelEdit(object sender, GridViewCancelEditEventArgs e)
{
gvstudent.EditIndex = -1;
gvdisp();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
GridViewRow row = gvstudent.Rows[e.RowIndex];
int id = Convert.ToInt16(gvstudent.DataKeys[e.RowIndex].Values[1]);
string name = (row.Cells[2].Controls[0] as TextBox).Text;
string city = (row.Cells[3].Controls[0] as TextBox).Text;
connection();
con.Open();
SqlCommand cmd = new SqlCommand("sp_UpdateRecord", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@studid", id);
cmd.Parameters.AddWithValue("@studname", name);
cmd.Parameters.AddWithValue("@studcity", city);
int res = cmd.ExecuteNonQuery();
if (res > 0)
{
gvstudent.EditIndex = -1;
con.Close();
Response.Write("<script>alert('Record Update Successfully');</script>");
gvdisp();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt16(gvstudent.DataKeys[e.RowIndex].Values[0]);
connection();
con.Open();
SqlCommand cmd = new SqlCommand("sp_DeleteRecord", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@studid", id);
int res = cmd.ExecuteNonQuery();
if (res > 0)
{
Response.Write("<script>alert('Record Delete Successfully');</script>");
con.Close();
gvdisp();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != gvstudent.EditIndex)
{
(e.Row.Cells[0].Controls[2] as LinkButton).Attributes["onclick"] = "return confirm('Are you sure want to delete record?');";
}
}
}
Comments
Post a Comment