Please navigate to the bottom of the page for Table of Contents

Thursday, June 2, 2011

Explain ASP.NET data binding using DataSets and DataSourceControls

In a data centric web application, you are required to bind data to a UI control (such as GridView or Repeater). This data can come from a variety of sources. In an interview, it is important for you to know a couple of simple ways to bind data (from say SQL Server table) to a web UI control. This is a very complex topic and there are entire books written on this subject. In this post we will explore two very basic ways to bind data from SQL to the web control.

Using DataSet to bind data

In the first example, we will explore an old way of bind data using DataSets. You will still find this code in many established (legacy) code bases and it is worthwhile to dig deeper in this style. The basic idea is that you get your data in a DataSet and then set that data to the DataSource prperty of your Data bound control and then call DataBind(). The code snippet below shows a simple ASPX page and it’s code behind:

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>


The code behind file loads data from the SQL into a DataSet and then binds it to the control.


using System;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connString = "some valid conn string";
SqlConnection connection = new SqlConnection(connString);

using (SqlCommand cmd = new SqlCommand("Select * from Products", connection))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}


Using Data Source controls to bind data


The other method of displaying data is using the Data Source controls. ASP.NET provides seven built-in data source controls; each targeting a specific data type. I urge you to explore and understand each of the following controls: SqlDataSource, LinqDataSource, ObjectDataSource, XmlDataSource, and so on. These controls can be configured extensively which is beyond the scope of this post. The example below illustrates the simplest use of a SqlDataSource with a GridView control to display data.


The code snippet below shows a very simple declarative way of defining the SqlDataSource.


<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
SelectCommand="SELECT * FROM [Products]"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" />


The same GridView we difined in our first example can be used with slight modifications. Instead of using DataSource and BindData() calls, we just use the DataSourceID property of the control.

 

<asp:GridView ID="GridView1" runat="server"
DataSourceID="SqlDataSource1" >
</asp:GridView>


We have barely scratched the surface of Data binding. But this should give a good base for preparing for your interview skills for data binding.

39 comments:

  1. You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! Data Blending in Tableau

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.
    data science certification malaysia

    ReplyDelete
  4. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
    data science training in bhilai

    ReplyDelete
  5. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
    Data Science Course in Pune
    Data Science Training in Pune

    ReplyDelete
  6. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.
    data science training in indore

    ReplyDelete
  7. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    Data Science Training in Hyderabad | Data Science Course in Hyderabad

    ReplyDelete
  8. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    data science training in vijayawada

    ReplyDelete
  9. You are in point of fact a just right webmaster. The website loading speed is amazing. It kind of feels that you're doing any distinctive trick. Moreover, The contents are masterpiece. you have done a fantastic activity on this subject!
    Business Analytics Course in Hyderabad | Business Analytics Training in Hyderabad

    ReplyDelete
  10. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.
    data science training in coimbatore

    ReplyDelete
  11. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.

    data science course in guntur

    ReplyDelete

  12. It has fully emerged to crown Singapore's southern shores and undoubtedly placed her on the global map of residential landmarks. I still scored the more points than I ever have in a season for GS. I think you would be hard pressed to find somebody with the same consistency I have had over the years so I am happy with that.
    data science training in noida

    ReplyDelete
  13. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

    data science training in guntur

    ReplyDelete
  14. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.
    data science training in indore

    ReplyDelete
  15. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
    data science training in vijayawada

    ReplyDelete
  16. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
    data science course in guduvanchery

    ReplyDelete
  17. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates.
    data science training in coimbatore

    ReplyDelete
  18. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    data science training in aurangabad

    ReplyDelete
  19. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.

    data science course in guntur

    ReplyDelete
  20. Great post I must say and thanks for the information. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.

    data science course in guntur

    ReplyDelete
  21. I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…
    data science course in guduvanchery

    ReplyDelete
  22. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
    data science course in ecil

    ReplyDelete
  23. I have bookmarked your website because this site contains valuable information in it. I am really happy with articles quality and presentation. Thanks a lot for keeping great stuff. I am very much thankful for this site.
    data science training in ecil

    ReplyDelete
  24. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
    artificial intelligence course in ecil

    ReplyDelete
  25. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
    artificial intelligence training in ecil

    ReplyDelete
  26. Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
    Data Analyst Course

    ReplyDelete
  27. Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.
    data science course in hyderabad
    data analytics course in hyderabad
    business analytics course in hyderabad

    ReplyDelete
  28. Going back to my personal experience, after the theoretical part I have done the Internship programme, which consist in a period of 3 months work experience made up of doing the task that a digital marketer would deal on a daily basis routine. digital marketing course in hyderabad

    ReplyDelete
  29. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have for sharing.
    software skills

    ReplyDelete
  30. Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data sciecne course in hyderabad

    ReplyDelete
  31. Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data sciecne course in hyderabad

    ReplyDelete
  32. Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data sciecne course in hyderabad

    ReplyDelete
  33. Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data science course

    ReplyDelete
  34. Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data sciecne course in hyderabad

    ReplyDelete