lauren brown

lauren brown

  • NA
  • 33
  • 92.9k

If Else Statement not working

Feb 17 2011 3:15 AM

Hi experts, I would like to add a staff from the "SearchLB" to "ListBox1". So for example, if I already have Staff A under "ListBox1" and I accidentally add similiar Staff A into "ListBox1", the system should prompt me with an error message, "Staff already existed.". However, my If Else statement does not seem to work.
Kindly please help me on it.
Thank you!
Below is the codes for my Confirm Button to add staff into "ListBox1".

//confirm selection to be included in stafflist

protected void ConfirmBtn_Click(object sender, EventArgs e)
{
int sel = 0;
for (int i = 0; i < SearchLB.Items.Count; i++)
{
if (SearchLB.Items[i].Selected == true )
{
sel++;
//
if (!ListBox1.Items.Contains(SearchLB.Items[i])) //&& ListBox1.DataValueField != ConfigurationSettings.AppSettings["STAFFID"]
{
SQL dbAdapter = new SQL();
dbAdapter.insertStafflist(SearchLB.SelectedValue, (
string)Session["userName"]);
}
else
{
MessageBox.Show("Staff already existed", "Error");
}
//if (ListBox1.DataValueField == ConfigurationSettings.AppSettings["STAFFID"])
//else
//{
// MessageBox.Show("Staff already existed", "Error!");
//}
}
}
if (sel == 0)
{
MessageBox.Show("There is no selected staff to be added.", "Error");
}

//generate listbox items
populateStafflist();
}

Answers (5)