public partial class Emp : System.Web.UI.Page
{
string ConnectionString = "Data Source=localhost;Initial Catalog=loginDataBase;Integrated Security=True";
SqlConnection con;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
//Initially load some data in the listaview from database
if(!Page.IsPostBack)
{
LoadListView();
}
}
private void LoadListView()
{
try
{
con = new SqlConnection(ConnectionString);
con.Open();
cmd = new SqlCommand("SELECT FROM emp", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ListView1.DataSource = ds;
ListView1.DataBind();
}
catch
{
throw;
}
//catch (Exception ex)
//{
//}
finally
{
con.Close();
}
}
//set and get sort statae in view state
private string GVSortDirection
{
get { return ViewState["SortDirection"] as string ?? "DESC"; }
set { ViewState["SortDirection"] = value; }
}
private void GetSortDirection(string arrange)
{
switch (arrange)
{
case "ASC":
GVSortDirection = "DESC";
break;
case "DESC":
GVSortDirection = "ASC";
break;
// switch (hgsg)
//{
// //if previous sort direction is ascending order then assume new sort direction is descending order
// Case "ASC":
// GVSortDirection="DESC";
// break;
// //if previous sort direction is descending order then assume new sort direction is ascending order
// Case "DESC":
// GVSortDirection="ASC";
// break;
}
}
protected void ListView1_ItemCommand(ListViewCommandEventArgs e)
{
//Get command name and process based on that
//Get the command name and verify that is insert if yes then this block will execute
if(e.CommandName=="Ins")
{
TextBox txtIeno=(TextBox)e.Item.FindControl("txtIeno");
TextBox txtIempname=(TextBox)e.Item.FindControl("txtIempname");
TextBox txtIsal=(TextBox)e.Item.FindControl("txtIsal");
string insertCommand="insert into emp values ('"+txtIeno.Text+"','"+txtIempname.Text+"','"+txtIsal.Text +"')";
ExecuteCommand(insertCommand);
LoadListView();
}
//Get command name and verify that is update if yes then this block will execute
if(e.CommandName=="Upd")
{
TextBox txtIeno=(TextBox)e.Item.FindControl("txtIeno");
TextBox txtIempname=(TextBox)e.Item.FindControl("txtIempname");
TextBox txtIsal=(TextBox)e.Item.FindControl("txtIsal");
string insertCommand="Update emp set empname='" + txtIempname.Text + "', sal='" + txtIsal.Text + "' where eno=" + Convert.ToInt32(txtIeno.Text) + ";";
ExecuteCommand(updateCommand);
ListView1.EditIndex=-1;
LoadListView();
}
//Get the command name and verify thatis delete if yes then this block will execute
if(e.CommandName=="Del")
{
Label txteno =(Label)e.Item.FindControl("lbleno");
String deleteComand="delete from emp where eno=" + Convert.ToInt32(txteno .Text);
ExecuteCommand(deleteComand);
LoadListView();
}
//Get the commmand name and verify that is view if yes then this block excecute and display details in the textbox
if(e.CommandName=="Show")
{
//get the selected item
ListViewDataItem selectedItem=e.Item as ListViewDataItem;
TextBox1.Text=((Label)selectedItem.FindControl("lbleno")).Text;
TextBox2.Text=((Label)selectedItem.FindControl("lblempname")).Text;
TextBox3.Text=((Label)selectedItem.FindControl("lblsal")).Text;
}
}
private void ExecuteCommand(string qry)
{
try
{
con.Open();
cmd=new SqlCommand(qry,con);
cmd.ExecuteNonQuery();
}
catch
{
}
finally
{
con.Close();
}
}
//call editing event when user click edit
protected void ListView1_ItemEditing(ListViewEditEventArgs e)
{
ListView1.EditIndex=e.NewEditIndex;
LoadListView();
}
//Cancel editing when user do want to update details
protected void ListView1_ItemCancelling(object sender, ListViewCancelEventArgs e)
{
ListView1.EditIndex = -1;
LoadListView();
}
//Sorting Listview data using sorting event I have verify here previous status ascending or descending
protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
{
DataTable dataTable = (DataTable)Session["dt"];
if (dataTable != null)
{
Session["sort"] = "Y";
//Create new dataview instance and pass datatable
DataView dataView = new DataView(dataTable);
//get sort direction from the get sort direction method
string sortDirection = GetSortDirection();
//Sort dataview data based on the sort directin value
dataView.Sort = e.SortExpression + " " + sortDirection;
//Assign datasource and bind data to List view
ListView1.DataSource = dataView;
ListView1.DataBind();
}
}
protected void ItemDataPager_PreRender(object sender, EventArgs e)
{
try
{
if (Session["sort"] == null)
{
LoadListView();
}
else
{
Session.Remove("sort");
}
}
catch
{
throw;
}
//catch (Exception ex)
//{
//}
}
protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
}
}