Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
How to concatenate two or more fields using LINQ in ASP.NET

Author: Rudy Pinson
Download Source Code : 1357_ConcatenateFieldsUsingLINQ.zip

LINQ query can be used to concatenate two or more fields to display on DropDowList, CheckBoxList or ListBox controls.

 

I will use LINQ to SQL class in my code to get data from database. I will concatenate three database table fields using LINQ query and make it a single field to bind it to DropDownList. Source code is available to download from the top.  

 

  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Now make connection to the server by using server explorer. I am using NORTHWIND Sample Database for this example.=
  4. Add LINQ to SQL Classes by using Add New Item and Rename it as Employees.dbml. It will be added in App_Code folder.
  5. Drag Emplyees table from Server Explorer table list of NORTHWIND database and drop it in Employees.dbml.
  6. Now open Default.aspx page and drag and drop a DropDownList control in the page.

    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
     
  7. Open code behind file and write code below in Page Load method. Make sure “System.Linq” namespace exists at the top of your code behind file.

    C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        EmployeesDataContext dataContext = new EmployeesDataContext();
     
        var employees = from emp in dataContext.Employees
                        select new
                        {
                            EmployeeID = emp.EmployeeID,
                            EmployeeName = emp.EmployeeID + ".  " + emp.FirstName + " " + emp.LastName
                        };
     
        DropDownList1.DataSource = employees;
        DropDownList1.DataValueField = "EmployeeID";
        DropDownList1.DataTextField = "EmployeeName";
        DropDownList1.DataBind();
    }
     
    VB.NET

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim dataContext As New EmployeesDataContext()
     
        Dim employees = From emp In dataContext.Employees
                        Select New With
                        {
                            Key .EmployeeID = emp.EmployeeID,
                            Key .EmployeeName = Convert.ToString(emp.EmployeeID) & ".  " & Convert.ToString(emp.FirstName) & " " & Convert.ToString(emp.LastName)
                        }
     
        DropDownList1.DataSource = employees
        DropDownList1.DataValueField = "EmployeeID"
        DropDownList1.DataTextField = "EmployeeName"
        DropDownList1.DataBind()
    End Sub
     
  8. View the Web Site in browser and see the result.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to read and display text file data in ASP.NET

Disclaimer - Privacy
© 2002-2014 DevASP.net