Click here to Skip to main content
15,741,811 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i add default value "select one" at zero index position when dropdownlist is having datatext field and datavalue field which is obtained from database througt dataset.
Posted
Comments
csharpbd 7-Jun-12 15:28pm    
both solution are good...
but i think solution 1 is the best 4 u......

you can add by using the query : you can add on pageload event of page.
but add in !ispostback event.

like.....
C#
protected void Page_Load(object sender, EventArgs e)
 {
    

     if (!IsPostBack)
     {
         //  you must set the connection first.
        // e.g c.setcon(); // here this is method for setting a connection
         DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("Select field1  FROM Table", connection object);
        myda.Fill(ds);
        DropDown1.DataSource = ds;
        DropDown1.DataValueField = "field1";
        DropDown1.DataBind();
        DropDown1.Items.Insert(0, new ListItem("Select One", "0"));
         
     }
    
 }



hope this will help you, if not then dear please post it.
 
Share this answer
 
v3
Comments
subin joseph 8-Jun-12 1:26am    
thanks for the help.i have spend one day my self to solve this.but it just difficult because of dropdown having datatext field and datavalue field.once again thanks.
[no name] 8-Jun-12 6:56am    
most welcome dear.
>One way to do this is to use UNION clause on the query you use to populate your dropdown with.

For e.g.

select 0, "select one"
UNION
select colA, colB from tableA
 
Share this answer
 
v2
Comments
subin joseph 8-Jun-12 1:14am    
i am still getting an error message like this " Invalid column name 'select' ".
my stored procedure query is
select 0,"select" UNION select ste_stateid,ste_statename from bqs_state where ste_countryid=@countryid
subin joseph 8-Jun-12 3:01am    
i have just got the output .i put ' ' instead of " ".thanks

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