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.

74 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. 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

  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. Thanks for sharing great information. I highly recommend you.data science courses

    ReplyDelete
  15. 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
  16. Your content is very unique and understandable useful for the readers keep update more article like this.
    data science training

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

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

    ReplyDelete
  19. 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
  20. 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
  21. 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
  22. 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

  23. 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
  24. For most successful advertisers, the greatest selling point is a bit of mystery. Find the beauty hidden in mundane things. dig this

    ReplyDelete
  25. First You got a great blog .I will be interested in more similar topics. I see you have really very useful topics, i will be always checking your blog thanks.
    digital marketing courses in hyderabad with placement

    ReplyDelete
  26. Join the best SEO Training Institute in India.Visit: <a href="https://www.bgda.in/>Bharat Go Digital Academy</a>.

    ReplyDelete
  27. I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work
    data scientist training and placement

    ReplyDelete
  28. 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!
    best data science institute in hyderabad

    ReplyDelete
  29. It helps us raise a lot of money and find exactly what you’re looking for the first time. In the artitle many of the queries i have never to know about them,but i will study it following this article. Thanks for your sharing. best coffee beans

    ReplyDelete
  30. 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.
    artificial intellingence training in chennai

    ReplyDelete
  31. Incredibly conventional blog and articles. I am realy very happy to visit your blog. Directly I am found which I truly need. Thankful to you and keeping it together for your new post.
    artificial intelligence course in pune

    ReplyDelete
  32. This is also a very good post which I really enjoyed reading. It is not every day that I have the possibility to see something like this..
    data science courses in aurangabad

    ReplyDelete
  33. Impressive. Your story always brings hope and new energy. Keep up the good work.
    best data science institute in hyderabad


    ReplyDelete
  34. impossible to find well-informed people in this particular topic,but you sound like you know what you’re talking about! Skydive Fyrosity Las Vegas
    Columbia Interchange Omni Heat
    skydiving jobs

    ReplyDelete
  35. Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks! .

    AWS Training in Hyderabad

    ReplyDelete
  36. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
    data science training in malaysia


    ReplyDelete
  37. 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 training in malaysia

    ReplyDelete
  38. Thanks for the information about Blogspot very informative for everyone
    ai course aurangabad

    ReplyDelete
  39. Awesome blog gigantic adulation to the blogger and trusting you to concoct a particularly remarkable substance in future. Without a doubt, this post will motivate numerous competitors who are extremely sharp in acquiring the information. Expecting a lot more substance with parcel greater interest further…

    AI Training in Hyderabad

    ReplyDelete
  40. Thanks for the share. But if you guys want Top Digital Branding Agency In Delhi then contact us. Candela Laser

    ReplyDelete
  41. You can be confident that you’re in safe hands with us. We’re among the best within the private security industry offering outstanding private security services in London.private security

    ReplyDelete
  42. Another reason why many people hire us is that our bodyguards are trustworthy, reliable, and dependable. bodyguard company
    Our bodyguards will always be available, and you can depend on them. They are reliable, making them worthy of your trust.

    ReplyDelete
  43. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    data scientist course

    ReplyDelete
  44. Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks! .
    <a href="https://360digitmg.com/india/data-analytics-certification-training-course-in-bangalore>Data Analytics training in Bangalore</a>

    ReplyDelete
  45. I really enjoyed this blog. It's an informative topic. It helps me very much to solve some problems. Its opportunities are so fantastic and the working style so speedy.
    artificial intelligence training in hyderabad

    ReplyDelete
  46. informative post. thanks for sharing. keep up the good work AWS Training in Chennai

    ReplyDelete
  47. I need to to thank you for this very good read!! I definitely loved every little bit of it. I have you bookmarked to check out new things you post… ethical hacking course in gurgaon

    ReplyDelete
  48. I was pinning away for such type of blogs, thanks for posting this for us.
    user experience design company

    ReplyDelete
  49. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one.
    full stack web development course malaysia

    ReplyDelete

  50. Thanks for the informative article. I hope you will provide more articles like this.
    Metamask wallet | Coinbase Login | Coinbase Login

    ReplyDelete

  51. Thanks for the informative article. I hope you will provide more articles like this. I highly recommend everyone to read this. Thanks for sharing your knowledge and opinion with us. Crypto.com Login | Pancake Swap

    ReplyDelete
  52. I curious more interest in some of them hope you will give more information on this topics in your next articles.
    cyber security course

    ReplyDelete
  53. I read this post your post so nice and very informative post thanks for sharing this post!
    spookyswap | Quickswap

    ReplyDelete
  54. 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 scientist course in malaysia

    ReplyDelete
  55. The new wave of innovation that is changing the way people do business is called data science. Gain expertise in organizing, sorting, and transforming data to uncover hidden patterns Learn the essential skills of probability, statistics, and machine learning along with the techniques to break your data into a simpler format to derive meaningful information. Enroll in Data science in Bangalore and give yourself a chance to power your career to greater heights.data science training in hyderabad

    ReplyDelete
  56. Pancake Swap allows users to earn an additional yield by staking supported liquidity provider (LP) tokens in one of its numerous yield farms. By participating in a yield farm, users earn a CAKE yield on their LP tokens. This is on top of the yields generated through transaction fees.

    ReplyDelete
  57. Atomic Wallet allows users to earn an additional yield by staking supported liquidity provider (LP) tokens in one of its numerous yield farms. By participating in a yield farm, users earn a CAKE yield on their LP tokens. This is on top of the yields generated through transaction fees.

    ReplyDelete
  58. 1kmovies Mobile Movies, Bollywood movies download 1kmovie org is one of the most popular online platforms for downloading New Tamil Movies, Bollywood movies, Tamil dubbed Telugu & Malayalam movies, Tamil dubbed Hollywood movies download.

    ReplyDelete
  59. CBS watcthing on roku tv and you are visit my website full information to activation on CBS on roku TV. So you are enjoy and watching the CBS Channel on your smart Tv.
    cbs.com/roku

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

    ReplyDelete
  61. In the world of cryptocurrencies, there are many tools and platforms available to make transactions and manage digital assets. One such tool that has gained popularity is the Metamask Login extension for Chrome. | Metamask Extension | Metamask Extension | Metamask Extension

    ReplyDelete
  62. React Native mobile app development services involve creating mobile applications using the React Native framework, which allows developers to build cross-platform apps with a single codebase. These apps offer a native-like experience on both iOS and Android platforms. Here's what you can expect from React Native mobile app development services
    Top React Native Mobile App Development Services Company

    ReplyDelete
  63. Get the best odds and betting options on cricket with our safe and reliable service. Online Cricket betting ID|

    ReplyDelete
  64. This blog is an insightful journey into the topic! The author's expertise shines through, making complex concepts easy to understand. The engaging writing style kept me hooked from the beginning to the end. Looking forward to more enlightening reads from this blog! data science course kochi

    ReplyDelete