Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
VB
Dim sql1 As String = "select * from Office_Mast where Office_Cd='" & officeCd.Trim & "' order by Office_Name"
            Dim cmd3 As New SqlCommand(sql1, conn)
            Dim dr1 As SqlDataReader
            dr1 = cmd3.ExecuteReader
            ddl_editOffice.DataTextField = "Office_Name"
            ddl_editOffice.DataValueField = "Office_Cd"
            ddl_editOffice.DataBind()
            ddl_editOffice.Items.Insert(0, "--Select--")
            If dr1.IsClosed = False Then dr1.Close()
            ddl_editOffice.SelectedValue = officeCd



i get this error while execute
'ddl_editOffice' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
plz any one provide solution
Posted
Comments
ZurdoDev 25-Nov-13 19:04pm    
Because the value of officeCd is not in your list of items.

Here is one class i've provided, you just pass dropdown id, table name and table id

C#
public void PopulateDropDown(DropDownList ddlList, string tblName, string colName, string colValue, string srchCodn, Page form)
    {
        try
        {
            string sqlQuery = "Select 'Select' as " + colName + ",0 as " + colValue + " Union Select " + colName + "," + colValue + " from " + tblName;
            if (srchCodn != "")
            {
                sqlQuery += " " + srchCodn;
            }
            adp = new SqlDataAdapter(sqlQuery, conn);
            tbl = new DataTable();
            adp.Fill(tbl);
            ddlList.DataSource = tbl;
            ddlList.DataTextField = colName;
            ddlList.DataValueField = colValue;
            ddlList.DataBind();
        }
        catch (Exception ex)
        {
            MsgBox("Populate DropDown Error: " + ex.Message, form);
        }
    }


Hope this helps you to bind dropdown. Happy coding.
 
Share this answer
 
actually problem was that when i access value from database its having spaces after value so i use trim after received value and solve problem.
Thanks to all to provide me solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900