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.

79 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
  35. 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.
    360digitmg

    ReplyDelete
  36. Wow! Such an amazing and helpful post this is. I really really love it. I hope that you continue to do your work like this in the future also.

    Apache Spark Training in Pune
    Spark Training Institute in Pune

    ReplyDelete
  37. Thanks for sharing great information. I highly recommend you.data science courses

    ReplyDelete
  38. Thanks for sharing the valuable information. it’s really helpful.Best data science courses in hyerabad

    ReplyDelete
  39. Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work.
    data science using python and r programming Guwahati

    ReplyDelete
  40. I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it!
    data science courses

    ReplyDelete
  41. This is a splendid website! I"m extremely content with the remarks!ExcelR Data Analytics Course

    ReplyDelete
  42. Your content is very unique and understandable useful for the readers keep update more article like this.
    data science training

    ReplyDelete
  43. Thanks for the information about Blogspot very informative for everyone
    data scientist malaysia

    ReplyDelete
  44. Thanks for the information about Blogspot very informative for everyone
    digital marketing course

    ReplyDelete
  45. Hello there to everyone, here everybody is sharing such information, so it's fussy to see this webpage, and I used to visit this blog day by day
    data science course noida

    ReplyDelete
  46. Happy to visit your blog, I am by all accounts forward to more solid articles and I figure we as a whole wish to thank such huge numbers of good articles, blog to impart to us.
    data science certification

    ReplyDelete
  47. 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.
    Data Science Course in Bangalore

    ReplyDelete
  48. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
    data analytics course in bangalore

    ReplyDelete
  49. Nice article. I liked very much. All the information given by you are really helpful for my research. keep on posting your views.
    data scientist training malaysia

    ReplyDelete
  50. Well we really like to visit this site, there is a lot of useful information we can get here.
    Best Data Science Courses in Hyderabad


    ReplyDelete
  51. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    Data Science Training in Bangalore

    ReplyDelete
  52. 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.
    artificial intelligence course in pune

    ReplyDelete
  53. I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.
    data analytics training in bangalore

    ReplyDelete
  54. You have an extremely knowledge perspective.It’s incredible how thorough your work is.
    machine learning course

    ReplyDelete
  55. I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
    Data Science Training in Chennai

    ReplyDelete
  56. Actually i read it yesterday but I had some ideas about it and today i wanted to read it again because it is so well written
    Data Science Training in Chennai

    ReplyDelete
  57. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
    data science courses in hyderabad

    ReplyDelete
  58. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
    artificial intelligence course in pune

    ReplyDelete

  59. Very awesome!!! When I searched for this I found this website at the top of all blogs in search engines.

    Best Institutes For Digital Marketing in Hyderabad


    ReplyDelete
  60. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
    data analytics course in bangalore

    ReplyDelete
  61. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
    data analytics course in bangalore

    ReplyDelete
  62. For most successful advertisers, the greatest selling point is a bit of mystery. Find the beauty hidden in mundane things. dig this

    ReplyDelete
  63. Watch movies online sa-movie.com, watch new movies, series Netflix HD 4K, ดูหนังออนไลน์ watch free movies on your mobile phone, Tablet, watch movies on the web.

    SEE4K Watch movies, watch movies, free series, load without interruption, sharp images in HD FullHD 4k, all matters, ดูหนังใหม่ all tastes, see anywhere, anytime, on mobile phones, tablets, computers.

    GangManga read manga, read manga, read manga online for free, fast loading, clear images in HD quality, all titles, อ่านการ์ตูน anywhere, anytime, on mobile, tablet, computer.

    Watch live football live24th, watch football online, ผลบอลสด a link to watch live football, watch football for free.

    ReplyDelete
  64. SamudraBet Situs Judi Online Terbaik, Terpercaya dan Terbesar di Indonesia. Menyediakan permainan :

    Judi bola terbaik

    Judi slot online terpercaya

    Poker online terbaru

    Live casino online

    Judi sabung ayam

    ReplyDelete
  65. I want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging enedevors.
    data science course fees in bangalore

    ReplyDelete