Thursday, May 14, 2009

How to Use DataList

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace MyCodeSnippet
{
public partial class DataList : System.Web.UI.Page
{

#region connection string

//static string txtpass;
static SqlConnection conn = new SqlConnection("server=sys17;Persist Security Info=False;database=Demo;User ID=sa;Password=Sunarc123");

#endregion
#region GetViewState/SetViewState
private int UserId;
private void GetViewState()
{
this.UserId = Convert.ToInt32(ViewState["UserId"]);

}
private void SetViewState()
{
ViewState["UserId"] = this.UserId;

}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.SetData();



}
}
private void SetData()
{

SqlCommand cmd = new SqlCommand("SELECT * FROM UserLogin", conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);

try
{
conn.Open();
DataSet ds = new DataSet();
adapter.Fill(ds);

dlview.DataSource = ds;
dlview.DataBind();




conn.Close();
}
finally
{

}
}

protected void Update_Command(object source, DataListCommandEventArgs e)
{

this.UserId=Convert.ToInt32(dlview.DataKeys[e.Item.ItemIndex]);

SetViewState();
TextBox tbox = default(TextBox);
TextBox txtUserId = (TextBox)e.Item.FindControl("EditUser");
TextBox txtPassword = (TextBox)e.Item.FindControl("EditPassword");
GetViewState();
if (txtUserId == null) { return; }
if (txtPassword == null) { return; }


SqlCommand cmd = new SqlCommand(
"update UserLogin set UserId=" + Convert.ToInt32(txtUserId.Text) + ",Password=" + "'" + txtPassword.Text + "'" + " where UserId=" + this.UserId,
conn);


try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
dlview.EditItemIndex = -1;
this.SetData();

}
finally
{

}





}

protected void Cancel_Command(object source, DataListCommandEventArgs e)
{
dlview.EditItemIndex = -1;
this.SetData();
}

protected void Delete_Command(object source, DataListCommandEventArgs e)
{
this.UserId = Convert.ToInt32(dlview.DataKeys[e.Item.ItemIndex]);
SqlCommand cmd = new SqlCommand("DELETE FROM UserLogin WHERE UserId =" + this.UserId,
conn);


try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
this.SetData();

}
finally
{

}
}

protected void Edit_Command(object source, DataListCommandEventArgs e)
{
dlview.EditItemIndex=e.Item.ItemIndex;
this.SetData();

}

protected void dlview_ItemCommand(object source, DataListCommandEventArgs e)
{


if (e.CommandName == "Insert")
{

TextBox UserId = (TextBox)e.Item.FindControl("InsUser");
TextBox Password = (TextBox)e.Item.FindControl("InsPassword");
if (UserId == null) { return; }
if (Password == null) { return; }


SqlCommand cmd = new SqlCommand(
"insert into UserLogin values(" + Convert.ToInt32(UserId.Text) + "," + "'" + Password.Text + "'" + ")",
conn);


try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
dlview.EditItemIndex = -1;
this.SetData();
}
finally
{

}
}
if (e.CommandName == "InsCancel")
{
dlview.EditItemIndex = -1;
this.SetData();
}

}
}
}

No comments: