Search code examples
asp.netlinqdrop-down-menurights

values in dropdown according to rights


I have login and webform1.aspx

values in dropdown

**values** 
apples
oranges
grapes
factory
juices
banana
Grapes

i want when i login with username admin and password then values in dropdown must be factory only where as when i login with other than this admin username then want to show other values except factory

and also when i login with another username then values are in dropdown but not complete list only few values are display where as i want all values except factory

i try this sp and this sp works perfect

ALTER procedure [dbo].[list]
as
select fruits from tblReg
where fruits <>'' and 
fruits not in ('Factory')
and fruits  not like '%[0-9]%'
group by fruits 

and i convert this sp in linq 2nd query

on login form i do this

button click

            try
            {
                loginmethod(txt_us.Text, txt_pwd.Text);
                Response.Redirect("WebForm1.aspx");
                Session["UserName"] = txt_us.Text;
            }
            catch
            {
                Label1.Text = ("");
                Label1.Visible = false;
            }

login method

  private bool loginmethod(string UserName, string Password)
    {

       Entities2 td = new Entities2();
        splogin_Result sp = td.splogin(UserName, Password).FirstOrDefault();
        if (sp.Password == txt_pwd.Text)
        {
            return true;
        }
        else
        {
            return false;
        }

  }

and on webform page load

 protected void Page_Load(object sender, EventArgs e)
        {
            Entities2 tea = new Entities2(); 
            if (!Page.IsPostBack)
            {
                if ((Session["UserName"] as string) == "admin")
                {
         //1st query 

regiondrop.DataSource = tea.tblReg.Where(x => x.Fruits== "Factory")
.Select(x => new { Fruits= x.Fruits, Value = x.Fruits}).Distinct()
 .ToList();

                }
                else
                { 
                 //2nd query 

          regiondrop.DataSource = tea.tblReg.AsEnumerable()
                               .Where(x => x.Fruits.All(char.IsLetter) &&
                                x.Fruits!= "" &&
                               x.Fruits!= "Factory")
                       .Select(x => new { Fruits=x.Fruits, Value=x.Fruits})
                       .Distinct().ToList();    
                }
                regiondrop.DataTextField = "Fruits";
                regiondrop.DataValueField = "Fruits";
                regiondrop.DataBind();
                Label4.Visible = false;

            }  

        }

Solution

  • Why don't you map table 'tblReg' with user role and pass Session["UserName"] or Session["role"] as parameter to SP and select values and display in ddl.Simply fetch data according to role.

    Updated

    Now add one column role(value = Admin or User) in tbllogin.

    select role, UserName , Password from tbllogin where UserName=@UserName and Password=@Password  
    

    Store role in session(Session["role"]).

    Now also add one column in tblReg and map with role Eg

    RegId   fruits        Role
      1      Mango        user
      2      Apple        Admin
      3      banana       Admin
    

    You need to pass sql parameter from code behind

    ALTER procedure [dbo].[list]
    @role varchar(10)
    as
    select fruits from tblReg
    where role = @role
    

    Finally bind in code without checking role again.