Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I've created a Dictionary like

C#
Dictionary<department,bool> dict= new Dictionary<department,bool>();


here Department is a class and I have `DepartmentId,Name and Code` for the departments. And in the `bool` am sending whether the person is `HOD` or not.

Am adding the records to this Dictionary like

C#
dict.Add(department,chkHOD.checked);


here the records are successfully added to the Dictionary and after this am binding the Dictionary to a GridView like

C#
gridDept.Datasource=dict;
  gridDept.Databind();


now the inserted records are displayed fine in the gridview. After this am storing this records in the 'StaffDepartments' table in my database. I have 3 columns in the 'Staffdepartments' table

1.StaffId(PK - has link with the Staff table)<br />
 2.DepartmentId(PK - has link with the Department table)<br />
 3.IsHOD.


here the records are stored fine in the database.No problem in adding the records into the database.

I have some questions here

****1.How can check whether the DepartmentId is already there in the Dictionary before adding to it.
2.When am editing the staff detail how can I delete the Selected Department from the Dictionary by checking the checkbox in Gridview rows.(here the records are coming from the database, so when I click delete button the records should be deleted in the database as well)****

if its a List instead of Dictionary, I can get the DepartmentId by

C#
int departmentId = (int)gridDept.DataKeys[row.RowIndex].Values["DepartmentId"];


but in Dictionary i dunno how to do the same with Key and Value pairs....can anyone help me here.
Posted
Updated 17-Apr-12 1:34am
v2
Comments
abdul samathu 17-Apr-12 7:49am    
thanks Uday
Mohamed Mitwalli 17-Apr-12 9:08am    
Hi ,
1.How can check whether the DepartmentId is already there in the Dictionary before adding to it.
for This one i believe you have loop through Dictionary and compare the key and value with the one your trying to add .
2. For second one , It has to be some unique value to compare it with and if it true you removed from Dictionary and Database .
abdul samathu 17-Apr-12 10:49am    
thanks Mohamed Mitwalli, I dunno how to do this

1 solution

Hi,

What you will need to do is to query your dictionary using LINQ

C#
List<dict> mylist = new List<dict>
mylist = dict.where(x=>x.value.deptID=yourdepID).tolist();
if(mylist.count>0)
{
// value exists
// to update you will need to create a new entry
// delete the old one
// insert the new value
}
else
{
//insert new
}</dict></dict>


The syntax may not be 100% correct as I cannot test at the moment.

Hope this helps you.
 
Share this answer
 
Comments
abdul samathu 18-Apr-12 0:36am    
if I tried to create a List with dict, as u suggested .....it reports error like "dict is a field, but is used like a field"

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