Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

SWAT - A simple Web-based Anomalies Tracker - Part 3

, 22 Jun 2003 CPOL
An account of my experience in learning to develop in the .NET environment.
swatpart3_source.zip
Toolbar_Bugs_hover.gif
Toolbar_Help.gif
Toolbar_Help_hover.gif
Toolbar_Home.gif
Toolbar_Home_hover.gif
Toolbar_Logout.gif
Toolbar_Logout_hover.gif
Toolbar_Report.gif
Toolbar_Report_hover.gif
Toolbar_System.gif
Toolbar_System_hover.gif
Global.asax
swat.gif
Swat.csproj.webinfo
Swat.vsdisco
Swat_Bugs.gif
Swat_Modules.gif
Swat_Projects.gif
Swat_Users.gif
SwatLogon.gif
Toolbar_Admin.gif
Toolbar_Admin_hover.gif
Toolbar_Analyze.gif
Toolbar_Analyze_hover.gif
Toolbar_Background.gif
Toolbar_Bugs.gif
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;

namespace Swat
{
	/// <summary>
	/// Summary description for SwatAdmin.
	/// </summary>
	public class SwatAdmin : System.Web.UI.Page
	{
		const int pagesize = 4;
		int pagecount;

		enum EditCategories
		{
			Category_None,
			Category_Users,
			Category_Projects,
			Category_Modules,
			Category_Bugs
		}
		protected System.Web.UI.WebControls.ImageButton btnEditUsers;
		protected System.Web.UI.WebControls.ImageButton btnEditProjects;
		protected System.Web.UI.WebControls.ImageButton btnEditModules;
		protected System.Web.UI.WebControls.DataList DataList1;
		protected System.Web.UI.WebControls.Label lblRole;
		protected System.Web.UI.WebControls.CheckBox chkDeveloper;
		protected System.Web.UI.WebControls.CheckBox chkManager;
		protected System.Web.UI.WebControls.CheckBox chkAdministrator;
		protected System.Web.UI.WebControls.Label lblError;
		protected System.Web.UI.WebControls.Label lblPassword;
		protected System.Web.UI.WebControls.TextBox txtPassword;
		protected System.Web.UI.WebControls.ImageButton btnEditBugs;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			//If the user got here through some other means...
			int nRole = (int)AccessPrivilege.Administrator;
			if (Request.Cookies["Roles"] != null)
			{
				nRole = System.Convert.ToInt16(Request.Cookies["Roles"].Value);
				if ((int)AccessPrivilege.Administrator != (nRole & (int)AccessPrivilege.Administrator))
					Response.Redirect("SwatBugs.aspx",true);
			}
			else
				Response.Redirect("SwatLogon.aspx",true);

			//We know we have this because the user is logged in
			if (Request.Cookies["UserID"] != null)
			{
				Response.Cookies.Add(Request.Cookies["UserID"]);
				Response.Cookies["UserID"].Expires = DateTime.MaxValue;
			}
			if (!Page.IsPostBack)
			{
				ViewState["catsel"] = (int)EditCategories.Category_Users;
				ViewState["curpage"] = 1;
				ViewState["pagecount"] = 0;
				ViewState["currproj"] = 0;
				SetTotalPages();
				BindList("0",ScrollMode.UpdateInPlace);
			}
			pagecount = (int)ViewState["pagecount"];
			lblError.Text = "";
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnEditUsers.Click += new System.Web.UI.ImageClickEventHandler(this.btnEditUsers_Click);
			this.btnEditProjects.Click += new System.Web.UI.ImageClickEventHandler(this.btnEditProjects_Click);
			this.btnEditModules.Click += new System.Web.UI.ImageClickEventHandler(this.btnEditModules_Click);
			this.btnEditBugs.Click += new System.Web.UI.ImageClickEventHandler(this.btnEditBugs_Click);
			this.DataList1.ItemCreated += new System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemCreated);
			this.DataList1.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_ItemCommand);
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.SwatAdmin_PreRender);

		}
		#endregion

		private void SwatAdmin_PreRender(object sender, System.EventArgs e)
		{
			//Clear all buttons
			btnEditUsers.BorderStyle = BorderStyle.Outset;
			btnEditProjects.BorderStyle = BorderStyle.Outset;
			btnEditModules.BorderStyle = BorderStyle.Outset;
			btnEditBugs.BorderStyle = BorderStyle.Outset;
			switch((int)ViewState["catsel"])
			{
				case (int)EditCategories.Category_Users:
					btnEditUsers.BorderStyle = BorderStyle.Inset;
					break;
				case (int)EditCategories.Category_Projects:
					btnEditProjects.BorderStyle = BorderStyle.Inset;
					break;
				case (int)EditCategories.Category_Modules:
					btnEditModules.BorderStyle = BorderStyle.Inset;
					break;
				case (int)EditCategories.Category_Bugs:
					btnEditBugs.BorderStyle = BorderStyle.Inset;
					break;
			}
			//If we are editing users we need to show the user fields
			//Else we hide them
			if ((int)ViewState["catsel"] == (int)EditCategories.Category_Users && (DataList1.EditItemIndex >= 0))
				ShowUserFields(true);
			else
				ShowUserFields(false);
		}

		private void btnEditUsers_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			ViewState["catsel"] = (int)EditCategories.Category_Users;
			ReloadList();
			}
		private void btnEditProjects_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			ViewState["catsel"] = (int)EditCategories.Category_Projects;
			ReloadList();
			}
		private void btnEditModules_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				ViewState["catsel"] = (int)EditCategories.Category_Modules;
				//Select a default project
				SqlConnection cnn;
				SqlCommand cmd;
				SqlDataReader dr;
				string ConnectionString = "user id=ASPNET;password=;initial catalog=swatbugs;data source=localhost;Integrated Security=false;connect timeout=30;";
				cnn = new SqlConnection(ConnectionString);
				cmd = cnn.CreateCommand();
				cnn.Open();
				//Populate the projects combo, I don't know why it won't accept dr directly??
				cmd.CommandText = "SWATGetAllProjects";
				cmd.CommandType = CommandType.StoredProcedure;

				dr = cmd.ExecuteReader();
				if (dr.Read())
				{
					ViewState["currproj"] = dr["id"].ToString();
				}
				dr.Close();
				cnn.Close();
				
				ViewState["selproj"] = 0;
				ReloadList();
			}
			catch(Exception ex)
			{
				string s = ex.ToString();
				lblError.Text = "Database Error!";
			}
		}
		private void btnEditBugs_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			ViewState["catsel"] = (int)EditCategories.Category_Bugs;
			ReloadList();
			}
		private void ReloadList()
		{
			SetTotalPages();
			DataList1.EditItemIndex = -1;
			DataList1.SelectedIndex = -1;
			BindList("0",ScrollMode.UpdateInPlace);
		}

		protected void SetTotalPages()
		{
			string strTable = GetCategoryTable();
			if (strTable.Length == 0)
				return;		//Since this shouldn't happen just return
			try
			{
				SqlConnection cnn;
				string ConnectionString = "user id=ASPNET;password=;initial catalog=swatbugs;data source=localhost;Integrated Security=false;connect timeout=30;";
				cnn = new SqlConnection(ConnectionString);
				cnn.Open();

				SqlCommand cmd = cnn.CreateCommand();
				cmd.CommandType = CommandType.StoredProcedure;
				//If it's modules we need to know the project
				if ((int)ViewState["catsel"] == (int)EditCategories.Category_Modules && ViewState["currproj"] != null)
				{
					cmd.CommandText = "SWATGetProjModuleCount";
					cmd.Parameters.Add("@projid", SqlDbType.Int).Value = ViewState["currproj"];
				}
				else
				{
					cmd.CommandText = "SWATGetTableCount";
					cmd.Parameters.Add("@tablename", SqlDbType.NVarChar, 30).Value = strTable;
				}

				int nRecords = (int)cmd.ExecuteScalar();
				if (nRecords%pagesize == 0)
				{
					pagecount = nRecords / pagesize;
				}
				else
				{
					pagecount = (nRecords / pagesize) + 1;
				}
				//Save it
				ViewState["pagecount"] = pagecount;
				ViewState["curpage"] = 1;
				cnn.Close();
			}
			catch(Exception e)
			{
				string s = e.ToString();
				lblError.Text = "Database Error!";
			}
		}
		private void BindList(string strRecNum, ScrollMode direction)
		{
			string strTable = GetCategoryTable();
			if (strTable.Length == 0)
				return;		//Since this shouldn't happen just return
			try
			{
				SqlConnection cnn;
				string strDirection = ">=";
				StringBuilder sqlString = new StringBuilder("SELECT TOP ");
				sqlString.Append(pagesize.ToString());
				sqlString.Append(" id, itemname FROM ");
				switch(direction)
				{
					case ScrollMode.UpdateInPlace:
						strDirection = ">=";
						break;
					case ScrollMode.UpdateNextPage:
						strDirection = ">";
						break;
					case ScrollMode.UpdatePrevPage:
						strDirection = "<";
						break;
				}

				string ConnectionString = "user id=ASPNET;password=;initial catalog=swatbugs;data source=localhost;Integrated Security=false;connect timeout=30;";
				cnn = new SqlConnection(ConnectionString);
				cnn.Open();
				sqlString.Append(strTable);
				sqlString.Append(" where ");

				//If it's modules we need to specify which project
				if ((int)ViewState["catsel"] == (int)EditCategories.Category_Modules && ViewState["currproj"] != null)
				{
					sqlString.Append("Project=");
					sqlString.Append(ViewState["currproj"].ToString());
					sqlString.Append(" AND ");
				}
				sqlString.Append("ID");
				sqlString.Append(strDirection);
				sqlString.Append(strRecNum);
				if (direction == ScrollMode.UpdatePrevPage)
					sqlString.Append(" ORDER BY id DESC");
				SqlCommand cmd = cnn.CreateCommand();
				cmd.CommandText = sqlString.ToString();
				SqlDataAdapter da = new SqlDataAdapter(cmd);
				DataSet ds = new DataSet();
				da.Fill(ds,"ITEMS");
				ds.Tables["ITEMS"].DefaultView.Sort = "id ASC";
				DataList1.DataSource = ds.Tables["ITEMS"].DefaultView;
				DataList1.DataMember = "Name";
				DataList1.DataKeyField = "ID";
				DataList1.DataBind();
				cnn.Close();
			}
			catch(Exception e)
			{
				string s = e.ToString();
				lblError.Text = "Database Error!";
			}
		}
		private string GetCategoryTable()
		{
			string strTable = "";

			switch((int)ViewState["catsel"])
			{
				case (int)EditCategories.Category_Users:
					strTable = "Users";
					break;
				case (int)EditCategories.Category_Projects:
					strTable = "Projects";
					break;
				case (int)EditCategories.Category_Modules:
					strTable = "Modules";
					break;
				case (int)EditCategories.Category_Bugs:
					strTable = "Bugs";
					break;
			}

			return strTable;
		}

		private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
		{
			if (e.CommandName == "SelectItem")
			{
				ViewState["selitem"] = e.Item.ItemIndex;
				DataList1.SelectedIndex = e.Item.ItemIndex;
				DataList1.EditItemIndex = -1;

				BindList(DataList1.DataKeys[0].ToString(),ScrollMode.UpdateInPlace);
			}
			if (e.CommandName == "EditItem")
			{
				//If we're showing users...
				if ((int)ViewState["catsel"] == (int)EditCategories.Category_Users)
				{
					GetUserData((int)DataList1.DataKeys[e.Item.ItemIndex]);
				}

				DataList1.EditItemIndex = e.Item.ItemIndex;
				BindList(DataList1.DataKeys[0].ToString(),ScrollMode.UpdateInPlace);
			}
			if (e.CommandName == "CancelEdit")
			{
				//Was it an 'add new'
				DataList1.SelectedIndex = -1;
				DataList1.EditItemIndex = -1;
				BindList(DataList1.DataKeys[0].ToString(),ScrollMode.UpdateInPlace);
			}
			if (e.CommandName == "UpdateItem")
			{
				switch((int)ViewState["catsel"])
				{
					case (int)EditCategories.Category_Users:
						UpdateUser(e);
						break;
					case (int)EditCategories.Category_Projects:
						UpdateItem("Projects",e);
						break;
					case (int)EditCategories.Category_Modules:
						UpdateItem("Modules",e);
						break;
					case (int)EditCategories.Category_Bugs:
						//We don't add new bugs from here, we can just edit their name
						UpdateItem("Bugs",e);
						break;
				}
			}
			if (e.CommandName == "DeleteItem")
			{
				DeleteItem((int)DataList1.DataKeys[e.Item.ItemIndex]);
			}
			if (e.CommandName == "Prev")
			{
				PreviousPage();
			}
			if (e.CommandName == "Next")
			{
				NextPage();
			}
			if (e.CommandName == "AddNew")
			{
				AddNewItem();
			}
		}

		private void DataList1_ItemCreated(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
		{
			//We're only interested in the footer
			if (e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Footer)
			{
				//Get a reference to each of our controls
				Control ctlAddNew = ((Control)(e.Item)).FindControl("btnAddNew");
				Control ctlPrev = ((Control)(e.Item)).FindControl("btnPrev");
				Control ctlNext = ((Control)(e.Item)).FindControl("btnNext");
				Control ctlProjects = ((Control)(e.Item)).FindControl("ddlSelProj");
				//If we are displaying the modules and we're not editing...
				if ((int)ViewState["catsel"] == (int)EditCategories.Category_Modules && DataList1.EditItemIndex == -1)
				{
					//Only if it's empty
					if (((DropDownList)ctlProjects).Items.Count == 0)
					{
						try
						{
							SqlConnection cnn;
							SqlCommand cmd;
							SqlDataReader dr;
							string ConnectionString = "user id=ASPNET;password=;initial catalog=swatbugs;data source=localhost;Integrated Security=false;connect timeout=30;";
							cnn = new SqlConnection(ConnectionString);
							cmd = cnn.CreateCommand();
							cnn.Open();
							cmd.CommandType = CommandType.StoredProcedure;
							//Populate the projects combo, I don't know why it won't accept dr directly??
							cmd.CommandText = "SWATGetAllProjects";
							dr = cmd.ExecuteReader();
							while (dr.Read())
							{
								((DropDownList)ctlProjects).Items.Add(new ListItem(dr["itemname"].ToString(),dr["id"].ToString()));
							}
							dr.Close();
							cnn.Close();
						}
						catch(Exception ex)
						{
							string s = ex.ToString();
							lblError.Text = "Database Error!";
						}
					}
					//
					((DropDownList)ctlProjects).SelectedIndex = (int)ViewState["selproj"];
					//Hook in an event handler
					((DropDownList)ctlProjects).SelectedIndexChanged += new System.EventHandler(this.ddlSelProj_SelectedIndexChanged);
					ctlProjects.Visible = true;	//Show the drop down
				}
				else
					ctlProjects.Visible = false;	//Hide the drop down
				//If we are not editing show the AddNew and paging buttons
				if (DataList1.EditItemIndex == -1)
				{
					ctlAddNew.Visible = true;
					ctlPrev.Visible = true;
					ctlNext.Visible = true;
				}
				else
				{
					ctlPrev.Visible = false;
					ctlNext.Visible = false;
					ctlAddNew.Visible = false;
				}
				//The AddNew button is hidden if it's bugs 
				if ((int)ViewState["catsel"] == (int)EditCategories.Category_Bugs)
					ctlAddNew.Visible = false;
				int curpage = (int)ViewState["curpage"];
				//If we're not at the beginning enable the prev page button
				if (curpage > 1)
					((Button)ctlPrev).Enabled = true;
				else
					((Button)ctlPrev).Enabled = false;
				//If we're not at the end enable the next page button
				if (curpage < pagecount)
					((Button)ctlNext).Enabled = true;
				else
					((Button)ctlNext).Enabled = false;
			}
		}
		protected void ddlSelProj_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			ViewState["currproj"] = ((DropDownList)sender).SelectedItem.Value;
			ViewState["selproj"] = ((DropDownList)sender).SelectedIndex;
			BindList("0",ScrollMode.UpdateInPlace);
		}
		private void AddNewItem()
		{
			string strTable = GetCategoryTable();
			if (strTable.Length == 0)
				return;
			try
			{
				//Same as editing except it's a new item
				SqlConnection cnn;
				string strDirection = ">=";
				StringBuilder sqlString = new StringBuilder("SELECT TOP ");
				sqlString.Append(pagesize.ToString());
				sqlString.Append(" id, itemname FROM ");

				string ConnectionString = "user id=ASPNET;password=;initial catalog=swatbugs;data source=localhost;Integrated Security=false;connect timeout=30;";
				cnn = new SqlConnection(ConnectionString);
				cnn.Open();
				sqlString.Append(strTable);
				sqlString.Append(" where ");
				//If it's modules we're displaying...
				if ((int)ViewState["catsel"] == (int)EditCategories.Category_Modules && ViewState["currproj"] != null)
				{
					sqlString.Append("Project=");
					sqlString.Append(ViewState["currproj"].ToString());
					sqlString.Append(" AND ");
				}
				sqlString.Append(" ID");
				sqlString.Append(strDirection);
				sqlString.Append("@recnum");
				SqlCommand cmd = cnn.CreateCommand();
				cmd.CommandText = sqlString.ToString();
				// Setup our parameters
				if (DataList1.Items.Count > 0)
					cmd.Parameters.Add("@recnum", SqlDbType.Int).Value = (int)DataList1.DataKeys[0];
				else
					cmd.Parameters.Add("@recnum", SqlDbType.Int).Value = 0;
				//Setup the data adapter
				SqlDataAdapter da = new SqlDataAdapter(cmd);
				DataSet ds = new DataSet();
				da.Fill(ds,strTable);
				//Create a new row
				DataRow dr = ds.Tables[strTable].NewRow();
				dr[0] = 0;
				ds.Tables[strTable].Rows.Add(dr);
				//Assign the table to the list
				DataList1.DataSource = ds.Tables[strTable];
				//We're editing the row that's being added
				DataList1.EditItemIndex = DataList1.Items.Count;
				DataList1.SelectedIndex = -1;
				DataList1.DataBind();
				cnn.Close();
				//If it's a user clear the data fields
				if ((int)ViewState["catsel"] == (int)EditCategories.Category_Users)
				{
					txtPassword.Text = "";
					chkAdministrator.Checked = false;
					chkManager.Checked = false;
					chkDeveloper.Checked = false;
				}
			}
			catch(Exception e)
			{
				string s = e.ToString();
				lblError.Text = "Database Error!";
			}
		}
		void UpdateItem(string strTable, System.Web.UI.WebControls.DataListCommandEventArgs e)
		{
			try
			{
				SqlConnection cnn;
				string ConnectionString = "user id=ASPNET;password=;initial catalog=swatbugs;data source=localhost;Integrated Security=false;connect timeout=30;";
				cnn = new SqlConnection(ConnectionString);
				cnn.Open();
				StringBuilder sqlString =  new StringBuilder();
				//Were we editing or adding a new one?
				if ((int)DataList1.DataKeys[e.Item.ItemIndex] == 0)
				{
					//It's an insert
					sqlString.Append("Insert ");
					sqlString.Append(strTable);
					//If it's module
					if ((int)ViewState["catsel"] == (int)EditCategories.Category_Modules && ViewState["currproj"] != null)
					{
						sqlString.Append(" (itemname,project) ");
						sqlString.Append("Values (@itemname,@proj)");
					}
					else
					{
						sqlString.Append(" (itemname) ");
						sqlString.Append("Values (@itemname)");
					}
					SqlCommand cmd = cnn.CreateCommand();
					cmd.CommandText = sqlString.ToString();
					// Fill our parameters
					cmd.Parameters.Add("@itemname", SqlDbType.NVarChar, 50).Value = ((TextBox)e.Item.FindControl("txtItemName")).Text;
					//If it's module
					if ((int)ViewState["catsel"] == (int)EditCategories.Category_Modules && ViewState["currproj"] != null)
					{
						cmd.Parameters.Add("@proj", SqlDbType.Int).Value = System.Convert.ToInt32(ViewState["currproj"]);
					}
					cmd.ExecuteNonQuery();
				}
				else
				{
					//An update
					sqlString.Append("Update ");
					sqlString.Append(strTable);
					sqlString.Append(" Set itemname=@itemname");
					//If it's module
					if ((int)ViewState["catsel"] == (int)EditCategories.Category_Modules && ViewState["currproj"] != null)
					{
						sqlString.Append(",project=@proj");
					}
					sqlString.Append(" Where id=@id");
					SqlCommand cmd = cnn.CreateCommand();
					cmd.CommandText = sqlString.ToString();
					// Fill our parameters
					cmd.Parameters.Add("@itemname", SqlDbType.NVarChar, 50).Value = ((TextBox)e.Item.FindControl("txtItemName")).Text;
					cmd.Parameters.Add("@id", SqlDbType.Int).Value = DataList1.DataKeys[e.Item.ItemIndex];
					//If it's module
					if ((int)ViewState["catsel"] == (int)EditCategories.Category_Modules && ViewState["currproj"] != null)
					{
						cmd.Parameters.Add("@proj", SqlDbType.Int).Value = System.Convert.ToInt32(ViewState["currproj"]);
					}
					cmd.ExecuteNonQuery();
				}
				cnn.Close();
			}
			catch(Exception ex)
			{
				string s = ex.ToString();
				lblError.Text = "Database Error!";
			}

			//Update list
			DataList1.EditItemIndex = -1;
			DataList1.SelectedIndex = -1;
			SetTotalPages();
			BindList(DataList1.DataKeys[0].ToString(),ScrollMode.UpdateInPlace);
			//If it's a user we're adding set up the data fields
			if ((int)ViewState["catsel"] == (int)EditCategories.Category_Users)
			{
				txtPassword.Text = "";
				chkDeveloper.Checked = false;
				chkAdministrator.Checked = false;
				chkManager.Checked = false;
			}
		}
		private void ShowUserFields(bool bShow)
		{
			lblPassword.Visible = bShow;
			txtPassword.Visible = bShow;
			lblRole.Visible = bShow;
			chkDeveloper.Visible = bShow;
			chkAdministrator.Visible = bShow;
			chkManager.Visible = bShow;
		}
		private void UpdateUser(System.Web.UI.WebControls.DataListCommandEventArgs e)
		{
			try
			{
				SqlConnection cnn;
				string ConnectionString = "user id=ASPNET;password=;initial catalog=swatbugs;data source=localhost;Integrated Security=false;connect timeout=30;";
				cnn = new SqlConnection(ConnectionString);
				cnn.Open();

				//Were we editing or adding a new one?
				SqlCommand cmd = cnn.CreateCommand();
				cmd.CommandType = CommandType.StoredProcedure;
				if ((int)DataList1.DataKeys[e.Item.ItemIndex] == 0)
				{
					cmd.CommandText = "SWATInsertUser";
				}
				else
				{
					cmd.CommandText = "SWATUpdateUser";
					cmd.Parameters.Add("@id", SqlDbType.Int).Value = DataList1.DataKeys[e.Item.ItemIndex];
				}
				// Fill our parameters
				cmd.Parameters.Add("@itemname", SqlDbType.NVarChar, 64).Value = ((TextBox)e.Item.FindControl("txtItemName")).Text;
				cmd.Parameters.Add("@password", SqlDbType.NVarChar, 128).Value = txtPassword.Text;
				
				//What roles
				int nRoles = 0;
				if (chkDeveloper.Checked)
					nRoles |= (int)AccessPrivilege.Developer;
				if (chkAdministrator.Checked)
					nRoles |= (int)AccessPrivilege.Administrator;
				if (chkManager.Checked)
					nRoles |= (int)AccessPrivilege.Manager;
				cmd.Parameters.Add("@roles", SqlDbType.TinyInt).Value = nRoles;
				cmd.ExecuteNonQuery();
				cnn.Close();
				//Update list
				DataList1.EditItemIndex = -1;
				DataList1.SelectedIndex = -1;
				SetTotalPages();
				BindList(DataList1.DataKeys[0].ToString(),ScrollMode.UpdateInPlace);
			}
			catch(Exception err)
			{
				string s = err.ToString();
				lblError.Text = "Error Updating User!";
			}
		}
		private void GetUserData(int nUserID)
		{
			try
			{
				SqlConnection cnn;
				string ConnectionString = "user id=ASPNET;password=;initial catalog=swatbugs;data source=localhost;Integrated Security=false;connect timeout=30;";
				cnn = new SqlConnection(ConnectionString);
				cnn.Open();
				SqlCommand cmd = cnn.CreateCommand();
				cmd.CommandText = "SWATGetUserData";
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add("@id", SqlDbType.Int).Value = nUserID;

				SqlDataReader dr = cmd.ExecuteReader();
				if (dr.Read())
				{
					if (dr["Password"] != System.DBNull.Value)
						txtPassword.Text = dr["Password"].ToString();
					int nRoles = 0;
					if (dr["Roles"] != System.DBNull.Value)
						nRoles = System.Convert.ToInt16(dr["Roles"].ToString());
					chkAdministrator.Checked = (nRoles & (int)AccessPrivilege.Administrator) == (int)AccessPrivilege.Administrator;
					chkManager.Checked = (nRoles & (int)AccessPrivilege.Manager) == (int)AccessPrivilege.Manager;
					chkDeveloper.Checked = (nRoles & (int)AccessPrivilege.Developer) == (int)AccessPrivilege.Developer;
				}
				
				dr.Close();
				cnn.Close();
			}
			catch(Exception e)
			{
				string s = e.ToString();
				lblError.Text = "Error getting user data!";
			}
		}
		private void DeleteItem(int nID)		
		{
			string strTable = GetCategoryTable();
			if (strTable.Length == 0)
				return;		//Since this shouldn't happen just return
			try
			{
				SqlConnection cnn;
				string ConnectionString = "user id=ASPNET;password=;initial catalog=swatbugs;data source=localhost;Integrated Security=false;connect timeout=30;";
				cnn = new SqlConnection(ConnectionString);
				cnn.Open();
				SqlCommand cmd = cnn.CreateCommand();
				cmd.CommandText = "SWATDeleteListItem";
				cmd.CommandType = CommandType.StoredProcedure;
				cmd.Parameters.Add("@id", SqlDbType.Int).Value = nID;
				cmd.Parameters.Add("@tablename", SqlDbType.NVarChar, 30).Value = strTable;
				cmd.ExecuteNonQuery();
				cnn.Close();
				DataList1.SelectedIndex = -1;
				DataList1.EditItemIndex = -1;
				BindList(DataList1.DataKeys[0].ToString(),ScrollMode.UpdateInPlace);
				SetTotalPages();
			}
			catch(Exception e)
			{
				string s = e.ToString();
				lblError.Text = "Error deleting item!";
			}
		}
		private void NextPage()
		{
			DataList1.EditItemIndex = -1;
			DataList1.SelectedIndex = -1;
			int curpage = (int)ViewState["curpage"];
			if (curpage < pagecount)
			{
				curpage += 1;
				ViewState["curpage"] = curpage;
				BindList(DataList1.DataKeys[DataList1.Items.Count-1].ToString(),ScrollMode.UpdateNextPage);
			}
		
		}
		private void PreviousPage()
		{
			DataList1.EditItemIndex = -1;
			DataList1.SelectedIndex = -1;
			int curpage = (int)ViewState["curpage"];
			if (curpage > 1)
			{
				curpage -= 1;
				ViewState["curpage"] = curpage;
				BindList(DataList1.DataKeys[0].ToString(),ScrollMode.UpdatePrevPage);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Al Alberto
Software Developer (Senior)
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 23 Jun 2003
Article Copyright 2003 by Al Alberto
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid