Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I am using drop down list for department
my dept contains
1
2
3
departments

When i am using data source it is showing only 1 2 3 .
I want to display "select a dept value" as first one, how can I achieve that.


my code is:
XML
<asp:DropDownList ID="ddlDept" runat="server" DataSourceID="SqlDataSource1"
                    DataTextField="DeptID" DataValueField="DeptID" Font-Underline="False">
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                    ConnectionString="<%$ ConnectionStrings:EmployeeDetailsConnectionString4 %>"
                    SelectCommand="SELECT DISTINCT [DeptID] FROM [Department]">
                </asp:SqlDataSource>


please help me.
Thanks in Advance.
Posted
Updated 23-May-11 1:30am
v3

Try this ...

ddlDept.Items.Insert(0, new ListItem("--select a dept value--", "-1"));
 
Share this answer
 
Comments
PRASAD GDV 23-May-11 6:45am    
no it is not working .
i dont know where to write this code.
i tried it with writing in form load.
please tell me where to writr
Deepthi Aravind 23-May-11 7:01am    
pls write this line after binding the drop down list...
PRASAD GDV 23-May-11 7:37am    
ok it is working
XML
<asp:DropDownList ID="ddlDept" runat="server" DataSourceID="SqlDataSource1"
                    DataTextField="departments" DataValueField="DeptID" Font-Underline="False">
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                    ConnectionString="<%$ ConnectionStrings:EmployeeDetailsConnectionString4 %>"
                    SelectCommand="SELECT DISTINCT [DeptID],departments  FROM [Department]">
                </asp:SqlDataSource>


in code behind do something like it
ddlDept.Items.Insert(0, "--select a dept value");
 
Share this answer
 
Well, the most obvious decision is to use a UNION operator. Another one I can think of, a bit more sophisticated, is first reading query output into array / collection and then adding your "select dept" option (or adding a new item). That requires coding, though
 
Share this answer
 
Why not use AppendDataBoundItems?

XML
<asp:dropdownlist id="ddlDept" runat="server" datasourceid="SqlDataSource1" datatextfield="DeptID" datavaluefield="DeptID" font-underline="False" appenddatabounditems="true" xmlns:asp="#unknown">

<asp:listitem text="-- Select --" value="" />

                </asp:dropdownlist>

                <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                    ConnectionString="<%$ ConnectionStrings:EmployeeDetailsConnectionString4 %>"
                    SelectCommand="SELECT DISTINCT [DeptID] FROM [Department]">
                </asp:SqlDataSource>
 
Share this answer
 
Comments
PRASAD GDV 23-May-11 7:18am    
ok it is working
thank you very much for u r valuble answer
Dalek Dave 23-May-11 7:28am    
If it was valuable, please vote a 5, not a one!

You can change it you know.

I suspect you confused the numbers.
PRASAD GDV 23-May-11 7:36am    
i didnt vote you till now .
i 1 is not mine it is some one's vote
ok i am voting you with 5.thank you for you answer
Dalek Dave 23-May-11 7:39am    
I apologise most sincerely then. And 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