Pages

Ruchi Tech

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.

11 comments:

  1. You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us
    java training in chennai | java training in bangalore

    java online training | java training in pune

    java training in chennai | java training in bangalore

    ReplyDelete
  2. This is a terrific article, and that I would really like additional info if you have got any. I’m fascinated with this subject and your post has been one among the simplest I actually have read.
    python training in chennai
    python training in Bangalore

    ReplyDelete
  3. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    DevOps online Training|DevOps Training in USA
    Devops Training in Chennai
    Devops Training in Bangalore

    ReplyDelete
  4. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    selenium training in chennai
    selenium training in bangalore
    selenium training in Pune
    selenium training in pune
    Selenium Online Training

    https://rupeshrshinde.blogspot.com/2014/11/limitations-of-selenium-web-driver.html

    ReplyDelete
  5. It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  6. Thanks for sharing such informative guide on .Net technology. This post gives me detailed information about the .net technology. I am working as trainer in leading IT training academy offering Dot Net Training in Chennai and i use your guide to educate my students.




    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery

    ReplyDelete