Asp.net Web form curd operation using store procedure
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WorkProgras
{
public partial class gridview : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindata();
}
}
#region databinding sqltogrid
public void bindata()
{
SqlConnection con = new SqlConnection("Data Source=DEVSYS075\\SQLEXPRESS2012;Initial Catalog=bookDb;Integrated Security=True");
con.Open();
//SqlCommand cmd = new SqlCommand("Select * from emp", con);
SqlCommand cmd = new SqlCommand();
//store procedure to getall record
cmd = new SqlCommand("getalldata", con);
cmd.CommandType = CommandType.StoredProcedure;
//sqldataadapter
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
//if (ds.Tables[0].Rows.Count > 0)
//{
GridView1.DataSource = ds;
GridView1.DataBind();
txtempname.Text = "";
txtempsal.Text = "";
//}
}
#endregion
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void btnsave_click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=DEVSYS075\\SQLEXPRESS2012;Initial Catalog=bookDb;Integrated Security=True");
////SqlCommand cmd = new SqlCommand("insert into emp(emp_name,emp_salary) values('"+txtempname.Text+"','"+txtempsal.Text+"')",con);
//SqlCommand cmd = new SqlCommand();
//cmd.Parameters.Add("@emp_name", SqlDbType.VarChar).Value = txtempname.Text;
//cmd.Parameters.Add("@emp_salary", SqlDbType.Decimal).Value = txtempsal.Text;
//cmd = new SqlCommand("insert_data", con);
//cmd.CommandType = CommandType.StoredProcedure;
//con.Open();
//cmd.ExecuteNonQuery();
//con.Close();
//con = new SqlConnection("server=(local); database= gaurav;uid=sa;pwd=");
cmd = new SqlCommand("insert_data", con);
cmd.Parameters.Add("@emp_name", SqlDbType.VarChar).Value = txtempname.Text;
cmd.Parameters.Add("@emp_salary", SqlDbType.Decimal).Value = txtempsal.Text;
//cmd = new SqlCommand("insert_data", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
bindata();
}
protected void btnrowedit_click(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindata();
}
protected void btndeleting_click(object sender, GridViewDeleteEventArgs e)
{
//Finding the controls from Gridview for the row which is going to update
Label name = GridView1.Rows[e.RowIndex].FindControl("txtgridname") as Label;
Label city = GridView1.Rows[e.RowIndex].FindControl("txtgridsalary") as Label;
SqlConnection con = new SqlConnection("Data Source=DEVSYS075\\SQLEXPRESS2012;Initial Catalog=bookDb;Integrated Security=True");
con.Open();
//updating the record
SqlCommand cmd = new SqlCommand("delete emp set emp_name='" + name.Text + "',emp_salary='" + city.Text + "' where emp_id = 1",con);
cmd.ExecuteNonQuery();
con.Close();
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
//Call ShowData method for displaying updated data
bindata();
}
protected void btncalceling_click(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindata();
}
protected void btnupdating_click(object sender, GridViewUpdateEventArgs e)
{
//Finding the controls from Gridview for the row which is going to update
TextBox name = GridView1.Rows[e.RowIndex].FindControl("txtgridname") as TextBox;
TextBox city = GridView1.Rows[e.RowIndex].FindControl("txtgridsalary") as TextBox;
SqlConnection con = new SqlConnection("Data Source=DEVSYS075\\SQLEXPRESS2012;Initial Catalog=bookDb;Integrated Security=True");
con.Open();
//updating the record
SqlCommand cmd = new SqlCommand("Update emp set emp_name='" + name.Text + "',emp_salary='" + city.Text + "' where emp_id = emp_id ", con);
cmd.ExecuteNonQuery();
con.Close();
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
//Call ShowData method for displaying updated data
bindata();
}
}
}
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WorkProgras
{
public partial class gridview : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindata();
}
}
#region databinding sqltogrid
public void bindata()
{
SqlConnection con = new SqlConnection("Data Source=DEVSYS075\\SQLEXPRESS2012;Initial Catalog=bookDb;Integrated Security=True");
con.Open();
//SqlCommand cmd = new SqlCommand("Select * from emp", con);
SqlCommand cmd = new SqlCommand();
//store procedure to getall record
cmd = new SqlCommand("getalldata", con);
cmd.CommandType = CommandType.StoredProcedure;
//sqldataadapter
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
//if (ds.Tables[0].Rows.Count > 0)
//{
GridView1.DataSource = ds;
GridView1.DataBind();
txtempname.Text = "";
txtempsal.Text = "";
//}
}
#endregion
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void btnsave_click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=DEVSYS075\\SQLEXPRESS2012;Initial Catalog=bookDb;Integrated Security=True");
////SqlCommand cmd = new SqlCommand("insert into emp(emp_name,emp_salary) values('"+txtempname.Text+"','"+txtempsal.Text+"')",con);
//SqlCommand cmd = new SqlCommand();
//cmd.Parameters.Add("@emp_name", SqlDbType.VarChar).Value = txtempname.Text;
//cmd.Parameters.Add("@emp_salary", SqlDbType.Decimal).Value = txtempsal.Text;
//cmd = new SqlCommand("insert_data", con);
//cmd.CommandType = CommandType.StoredProcedure;
//con.Open();
//cmd.ExecuteNonQuery();
//con.Close();
//con = new SqlConnection("server=(local); database= gaurav;uid=sa;pwd=");
cmd = new SqlCommand("insert_data", con);
cmd.Parameters.Add("@emp_name", SqlDbType.VarChar).Value = txtempname.Text;
cmd.Parameters.Add("@emp_salary", SqlDbType.Decimal).Value = txtempsal.Text;
//cmd = new SqlCommand("insert_data", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
bindata();
}
protected void btnrowedit_click(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindata();
}
protected void btndeleting_click(object sender, GridViewDeleteEventArgs e)
{
//Finding the controls from Gridview for the row which is going to update
Label name = GridView1.Rows[e.RowIndex].FindControl("txtgridname") as Label;
Label city = GridView1.Rows[e.RowIndex].FindControl("txtgridsalary") as Label;
SqlConnection con = new SqlConnection("Data Source=DEVSYS075\\SQLEXPRESS2012;Initial Catalog=bookDb;Integrated Security=True");
con.Open();
//updating the record
SqlCommand cmd = new SqlCommand("delete emp set emp_name='" + name.Text + "',emp_salary='" + city.Text + "' where emp_id = 1",con);
cmd.ExecuteNonQuery();
con.Close();
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
//Call ShowData method for displaying updated data
bindata();
}
protected void btncalceling_click(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindata();
}
protected void btnupdating_click(object sender, GridViewUpdateEventArgs e)
{
//Finding the controls from Gridview for the row which is going to update
TextBox name = GridView1.Rows[e.RowIndex].FindControl("txtgridname") as TextBox;
TextBox city = GridView1.Rows[e.RowIndex].FindControl("txtgridsalary") as TextBox;
SqlConnection con = new SqlConnection("Data Source=DEVSYS075\\SQLEXPRESS2012;Initial Catalog=bookDb;Integrated Security=True");
con.Open();
//updating the record
SqlCommand cmd = new SqlCommand("Update emp set emp_name='" + name.Text + "',emp_salary='" + city.Text + "' where emp_id = emp_id ", con);
cmd.ExecuteNonQuery();
con.Close();
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
//Call ShowData method for displaying updated data
bindata();
}
}
}
Comments
Post a Comment