Click here to Skip to main content
15,886,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When user selects the initial value Please Select I get an exception which is.
Conversion failed when converting the nvarchar value 'Please Select' to data type int.
The Initial value has to be added programmatically
How do I fix the error,below is my code which works correctly except if the initial value is selected.
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
        string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
        using(SqlConnection connection=new SqlConnection(cn))
        {
           
            SqlCommand command = new SqlCommand("select * from Employees",connection);
            connection.Open();
            //command.Parameters.AddWithValue("@id",DropDownList1.SelectedValue);
            SqlDataReader dr = command.ExecuteReader();
            DropDownList1.DataTextField = "name";
            DropDownList1.DataValueField = "id";
            DropDownList1.DataSource = dr;
            DropDownList1.DataBind();
            ListItem li = new ListItem("Please Select");
            DropDownList1.Items.Insert(0, li); 
        }
    }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(cn))
        {
            SqlCommand command = new SqlCommand("select * from Employees where id=@id", connection);
            connection.Open();
            command.Parameters.AddWithValue("@id",DropDownList1.SelectedValue);
            SqlDataReader dr = command.ExecuteReader();
            GridView1.DataSource = dr;
            GridView1.DataBind();

        }
    }
Posted
Updated 17-Jun-13 7:09am
v4
Comments
Ariana Bond 17-Jun-13 12:59pm    
When you are adding initial value to dropdownlist typecast error occurred, try to the replace initial value add code as follows--
DropDownList1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Please Select", "0"));
Member 9176543 18-Jun-13 1:43am    
And if a user selects the initial value then how to
Display a messsge ,"No Record Selected"
Thanks7872 18-Jun-13 1:47am    
To display message,set EmptyDataText="No Record Selected" in gridview definition.
Member 9176543 18-Jun-13 2:09am    
Thanks Ariana and Rohan.

1 solution

Bind your dropdown like

C#
SqlCommand command = new SqlCommand("select * from Employees",connection);
connection.Open();
SqlDataAdapter da=new SqlDataAdapter(command);
DataSet ds=new DataSet();
da.fill(ds);
DropDownList1.DataSource =ds; 
DropDownList1.DataBind(); 


in if(!Page.IsPostBack) as well as DropDownList1_SelectedIndexChanged

and define selected value of dropdown in definition itself. No need to make it in this code.

Regards.. :)
 
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