Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlCommand cmdPickAllAccessebility = DBManager.DataAccess.command();
            
cmdPickAllAccessebility.Parameters.Add(new SqlParameter("@option", SqlDbType.VarChar, 50));
cmdPickAllAccessebility.Parameters["@option"].Value = "PickAllAccessebility";

cmdPickAllAccessebility.Parameters.Add(new SqlParameter("@User_identity", SqlDbType.VarChar, 50));
cmdPickAllAccessebility.Parameters["@User_identity"].Value = rbtnForSelectUser.SelectedValue.ToString();//rbtnForSelectUser.SelectedValue //Session["User_ID"];

string sqlquery2 = "SchoolProc";
DataSet dsetPickAllAccessebility = DBManager.DataAccess.getdata(sqlquery2);

for (int i = 0; i < CheckBoxListForSelectingPrivileges.Items.Count; i++)
{
for (int j = 0; j < dsetPickAllAccessebility.Tables[0].Rows.Count; j++)
{
if (dsetPickAllAccessebility.Tables[0].Rows[j].ToString() == CheckBoxListForSelectingPrivileges.Items[i].Value)
{
CheckBoxListForSelectingPrivileges.Items[i].Selected = true;
}
else
{
CheckBoxListForSelectingPrivileges.Items[i].Selected = false;
}

}
}

this was my code for retrieving the data from database and checked the check box list
but i am not getting any output
i will clearly explain about my problem

i am having a page called administration page.in that page we can see all the user in radio button list(binded all the users to radio button list) and all the privileges in check box list (binded all the privileges to check box list list)

by selecting particular user and selecting particular privileges to that user by checking the check box list that will save in table "User_Access" like below

registration table
user_id | login_id | password 
1       |nitish    | ******

privileges table
access_id | Description | status
1         | admission   |  true
2         | events      |  true
3         |fee-collection| true

User_Access table
user_id |access_id
1       | 2
1       | 3

like the above table i am saving....

what was my problem if next time i selected the same user i want to see the privileges what all ready he is having by retrieving the data from User_Access table

here i am using access_id as data value field for check box list.
in data set i am getting the access_id called as the data value fields of check box list
now what i want is according to the user_access table when we select user_id 1 in admin page it should show the check box list with check box 2,3 should be checked and 1 should be unchecked
Posted
Updated 11-Feb-13 22:01pm
v4
Comments
Maciej Los 12-Feb-13 3:12am    
@ntitish,
Do not repost and do not remove questions with answers (as you've done with your previous question and my answer).
Here was my answer: http://www.codeproject.com/Answers/544637/howplustopluscheckplusthepluscheckboxpluslistplusb
ntitish 12-Feb-13 3:14am    
sir it was a mistakenly deleted thats why i am again posting....i am really sorry for what i did ...i not even seen the answer of that question....can u send me again if u dont mind...
ntitish 12-Feb-13 3:16am    
sir sorry,actually after clicking on that link it is showing my previous question with out answer......
Maciej Los 12-Feb-13 3:21am    
I see only: Sorry, the item you requested could not be found. Question and answer doesn't exists!

OK, next time you should be more careful ;)
ntitish 12-Feb-13 3:23am    
sure sir....are you sending again that answer...

First of all, read my comment.

If i understood you well and dsetPickAllAccessebility.Tables[0].Rows is equal to CheckBoxListForSelectingPrivileges.Items.Count, you should change the Selected property based on dsetPickAllAccessebility.Tables[i].Rows[N].Item["status"] value.
C#
for (int j = 0; j < dsetPickAllAccessebility.Tables[0].Rows.Count; j++)
{
    CheckBoxListForSelectingPrivileges.Items[j].Selected = dsetPickAllAccessebility.Tables[0].Rows[j].Item["status"].Value;
}

You don't need the second loop.
 
Share this answer
 
Comments
ntitish 12-Feb-13 3:41am    
sir dsetPickAllAccessebility.Tables[0].Rows is not equal to CheckBoxListForSelectingPrivileges.Items.Count
because in user_access table i am saving only checked check boxes data value field but i am not saving unchecked data value field..
ntitish 12-Feb-13 3:53am    
is there will be any solution for this sir.
Maciej Los 12-Feb-13 4:04am    
Yes, it is. You need to write code based on your criteria. I'll update my answer.
ntitish 12-Feb-13 4:08am    
ok sir...
ntitish 12-Feb-13 4:20am    
sir while saving the checked check boxes i am saving only data value field not saving the status. user_access table u can see above.
Hi Dear

Plz try this.
C#
string userId = Session["UserId"].ToString();
            foreach (ListItem item in this.chkList.Items)
            {
                if (item.Value != userId)
                {
                    item.Selected = true;
                }
            }
 
Share this answer
 
Comments
ntitish 13-Feb-13 7:24am    
sir here i am taking the user_id by selecting the radio button.....not by session[user_id]
Try this:
C#
//Clear all the selected items
for (int i = 0; i < CheckBoxListForSelectingPrivileges.Items.Count; i++)
{
CheckBoxListForSelectingPrivileges.Items[i].Selected = false;
}
//Iterate all the items and compare them to the returned result set
for (int i = 0; i < CheckBoxListForSelectingPrivileges.Items.Count; i++)
{
for (int j = 0; j < dsetPickAllAccessebility.Tables[0].Rows.Count; j++)
{
if (dsetPickAllAccessebility.Tables[0].Rows[j]["columnNameOrIndex"].ToString() == CheckBoxListForSelectingPrivileges.Items[i].Value)
{
CheckBoxListForSelectingPrivileges.Items[i].Selected = true;
break;//break the loop if item is matched.
}
//No need for an else block.
}
}
 
Share this answer
 
Comments
ntitish 14-Feb-13 6:39am    
thanks sir.......
Zafar Sultan 14-Feb-13 10:40am    
Welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900