I am trying to add an item and value to an asp.net dropdownlist control.
arrEmpName = HttpContext.Current.Session["EmployeeName"].ToString();
arrEmpID = HttpContext.Current.Session["EmpID"].ToString();
char[] separator = new char[] { ',' };
string[] sEmployeeName = arrEmployeeName.Split(separator);
string[] sEmpID = arrEmpID.Split(separator);
foreach (string s in sEmployeeName)
{
ddlEmp.Items.Add(new ListItem(sEmployeeName, sEmpID));
}
I get two error messages :
‘System.Web.UI.WebControls.ListItem.ListItem(string, string)’ has some
invalid arguments
and
Argument 1: cannot convert from 'string[]' to 'string'
Any ideas what the issue(s) would be ?
You’re trying to add the arrays as a whole to the ListItem value. I suspect they’re supposed to be linked:
You may also want to consider checking that they’re the same length:
EDIT: Could throw in some Linq if you want though not really necessary: