i am trying to bind value as select in dropdown.but getting error i have one dropdown
Type<asp:DropDownList ID="ddlSurf" runat="server" Style="border: 0px;" ></asp:DropDownList>
which has value bind on page load.
void GetSurf()
{
dt = conn.GetData("select product from group a where formname in('a','b','c','d','e')");
ddlSurf.DataSource = dt;
ddlSurf.DataTextField = "product";
ddlSurf.DataValueField = "product";
ddlSurf.DataBind();
ddlSurf.Items.Insert(0, "Select");
}
if do not select any value while submitting then value 'select' will be saved in database
SURF = ddlSurf.Text;
but while retrieving value from database i am simply doing
ddlSurf.SelectedValue = ReadData["surf"].ToString();
but this will create one error while binding from database "'ddlSurf' has a SelectedValue which is invalid because it does not exist in the list of items.
How to resolve this issue any idea would be appreciated.
try this one, for sure
int index = DropDownList1.Items.IndexOf(ReadData["surf"].ToString());
if (index != -1)
{
DropDownList1.SelectedIndex = index;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.