views:

143

answers:

3

I have a winform data entry and retrieval program called CaseNotes. This has a form that that fill in. On the form there are multiple dropdown,checkbox controls that I are data-bound to a tblCNMaintItem. The structure of that table is -->

ItemID | CategoryID | ItemDescription | OrderID | IsActive

There is a seperate Category table that provides that categoryID's. A CategoryID maps to a Single Control on the Case Notes Form.

My question is two-fold:

  1. How should I go about getting the value for each item? As in, I grab the itemDescription by CategoryID to populate the controls but on SaveNewCaseNote() I need to get the corresponding ItemID instead. How can I accomplish this? Should I create a dictonary for each category(Control)? Should I Enumerate the ItemID/ItemDescription combo?

  2. EDIT Forget #2 as I have found my answer.

Thanks!

EDIT

Okay, for some reason I have failed to state this clearly and it is generating confusion. My DB consists of 3 tables. tblCaseNotes, tblCNMaintCategory, tblCNMaintItem. CaseNotes contains a complete "Case Note" per row. The Maint tables are for the multiple choice answers when filling out a CaseNote. Example: They must select a "Contact Location". The options "Office" or "Member's Home". In tblCNMaintCategory there is a enntry like so-->

CategoryID = 3, CatgoryName = Contact Location, IsActive = True

In tblCNMaintItem there are 2 entries like so -->

ItemID=51, CategoryID=2, ItemDescription=Office, OrderID=0, IsActive=True ItemID=52, CategoryID=2, ItemDescription=Member's Home, OrderID=0, IsActive=True

In tblCaseNote there would be an entry like so --> CaseNoteID=3243, PersonID=454676, AssocContactLocations= 51, and then many more columns following same pattern

Question 1 is pertaining to wanting to store the ItemID in AssocContactLocation versus the ItemDescription. I am thinking a

Dictionary <strng, int> cLocateItems(itemDescription,itemID) maybe...

Does this help?

Thanks everyone for the time and effort!

+1  A: 

After your edit, I think you mean, "How can I display the text but still use the numeric ID when I save the data?"

If that is your question, then yes your dictionary idea would work.

It's been a long time since I've done any CRUD work, but I recall that you could store the ID in the drop down box along with the text. Then when you do your update, you would use the ID instead of the text. This option may be easier than your dictionary idea.

If I have still misunderstood your question, I guess someone else will have to help.

dss539
See edit above for clarification
Refracted Paladin
No you understood correctly. Maybe I have just overcomplicated it. I will give this a try and post back my efforts.Thanks!
Refracted Paladin
+1  A: 

I don't understand the first part, but the second part...

The best practice is not to store multiple values in a single column at all.

Instead you create a one-to-many reference to a second table containing all the values.

Lasse V. Karlsen
A: 

What is the underlying data source for CNMaintItem? If it is already in an ADO.NET DataTable then the solution is easy. When binding to your list control, set the DisplayMember property to "ItemDescription" and the DisplayValue to "ItemID", eg:

//dtItem is the DataTable
ddlItem.DataSource = dtItem;
ddlItem.DisplayMember = "ItemDescription";
ddlItem.ValueMember = "ItemID";

or better, for a strongly-typed DataTable:

ddlItem.DataSource = dtItem;
ddlItem.DisplayMember = dtItem.ItemDescriptionColumn.ColumnName;
ddlItem.ValueMember = dtItem.ItemIDColumn.ColumnName;

The above could be any kind of object collection as well. If you have a Dictionary you will need to assign the dictionary object it to a BindingSource object, then assign the BindingSource object to the DataSource property of the list control:

BindingSource bs = new BindingSource();
bs.DataSource = dictItem;
ddlItem.DataSource = bs;
ddlItem.DisplayMember = "Value";
ddlItem.ValueMember = "Key";

In all cases, at save time, the ItemID is available via SelectedValue:

int IdToSave = Convert.ToInt32(ddlItem.SelectedValue);
PaulR