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(); }
|
Ashley JacksonPosted Feb 17, 2011, 6:43 AM
First set the display member of the List box:
Then populate the list (for this example I am just manually adding items)
listBox1.Items.Add(new person {Name="ash", StaffId = 01});
listBox1.Items.Add(new person { Name = "John", StaffId = 02 });
listBox1.Items.Add(new person { Name = "Jane", StaffId = 03 });
listBox1.Items.Add(new person { Name = "Mary", StaffId = 05 });
listBox2.Items.Add(new person { Name = "Jane", StaffId = 03 });
listBox2.Items.Add(new person { Name = "Mary", StaffId = 04 });
NOTE: In listbox 2 Mary has a different Staff Id so is ot a dupicate.
Then to check for duplicates do this:
if(listBox1.SelectedIndex > -1)
{
bool found=false;
foreach (person per in listBox2.Items)
{
if (per.StaffId == ((person)listBox1.SelectedItem).StaffId)
{
found = true;
}
}
if (found)
{
MessageBox.Show("Duplicate Found");
}
else
{
MessageBox.Show("Add Ok");
}
}
Let me know if this resolves everything. I have a dummy vs2010 project I can upload or email to you if you want it.
Ash...
Ashley JacksonPosted Feb 17, 2011, 6:16 AM
if(listBox1.SelectedIndex > -1)
{
if (listBox2.FindString(listBox1.SelectedItem.ToString()) > -1)
{
MessageBox.Show("Duplicate Found");
}
else
{
MessageBox.Show("OK Add It!");
}
}
Let mek now how that is.
Ash...
Syed ShakeerPosted Feb 17, 2011, 5:37 AM
Check the this link.http://stackoverflow.com/questions/1137083/c-how-do-i-select-a-list-box-item-when-i-have-the-value-name-in-a-string
lauren brownPosted Feb 17, 2011, 5:16 AM
Ashley JacksonPosted Feb 17, 2011, 4:07 AM
The issue is caused by you checkig if a list item from List A is in List B. The list item will not be in both list, but the value of that item may be.
Try something along the linex of:
if (ListBox1.SelectedIndex > -1) // Something Selected
{
if (ListBox2.Items.FindByValue(ListBox1.SelectedValue) != null)
{
// Output Error Message
}
else
{
// Process selected entry
}
}
Let me know how you get on withthis.
Ash....