Wednesday, January 18, 2012

Class1 stored procedure

\App_Code\Connection.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Web;
using System.Data;
using System.Data.SqlClient;

///
/// Summary description for Connection
///
public class Connection
{
public static SqlConnection CreateConnection()
{
string connectionstring;
connectionstring = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(connectionstring);
return sqlcon;
}
}


\App_Code\Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web;
///
/// Summary description for Class1
///
public class Class1
{

// this function is used to bind data
public static DataSet execute_spfill_grid(string spname)
{

SqlConnection con = Connection.CreateConnection();
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = spname;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(spname, con);
da.Fill(ds);

return ds;

}


public static void update_reg_form(string spname, string id, string pass, string name) // this function is used to update data
{
SqlConnection con = Connection.CreateConnection();

SqlCommand cmd = new SqlCommand();
cmd.CommandText = spname;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = id;
cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = pass;
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name;

cmd.Connection = con;
con.Open();

cmd.ExecuteNonQuery();
con.Close();
}

public static void ADD_reg_form(string spname, string id, string pass, string name)// this function is used to Add data
{
SqlConnection con = Connection.CreateConnection();

SqlCommand cmd = new SqlCommand();
cmd.CommandText = spname;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = id;
cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = pass;
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name;

cmd.Connection = con;
con.Open();

cmd.ExecuteNonQuery();
con.Close();
}

public static void DEL_reg_form(string spname, string id)// this function is used to Delete data
{
SqlConnection con = Connection.CreateConnection();

SqlCommand cmd = new SqlCommand();
cmd.CommandText = spname;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = id;


cmd.Connection = con;
con.Open();

cmd.ExecuteNonQuery();
con.Close();
}
}


\Default.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;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();// this function is called when the page is loaded.
}
}
public void BindData()
{
// Here data is fetched from the database through stored procedure(insert_reg).

GridView1.DataSource = Class1.execute_spfill_grid("insert_reg");
// Here data is bind to the control and show on page

GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;

tbUid.Text = ((TextBox)GridView1.FooterRow.FindControl("txtid2")).Text;
tbPassword.Text = ((TextBox)GridView1.FooterRow.FindControl("txtid4")).Text;
tbname.Text = ((TextBox)GridView1.FooterRow.FindControl("txtid6")).Text;


BindData();
}
//RowCancelingEdit event is used to cancel edit mode.
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Here FindControl will find the Id in which updating will be done.
string i = ((Label)GridView1.Rows[e.RowIndex].Cells[0].FindControl("lblid1")).Text;
string pas = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("txtid3")).Text;
string nm = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtid5")).Text;
Class1.update_reg_form("update_reg", i, pas, nm);
// update_regform is a function which fetch the data from database through stored procedure(update_reg).
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ADD")
{
// Here FindControl will find the Id in which adding will be done.

string i = ((TextBox)GridView1.FooterRow.FindControl("txtid2")).Text;
string pas = ((TextBox)GridView1.FooterRow.FindControl("txtid4")).Text;
string nm = ((TextBox)GridView1.FooterRow.FindControl("txtid6")).Text;

Class1.ADD_reg_form("add_reg", i, pas, nm);
GridView1.EditIndex = -1;
BindData();


}
if (e.CommandName == "edit")
{
tbUid.Text = ((TextBox)GridView1.FooterRow.FindControl("txtid2")).Text;
tbPassword.Text = ((TextBox)GridView1.FooterRow.FindControl("txtid4")).Text;
tbname.Text = ((TextBox)GridView1.FooterRow.FindControl("txtid6")).Text;
//BindData();
}
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string i = ((Label)GridView1.Rows[e.RowIndex].Cells[0].FindControl("lblid")).Text;
Class1.DEL_reg_form("del_reg", i);
GridView1.EditIndex = -1;
BindData();
}
protected void btnSave_Click(object sender, EventArgs e)
{
string i = tbUid.Text;
string pas = tbPassword.Text;
string nm = tbname.Text;

Class1.ADD_reg_form("add_reg", i, pas, nm);
GridView1.EditIndex = -1;
BindData();
}
}

\Default.aspx


No comments: