I have comboboxes where at any given point, no selection is necessary, so it is left blank. All my comboboxes have nullable ID's that use a picklist item.
example: Upon form load, all my comboboxes are blank (no value until one is selected). When I skip my cboCaseStatus (unselected), no error occurs, but upon Save, it returns the last item in the list by default. It never leaves it null. I do check for null in my app.
My cbo drop down list has: investigation, court case, grand jury, closed. When nothing is selected, it returns Closed by default, when database should return null (keep combobx empty)
In my form I have:
Div
private void btnSave_Click(object sender, EventArgs e) { CaseRecord.PersonLast = tbPersonLast.Text;
CaseRecord.PersonFirst = tbPersonFirst.Text;
CaseRecord.CaseStatusID = cboCaseStatus.SelectedPicklistNullableID();
//this is a required combobox field, vaildation works fine.
CaseRecord.CaseTypeID = cboCaseType.SelectedPicklistID();
}.
//My combobox extension class has:
public static int? SelectedPicklistNullableID(this ComboBox cb) { if (cb.SelectedItem == null) return null; return ((PickListItem)cb.SelectedItem).ID; } public static int SelectedPicklistID(this ComboBox cb) { return cb.SelectedPicklistNullableID().Value; }
In my DAL and BLL clases, I have nullable comboboxID's for the nullable comboboxes.
How do I get my comboboxes to return null, when no item is selected. Thank you.