Cannot have multiple items selected in a DropDownList.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Cannot have multiple items selected in a DropDownList.
I have a nested gridview and inisde a child grid there are several columns having dropdown
and one add button. when add button is click a new row inside a child grid is created with an empty data
and previous row data have been saved in viewstate so that it can be bind wen add button is click to create new rows.
My code is working fine when i dont select any dropdown list item and new row is also created but wen i select items from dropdown and click on add more button i get above error.
Please help me out Mudassar sir and Azim sir.
When add button is click
protected void ButtonAdd_Click(object sender, EventArgs e)
{
Button gv = ((sender) as Button);
string[] ParentIdArray = gv.ClientID.ToString().Split('_');
int ParentId = int.Parse(ParentIdArray[1].ToString().Replace("ctl", "").Trim()) - 2;
int RecordCount = ((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).Rows.Count;
DataTable dtAddHotel = new DataTable();
dtAddHotel.Columns.Add("CostSheetId");
dtAddHotel.Columns.Add("HotelId");
dtAddHotel.Columns.Add("RoomId");
dtAddHotel.Columns.Add("CityId");
dtAddHotel.Columns.Add("StartDate");
dtAddHotel.Columns.Add("EndDate");
dtAddHotel.Columns.Add("SNGL");
dtAddHotel.Columns.Add("Child");
dtAddHotel.Columns.Add("Extra");
dtAddHotel.Columns.Add("MealID");
dtAddHotel.Columns.Add("ServiceTaxId");
dtAddHotel.Columns.Add("Preference");
DataRow dr = null;
if (RecordCount < 5)
{
for (int i = 0; i < RecordCount + 1; i++)
{
dr = dtAddHotel.NewRow();
if (i < RecordCount)
{
HiddenField hdnCity = (HiddenField)GVMain.Rows[ParentId].FindControl("HdnCityId");
dr["CityId"] = hdnCity.Value;
dr["StartDate"] = ((Label)((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).Rows[i].FindControl("LblTimePeriodStartDate")).Text;
dr["EndDate"] = ((Label)((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).Rows[i].FindControl("LblTimePeriodEndDate")).Text;
dr["HotelId"] = ((DropDownList)((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).Rows[i].FindControl("ddlAddHotel")).SelectedValue;
dr["RoomId"] = ((DropDownList)((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).Rows[i].FindControl("ddlAddRoomType")).SelectedValue;
dr["SNGL"] = ((TextBox)((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).Rows[i].FindControl("txtSNGL")).Text;
dr["Child"] = ((TextBox)((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).Rows[i].FindControl("txtChild")).Text;
dr["Extra"] = ((TextBox)((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).Rows[i].FindControl("txtExtra")).Text;
dr["MealID"] = ((DropDownList)((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).Rows[i].FindControl("ddlMealPlan")).SelectedValue;
dr["ServiceTaxId"] = ((DropDownList)((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).Rows[i].FindControl("ddlvat")).SelectedValue;
dr["Preference"] = ((DropDownList)((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).Rows[i].FindControl("ddlOption")).SelectedValue;
}
else
{
dtMain = serviceLocatorbean.GetFromTblHotelRates(PackageId);
dr["StartDate"] = dtMain.Rows[0]["FromDate"].ToString();
dr["EndDate"] = dtMain.Rows[0]["ToDate"].ToString();
dr["CityId"] = dtMain.Rows[0]["CityId"].ToString();
}
dtAddHotel.Rows.Add(dr);
}
((object[])ViewState["AddHotel"]).SetValue(dtAddHotel, ParentId);
((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).DataSource = ((DataTable)((object[])ViewState["AddHotel"])[ParentId]);
((GridView)GVMain.Rows[ParentId].FindControl("grdAddHotel")).DataBind();
}
}