I have developed User Management Module in the web-based application that I am working on it. By using ASP.NET Wizard control, it is developed to be worked as following: First Step: it contains a TextBox for entering the username of employee Second Step: it shows the information of the employee Third Step: giving the user the role to access the system
If the user is not in the database, it will be added to the system after specifying a role for him and click on the Finish button. What I want now is the following: In the second step, when the user information be displayed, I want to put two options: Delete
or Add/Edit Role
Delete will be enabled, if he is existed in the system
Add will be enabled all the time, for editing the role for the user
protected void Page_Load(object sender, EventArgs e)
{
//Set the Wizard Step 0 as the initial wizard step when the page loads
if (!Page.IsPostBack)
{
Wizard1.ActiveStepIndex = 0;
}
//For sending object to the Wizard1.PreRender
Wizard1.PreRender += new EventHandler(Wizard1_PreRender);
}
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
//If one of the items is selected AND a username exists in the Username session object update the user role
string username = TextBox1.Text;
if (!String.IsNullOrEmpty(radio1.SelectedValue) && !String.IsNullOrEmpty(username))
{
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
//This for adding the new PMOD user to the system
string insertUserCommand = "INSERT INTO employee (Name, Username, JobTitle, BadgeNo, EmpOrgType, DivisionCode) values (@Name, @Username, @JobTitle, @BadgeNo, @EmpOrgType, @DivisionCode)";
string cmdText = "SELECT Count(*) FROM employee WHERE Username = '" + username + "'";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
// Open DB connection.
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
if ((int)cmd.ExecuteScalar() == 0){
//An object from ObjectUser class to get the user information from the Secure system and insert them to the database
ObjectUser user = new ObjectUser(username,true);
SqlCommand cmd2 = new SqlCommand(insertUserCommand, conn);
cmd2.Parameters.AddWithValue("@Name", user.Name);
cmd2.Parameters.AddWithValue("@Username", username);
cmd2.Parameters.AddWithValue("@JobTitle", user.GetProperty("EMP_TITLE"));
cmd2.Parameters.AddWithValue("@BadgeNo", user.GetProperty("EMP_BADGE_NUMBER"));
cmd2.Parameters.AddWithValue("@EmpOrgType", user.GetProperty("EMP_EMPTYPE"));
cmd2.Parameters.AddWithValue("@DivisionCode", user.Org.Division.SapCode);
cmd2.ExecuteNonQuery();
}
}
}
//For updating the role of the user by deleting its current role and inserting a new role
string deleteCommand = "DELETE FROM UserRole where Username=@Username";
string insertCommand = "INSERT INTO UserRole (RoleID,Username) values(@RoleID,@Username)";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
//using (SqlCommand cmd = new SqlCommand(cmdText, conn))
using (SqlCommand cmd = new SqlCommand(deleteCommand, conn))
{
cmd.Parameters.AddWithValue("@Username", username);
cmd.ExecuteNonQuery();
//Now the insert
cmd.CommandText = insertCommand;
cmd.Parameters.Clear(); //need this because still has params from del comm
cmd.Parameters.AddWithValue("@RoleID", radio1.SelectedValue);
cmd.Parameters.AddWithValue("@Username", username);
cmd.ExecuteNonQuery();
//infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
//cmd.ExecuteScalar();
//infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue);
}
}
Wizard1.Visible = false;
wizard.InnerHtml = @"<p><b>The task has been done successfully.</b> <br /> <a href='UserManagement.aspx'>Edit Another User</a></p>";
}
}
//Method for checking the existence of the username in the database (retrun true or false)
private bool CheckUsername(string username)
{
if (Service.GetPerson(username).GetProperty("RES_NETID").Equals("-"))
return false;
else if (Security.isPMODMember(username))
return true;
else
return false;
//string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
//string cmdText = "SELECT Count(*) FROM employee WHERE Username = '" + username + "'";
//using (SqlConnection conn = new SqlConnection(connString))
//{
// conn.Open();
// // Open DB connection.
// using (SqlCommand cmd = new SqlCommand(cmdText, conn))
// {
// int count = (int)cmd.ExecuteScalar();
// // True (> 0) when the username exists, false (= 0) when the username does not exist.
// return (count > 0);
// }
//}
}
protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
string username = TextBox1.Text;
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
switch (Wizard1.WizardSteps[e.NextStepIndex].ID)
{
case "WizardStep2":
//For checking the user
if (!String.IsNullOrEmpty(username) && CheckUsername(username))
{
try
{
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string cmdText = @"SELECT dbo.employee.Username, dbo.employee.Name, dbo.employee.JobTitle, dbo.employee.BadgeNo,
ISNULL(dbo.Roles.RoleID, 3) AS RoleID, dbo.Divisions.DivisionName, dbo.Roles.RoleName
FROM dbo.Divisions INNER JOIN dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode
LEFT OUTER JOIN dbo.Roles RIGHT OUTER JOIN dbo.UserRole ON dbo.Roles.RoleID = dbo.UserRole.RoleID ON
dbo.employee.Username = dbo.UserRole.Username
WHERE (dbo.employee.Username = @Username)";
SqlCommand myCommand = new SqlCommand(cmdText, conn);
myCommand.Parameters.AddWithValue("@Username", username);
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
adapter.Fill(table);
ObjectUser user = new ObjectUser(username, true);
string Name = user.Name;
string Username = user.ID;
string DivisionName = user.Org.Title;
string JobTitle = user.GetProperty("EMP_TITLE");
string BadgeNo = user.GetProperty("EMP_BADGE_NUMBER");
string role = "User";
string roleid = "4";
if (table.Rows.Count > 0)
{
role = table.Rows[0]["RoleName"] as string;
roleid = table.Rows[0]["RoleID"].ToString();
}
lblName.Text = Name;
lblUsername.Text = Username;
lblDivision.Text = DivisionName;
lblJobTitle.Text = JobTitle;
lblBadgeNo.Text = BadgeNo;
lblRole.Text = role;
radio1.SelectedValue = roleid;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
else
{
//If the user does not exist or a blank value has been entered
//Cancel the nextstep redirection and display an error message in a span
e.Cancel = true;
errorSpan.InnerText = "The username specified is blank or does not belong to PMOD";
}
break;
case "WizardStep3":
break;
}
}
matrix388
Member
47 Points
92 Posts
How to put Delete option in this User Management Wizard?
Feb 13, 2012 05:55 AM|LINK
I have developed User Management Module in the web-based application that I am working on it. By using ASP.NET Wizard control, it is developed to be worked as following:
First Step: it contains a TextBox for entering the username of employee
Second Step: it shows the information of the employee
Third Step: giving the user the role to access the system
If the user is not in the database, it will be added to the system after specifying a role for him and click on the Finish button. What I want now is the following: In the second step, when the user information be displayed, I want to put two options: Delete or Add/Edit Role
Delete will be enabled, if he is existed in the system
Add will be enabled all the time, for editing the role for the user
So is there any way to do that?
My code in ASP.NET and C# is as following:
ASP.NET:
<div id="wizard" align="center" runat="server"> <asp:Wizard ID="Wizard1" runat="server" DisplaySideBar="False" Width="63%" ActiveStepIndex="2" OnNextButtonClick="Wizard1_NextButtonClick" OnFinishButtonClick="Wizard1_FinishButtonClick"> <WizardSteps> <asp:WizardStep ID="WizardStep1" runat="server" Title="Employee Username/Network ID"> <table border="0"> <tr> <td class="InputLabel"> Username: </td> <td class="InputControl"> <asp:TextBox ID="TextBox1" runat="server" /> </td> <td> <span id="errorSpan" runat="server" style="color:Red;"></span> </td> </tr> </table> </asp:WizardStep> <asp:WizardStep ID="WizardStep2" runat="server" Title="Manage User"> <div class="content"> <table> <tr> <td> Name: <asp:Label ID="lblName" runat="server"></asp:Label> </td> </tr> <tr> <td> Username: <asp:Label ID="lblUsername" runat="server"></asp:Label> </td> </tr> <tr> <td> Division: <asp:Label ID="lblDivision" runat="server"></asp:Label> </td> </tr> <tr> <td> Badge Number: <asp:Label ID="lblBadgeNo" runat="server"></asp:Label> </td> </tr> <tr> <td> Job Title: <asp:Label ID="lblJobTitle" runat="server"></asp:Label> </td> </tr> </table> </div> </asp:WizardStep> <asp:WizardStep ID="WizardStep3" runat="server" Title="Edit User Role"> <div class="content"> <table> <caption> Current Role: <asp:Label ID="lblRole" runat="server"></asp:Label> </caption> </tr> <tr> <asp:Label ID="Label1" runat="server" BackColor="#FFFF99" Font-Bold="True" ForeColor="#000099" /> <asp:RadioButtonList ID="radio1" runat="server" TextAlign="left" RepeatDirection="Horizontal" DataSourceID="SqlDataSource1" DataTextField="RoleName" DataValueField="RoleID"> <asp:ListItem id="option1" runat="server" Value="Admin" /> <asp:ListItem id="option2" runat="server" Value="Contribute" /> <asp:ListItem id="option3" runat="server" Value="Management" /> <asp:ListItem id="option4" runat="server" Value="User" /> </asp:RadioButtonList> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT * FROM [Roles]"></asp:SqlDataSource> <%--<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Clicked" /> <span id="infoSpan" runat="server" style="color:Red;"></span>--%> </tr> </table> </div> </asp:WizardStep> </WizardSteps> <HeaderTemplate> <ul id="wizHeader"> <asp:Repeater ID="SideBarList" runat="server"> <ItemTemplate> <li><a class="<%# GetClassForWizardStep(Container.DataItem) %>" title="<%#Eval("Name")%>"> <%# Eval("Name")%></a> </li> </ItemTemplate> </asp:Repeater> </ul> </HeaderTemplate> </asp:Wizard>C#:
protected void Page_Load(object sender, EventArgs e) { //Set the Wizard Step 0 as the initial wizard step when the page loads if (!Page.IsPostBack) { Wizard1.ActiveStepIndex = 0; } //For sending object to the Wizard1.PreRender Wizard1.PreRender += new EventHandler(Wizard1_PreRender); } protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e) { //If one of the items is selected AND a username exists in the Username session object update the user role string username = TextBox1.Text; if (!String.IsNullOrEmpty(radio1.SelectedValue) && !String.IsNullOrEmpty(username)) { string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True"; //This for adding the new PMOD user to the system string insertUserCommand = "INSERT INTO employee (Name, Username, JobTitle, BadgeNo, EmpOrgType, DivisionCode) values (@Name, @Username, @JobTitle, @BadgeNo, @EmpOrgType, @DivisionCode)"; string cmdText = "SELECT Count(*) FROM employee WHERE Username = '" + username + "'"; using (SqlConnection conn = new SqlConnection(connString)) { conn.Open(); // Open DB connection. using (SqlCommand cmd = new SqlCommand(cmdText, conn)) { if ((int)cmd.ExecuteScalar() == 0){ //An object from ObjectUser class to get the user information from the Secure system and insert them to the database ObjectUser user = new ObjectUser(username,true); SqlCommand cmd2 = new SqlCommand(insertUserCommand, conn); cmd2.Parameters.AddWithValue("@Name", user.Name); cmd2.Parameters.AddWithValue("@Username", username); cmd2.Parameters.AddWithValue("@JobTitle", user.GetProperty("EMP_TITLE")); cmd2.Parameters.AddWithValue("@BadgeNo", user.GetProperty("EMP_BADGE_NUMBER")); cmd2.Parameters.AddWithValue("@EmpOrgType", user.GetProperty("EMP_EMPTYPE")); cmd2.Parameters.AddWithValue("@DivisionCode", user.Org.Division.SapCode); cmd2.ExecuteNonQuery(); } } } //For updating the role of the user by deleting its current role and inserting a new role string deleteCommand = "DELETE FROM UserRole where Username=@Username"; string insertCommand = "INSERT INTO UserRole (RoleID,Username) values(@RoleID,@Username)"; using (SqlConnection conn = new SqlConnection(connString)) { conn.Open(); //using (SqlCommand cmd = new SqlCommand(cmdText, conn)) using (SqlCommand cmd = new SqlCommand(deleteCommand, conn)) { cmd.Parameters.AddWithValue("@Username", username); cmd.ExecuteNonQuery(); //Now the insert cmd.CommandText = insertCommand; cmd.Parameters.Clear(); //need this because still has params from del comm cmd.Parameters.AddWithValue("@RoleID", radio1.SelectedValue); cmd.Parameters.AddWithValue("@Username", username); cmd.ExecuteNonQuery(); //infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue); //cmd.ExecuteScalar(); //infoSpan.InnerText = String.Format("The users role has been updated to - {0}", radio1.SelectedValue); } } Wizard1.Visible = false; wizard.InnerHtml = @"<p><b>The task has been done successfully.</b> <br /> <a href='UserManagement.aspx'>Edit Another User</a></p>"; } } //Method for checking the existence of the username in the database (retrun true or false) private bool CheckUsername(string username) { if (Service.GetPerson(username).GetProperty("RES_NETID").Equals("-")) return false; else if (Security.isPMODMember(username)) return true; else return false; //string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True"; //string cmdText = "SELECT Count(*) FROM employee WHERE Username = '" + username + "'"; //using (SqlConnection conn = new SqlConnection(connString)) //{ // conn.Open(); // // Open DB connection. // using (SqlCommand cmd = new SqlCommand(cmdText, conn)) // { // int count = (int)cmd.ExecuteScalar(); // // True (> 0) when the username exists, false (= 0) when the username does not exist. // return (count > 0); // } //} } protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e) { string username = TextBox1.Text; string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True"; switch (Wizard1.WizardSteps[e.NextStepIndex].ID) { case "WizardStep2": //For checking the user if (!String.IsNullOrEmpty(username) && CheckUsername(username)) { try { SqlConnection conn = new SqlConnection(connString); conn.Open(); string cmdText = @"SELECT dbo.employee.Username, dbo.employee.Name, dbo.employee.JobTitle, dbo.employee.BadgeNo, ISNULL(dbo.Roles.RoleID, 3) AS RoleID, dbo.Divisions.DivisionName, dbo.Roles.RoleName FROM dbo.Divisions INNER JOIN dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode LEFT OUTER JOIN dbo.Roles RIGHT OUTER JOIN dbo.UserRole ON dbo.Roles.RoleID = dbo.UserRole.RoleID ON dbo.employee.Username = dbo.UserRole.Username WHERE (dbo.employee.Username = @Username)"; SqlCommand myCommand = new SqlCommand(cmdText, conn); myCommand.Parameters.AddWithValue("@Username", username); DataTable table = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter(myCommand); adapter.Fill(table); ObjectUser user = new ObjectUser(username, true); string Name = user.Name; string Username = user.ID; string DivisionName = user.Org.Title; string JobTitle = user.GetProperty("EMP_TITLE"); string BadgeNo = user.GetProperty("EMP_BADGE_NUMBER"); string role = "User"; string roleid = "4"; if (table.Rows.Count > 0) { role = table.Rows[0]["RoleName"] as string; roleid = table.Rows[0]["RoleID"].ToString(); } lblName.Text = Name; lblUsername.Text = Username; lblDivision.Text = DivisionName; lblJobTitle.Text = JobTitle; lblBadgeNo.Text = BadgeNo; lblRole.Text = role; radio1.SelectedValue = roleid; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } else { //If the user does not exist or a blank value has been entered //Cancel the nextstep redirection and display an error message in a span e.Cancel = true; errorSpan.InnerText = "The username specified is blank or does not belong to PMOD"; } break; case "WizardStep3": break; } }Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: How to put Delete option in this User Management Wizard?
Feb 15, 2012 07:47 AM|LINK
Hi,
You can handle the ActiveStepChanged event of Wizard control to check the user to disable or enable delete/add button.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework