user6628729 user6628729 - 1 year ago 63
ASP.NET (C#) Question

values in dropdown according to rights

I have login and webform1.aspx

values in dropdown


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]
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

loginmethod(txt_us.Text, txt_pwd.Text);
Session["UserName"] = txt_us.Text;
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;
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()

//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})
regiondrop.DataTextField = "Fruits";
regiondrop.DataValueField = "Fruits";
Label4.Visible = false;



Answer Source

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.


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)
select fruits from tblReg
where role = @role

Finally bind in code without checking role again.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download