Link to home
Start Free TrialLog in
Avatar of Johny Bravo
Johny Bravo

asked on

bind Listbox in asp.net

Hi Experts,
I am binding a ListBox using below code,
 private void FillRole()
        {
            SkillUpdateRight skUpdateRights = new SkillUpdateRight();
            Dictionary<object, object> dictRole = skUpdateRights.GetAllRole();
            lstRole.DataSource = dictRole;
            lstRole.DataTextField = "Value";
            lstRole.DataValueField = "Key";
            lstRole.DataBind();
      }


skUpdateRights.GetAllRole()

 public Dictionary<object, object> GetAllRole()
        {
            Dictionary<object, object> DictRole = new Dictionary<object, object>();
            //IDataReader dr = DBDataHandler.ExecuteReader(strCon, WDProcedures.GET_ALL_RESOURCES, null);

            IDataReader dr = DBDataHandler.ExecuteReader(strCon, WDProcedures.GET_ROLESFORSKILLRIGHTS, null);
            while (dr.Read())
            {
                if (DictRole == null)
                {
                    DictRole = new Dictionary<object, object>();
                    DictRole.Add(dr["RoleId"].ToString(), dr["RoleName"].ToString());
                }
                else
                {
                    if (!DictRole.ContainsKey(dr["RoleId"].ToString()))
                    {
                        DictRole.Add(dr["RoleId"].ToString(), dr["RoleName"].ToString());
                    }
                }
            }
            dr.Close();

            return DictRole;
        }



Now I have a sql ,

Select RoleId, RoleName, 'false' AS RoleSelected from Role
UNION ALL
Select RoleId, RoleName, 'true' AS RoleSelected from Role
WHERE RoleID IN
(Select lr.RoleId from SkillUpdateRights lr
inner join Role r on r.RoleId = lr.RoleId)

I want to check the RoleSelected is true or false and accordingly bind the ListBox.
If the RoleSelected is true,I will set the item in the list as Selected otherwise it will just appear in the Listbox.

How can I acheive this functionality.
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Hi,

In your SQL, you could create a third column "IsRoleSelected" as a bit data type for example and use the value of this column (0 or 1) to determine what to do each row of your Role table.
ASKER CERTIFIED SOLUTION
Avatar of Alfred A.
Alfred A.
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Johny Bravo
Johny Bravo

ASKER

Thanks for the reply.
I want unique RoleID,Rolename.
If it is selected ,them true otherwise false.

Only true or false,not both
RoleID  RoleName  
  1          TestRole     'true'

One more problem is that,how to bind this to Listbox depending on the True,false.
Currently I found one way to set the selected value in the Listbox.It directly set the selected property as true.
Which I want dynamic.I mean accrding to value the query returns (true/false)

 SkillUpdateRight skUpdateRights = new SkillUpdateRight();
            Dictionary<object, object> dictRoleSelected = skUpdateRights.GetSelectedRole();
            foreach (KeyValuePair<object, object> kvp in dictRoleSelected)
            {
                ListItem li = new ListItem(kvp.Value.ToString(), kvp.Key.ToString());
                li.Selected = true;
                lstRole.Items.Add(li);
            }
Thanks for the suggestion