Monday, July 21, 2014

Bind two data columns as DataTextField in a DropDownList with SQLDataSource in ASP.NET C#

Hello! To day i will show you how to bind two data fields into one DataTextField or DataValueField in a DropDownList in ASP.NET.

Table structure :

Id             Name           
1        Arkadeep De
2        Pradeep Das
3        Praphul Kumar

Now in your DropDownList your data will show llike

Arkadeep De (1)
Pradeep Das (2)
Praphul Kumar (3)

And in your value will be as your data in Id column ( 1,2,3...).

So how to do that with SQLDataSource !

First create a new project and add a new web form. After that add a new DropDownList and a New SQLDataSource. Bind the DropDownList with the SQLDataSource.

<asp:DropDownList ID="ddlTID_NO" runat="server" DataSourceID="SqlDataSource1"> </asp:DropDownList>

Now in the SelectCommand of the SQLDataSource write down the following query.

SELECT [ID], [NAME]+'  ('+Id+') ' as Name1 FROM [TABLE_NAME] order by ID

Now in the DropDownList add the DataTextField=Name1 and DataValueField=Id

Your project is ready. Now run the project and check the result.


Post a Comment

Popular Posts