Hi all

I am a little confused with an issue regarding a combo box being displayed on my asp.net webpage.

All I want to do it have a blank default value at the top of a list which is bound with a database, which also prevents the user from selecting it, for example "--Select One--"

I have included the following code in the DropDownList asp code:

<asp:ListItem Text="--Select One--" Value="0"/>

I have also set AppendDataBoundItems property to true (as the combobox AutoPostBack property is true) as well as the CausesValidation being set to true.

However my problem is, this does display but when this blank value "--Select One--" is chosen, the page errors because obviously there isn't a value "Select One"

Server Error in '/' Application.

Conversion failed when converting the varchar value '--Select One--' to data type int. 
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.Data.SqlClient.SqlException: Conversion failed when converting the varchar value '--Select One--' to data type int.

I don't want anything to happen when the user selects the "--Select One--" option, as obviously this might happen accidenly when the user is using it.

Thanks in advance!

6 Years
Discussion Span
Last Post by katieJane

I'm assuming you are going to do this database wise. Enter a "--Select One--" in the database and assign the default value to show up as that.

<asp:DropDownList ID="StakedBy" Style="width: 205px;" runat="server" AutoPostBack="True"
                DataSourceID="SqlDataSourceStaker" DataTextField="SName" DataValueField="ID">
            <asp:SqlDataSource ID="SqlDataSourceStaker" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                SelectCommand="SELECT [ID], [FName] + ' ' + [LName] as SName FROM [Work_Staker] order by FName">

if your not doing it database wise try this.

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
            <asp:ListItem Selected="True" Value="0">--Select One--</asp:ListItem>

Hi Thanks for the response!

I am doing it via a database so I was able to do it with your first suggestion by entering a row in the database with null values in each column.

Many thanks!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.