Wednesday, 20 June 2012

Data binding using LINQ in c#

This article shows how you can connect to a database, get data from a database table, and display it in a DataGrid control. 

Step 1: Creating a C# LINQ ASP.NET Web Site

Step 2: Adding LINQ to SQL class in App_Code in your ASP.NET Web Site

Add your database and required table in this class. After adding, the model looks like


Step 3: Creating your first ASP.NET page using LINQ

Create a new page called "linq.aspx".  Within the .aspx page add a DataGrid control like so:

<asp:DataGrid ID="datagrd1" runat="server" CellPadding="4" CellSpacing="4" Width="100%" EnableViewState="False" AutoGenerateColumns="false">        <HeaderStyle BackColor="ActiveBorder" />             <Columns>                 <asp:TemplateColumn>                     <HeaderTemplate>                          <asp:Label runat="server" Text="User"></asp:Label>                     </HeaderTemplate>                  <ItemTemplate>                         <asp:Label ID="lbluser" runat="server" Text='<%# Eval("user") %>'></asp:Label>                   </ItemTemplate>                 </asp:TemplateColumn>              </Columns>              <Columns>                  <asp:TemplateColumn>                      <HeaderTemplate>                         <asp:Label runat="server" Text="City"></asp:Label>                      </HeaderTemplate>                      <ItemTemplate>                         <asp:Label ID="lblcity" runat="server" Text='<%# Eval("city") %>'></asp:Label>                      </ItemTemplate>                 </asp:TemplateColumn>              </Columns>              <Columns>                 <asp:TemplateColumn>                     <HeaderTemplate>                         <asp:Label runat="server" Text="Age"></asp:Label>                     </HeaderTemplate>                     <ItemTemplate>                        <asp:Label ID="lblage" runat="server" Text='<%# Eval("age") %>'></asp:Label>                     </ItemTemplate>                 </asp:TemplateColumn>             </Columns>
</asp:DataGrid>

Within the code-behind file we’ll then write the code for binding the datagrid like:

DataClassesDataContext dc = new DataClassesDataContext("ur_connection_string");
protected void Page_Load(object sender, EventArgs e)     {         if (!IsPostBack)         {                        dc.Connection.Open();             var q = from a in dc.aaas                     select new                     {                         user = a.user.ToString().Trim(),                         city = a.city.ToString().Trim(),                         age = a.age.ToString().Trim(),                     };             datagrd1.DataSource = q;             datagrd1.DataBind();         }     }
compile and run the program. The result like so:

This post gives you a little idea of how you can bind LINQ to DataSet query results, please let me know what kind of questions you’d like to see answered, and I will do my best to answer them.

2 comments: