Sunday, 3 March 2013

How to change user role using asp.net


For this you have to fallow below steps

Step 1:
 Make one “XML File “ which contains role types. In this i took one xml file and named it as
“chengerole.xml”.
<?xml version="1.0" encoding="utf-8" ?>
<changerole>
  <roles>
    <role>admin</role>
  </roles>
  <roles>
    <role>accountent</role>
  </roles>
  <roles>
    <role>teacher</role>
  </roles>
  <roles>
    <role>student</role>
  </roles>
  <roles>
    <role>parent</role>
  </roles>
  
</changerole>


Step 2:

Create a table as
create table userstable
(
UserName  nvarchar(50) not null,
Roletype nvarchar(50) not null
)

This table values will be added while the user registration: for understanding add some values to

this table

User NameRole
srinu.munagala33@gmail.comadmin
krishnareddy4b8@gmail.comaccountent
srinu33aryan@gmail.comteacher


Step 3:

Create one stored Procedure as
CREATE PROCEDURE rolechange 
@username nvarChar(50),
@role nvarChar(50)
    AS
    update usertable set Roletype=@role where UserName=@username

Step 4:
set the sqlconnection in webconfig file.

<connectionStrings>
    <add name="mycon" connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=
|DataDirectory|\SchoolManagementSystemdatabase.mdf;Integrated 
Security=True;
User Instance=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>


Step 5:
 Create one aspx file as below

<h3>Chenge Role</h3>
    <br />
    <asp:Table runat="server" ID="tblchangerole" BorderStyle="Solid"  
Height="200px"
 Width="300px" BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" >
    <asp:TableRow runat="server" ID="row1">
    <asp:TableCell>
    UserName:
    </asp:TableCell>
    <asp:TableCell>
    <asp:DropDownList ID="DropDownList1" runat="server" Width="160px"  
AutoPostBack="true" >
    </asp:DropDownList>
    </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow runat="server" ID="row2">
    <asp:TableCell>
    Change Role:
    </asp:TableCell>
    <asp:TableCell>
    <asp:DropDownList runat="server" ID="drdprolechange" Width="160px" 
 AutoPostBack
 ="true">
    </asp:DropDownList>
    </asp:TableCell>
    </asp:TableRow>
    <asp:TableRow runat="server" ID="row3">
    
    <asp:TableCell>
    
    </asp:TableCell>
    <asp:TableCell>
    <asp:Button ID="Button1" runat="server" Text="Change Role"  OnClick=
"Button1_Click"/>
    
    </asp:TableCell>
    </asp:TableRow>
    </asp:Table>
    
   
    
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    
Step 6:
Code Behind code:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;

namespace SchoolManagementSystem.Admin
{
    public partial class changerole : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            intiatepage();
            
            usernamebind();
            Bindrolestodropdownlist();
            Page.Header.Title = "Changerole";
            if (!IsPostBack)
            {

                if (Session["adminALoginId"] == null)
                {

                    Response.Redirect(@"~\Admin\Adminlogin.aspx");
                }

                else
                {

                    Response.ClearHeaders();

 Response.AddHeader("Cache-Control", "no-cache, no-store, max-age=0, 
must-revalidate");



                }

            }
            
        }

        public void intiatepage()
        {
            
        }

        public void Bindrolestodropdownlist()
        {
            if (!IsPostBack)
            {
                XmlReader xmrd = XmlReader.Create(Server.MapPath(@"~\chengerole.
xml"));
                DataSet dt = new DataSet();
                dt.ReadXml(xmrd);
                xmrd.Close();
                drdprolechange.DataValueField = "role";
                drdprolechange.DataTextField = "role";
                drdprolechange.DataSource = dt;
                drdprolechange.DataBind();


            }

        }

        public void usernamebind()
        {
            if (!IsPostBack)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.
ConnectionStrings["mycon"].ConnectionString);
                con.Open();
                SqlCommand cmm = new SqlCommand("select * from usertable", con);
                SqlDataAdapter adp = new SqlDataAdapter(cmm);
                DataSet dt = new DataSet();
                adp.Fill(dt);
                DropDownList1.DataTextField = dt.Tables[0].Columns["UserName"].
ToString();
                DropDownList1.DataValueField = dt.Tables[0].Columns["UserName"].
ToString();

                DropDownList1.DataSource = dt.Tables[0];
                
                
                DropDownList1.DataBind();
                con.Close();
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection(ConfigurationManager.
ConnectionStrings["mycon"].ConnectionString);
            cnn.Open();
            SqlCommand cmm = new SqlCommand("rolechange", cnn);
            cmm.CommandType = CommandType.StoredProcedure;
            cmm.Parameters.Add("@username", SqlDbType.NVarChar).Value = 
DropDownList1.SelectedValue.ToString();
            cmm.Parameters.Add("@role", SqlDbType.NVarChar).Value = 
drdprolechange.SelectedValue.ToString();
            cmm.ExecuteNonQuery();
            Label1.Text = "sucessfull";
            cnn.Close();
        }

       

    }
}


No comments:

Post a Comment