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; public partial class SwitchRoles : System.Web.UI.Page { private void populateCombo(SqlDataSource ds, DropDownList comboName, string spName, string dataValueField, string dataTextField, string paramName, string paramValue) { ds.SelectCommandType = SqlDataSourceCommandType.StoredProcedure; ds.SelectCommand = spName; ds.SelectParameters.Clear(); // Declare a parameter & add it to the datasource //Parameter spParameter = new Parameter(paramName, TypeCode.Int32); //spParameter.DefaultValue = paramValue.ToString(); //ds.SelectParameters.Add(spParameter); // OR add the parameter directly ds.SelectParameters.Add(paramName, paramValue.ToString()); ds.SelectParameters[0].Direction = ParameterDirection.Input; comboName.DataSource = ds; comboName.DataValueField = dataValueField; comboName.DataTextField = dataTextField; comboName.DataBind(); comboName.Items.Insert(0, "Select One"); } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SqlDataSource ds = new SqlDataSource(); ds.ConnectionString = ConfigurationManager.ConnectionStrings["cnSqlRoleManager"].ToString(); ds.DataSourceMode = SqlDataSourceMode.DataSet; // Populate drop-downs populateCombo(ds, rolesDropDown, "aspnet_Roles_GetAllRoles", "RoleName", "RoleName", "ApplicationName", "/"); } } protected void switchButton_Click(object sender, EventArgs e) { string logonUserName = Utilities.GetLoggedInUserID(User.Identity.Name); MembershipUser user = Membership.GetUser(logonUserName); if (user != null) { //Add user to the role if (rolesDropDown.SelectedItem.ToString().ToUpper() != "SELECT ONE") { if (rolesDropDown.SelectedItem.ToString() != "ChairBoardPortfolio") { if (Roles.IsUserInRole(logonUserName, rolesDropDown.SelectedItem.ToString()) == false) { Roles.RemoveUserFromRoles(logonUserName, Roles.GetRolesForUser(logonUserName)); Roles.AddUserToRole(logonUserName, rolesDropDown.SelectedItem.ToString()); } } else { Roles.AddUserToRole(logonUserName, rolesDropDown.SelectedItem.ToString()); } } } } }