I have a dropdown menu ddlInstructorAllocated in my asp.net web form which is getting all its items in the list from the database. I have inserted another item to be displayed as "Please Select Instructor" so users know what to do. When this form is saved, it populates the information entered to the database. The column that corresponds to ddlInstructorAllocated is not a mandatory field and can be left as NULL. What I'm trying to do is when "Please Select Instructor" is selected by default, its value to be NULL. I've tried many ways but can't seem to figure it out. below is one of the ways i tried. Please help. For the instances when an instructor is selected from the list, it's ID is returned, converted to a string and then saved into the DB.
private void DisplayInstructors()
{
ListItem li;
li = new ListItem("Please Select Instructor");
li.Value = null;
ddlInstructorAllocated.Items.Add(li);
var instructors = EmployeeRepository.GetInstructors();
foreach (var i in instructors)
{
ddlInstructorAllocated.Items.Add(new ListItem(string.Format("{0} {1}", i.FirstName, i.LastName), i.EmployeeID.ToString()));
}
}
The ID in the database is stored as int and i'm doing EmployeeID = int.Parse(ddlInstructorAllocated.SelectedValue) later on, i am getting an exception that input string was not in a correct format