Binding to a DataGrid Web Control

This demo illustrates how to bind a DataSet to a DataGrid. Note our SQL query is returning over 160 results, making this data hard to digest. Ideally, we'd like to page this database data. As we will see further on in the article, giving this DataGrid the ability to page is a snap!


FAQIDDescriptionDateEnteredViewCount
2How do you convert a string to all uppercase or all lowercase?9/23/2000 7:53:10 PM53397
7How do I dynamically resize an array?9/23/2000 7:53:09 PM71897
8How do you create a database table through a SQL statement (via an ASP page, perhaps)?9/24/2000 12:27:51 AM74803
10How can I tell what version of ADO I am using?9/24/2000 12:40:00 AM43809
11How can I have my ASP page send HTML-formatted email?9/24/2000 12:48:05 AM62047
12How can I display line breaks in an email message?9/24/2000 12:53:09 AM48000
13How do you send email attachments through an ASP page?9/24/2000 1:14:12 AM111649
14Can I send emails without using CDONTS?9/24/2000 1:18:37 AM130322
15How can I determine the length of a string (how many characters are in a string)?9/24/2000 3:22:52 PM45354
16Using VBScript, how can I determine if a variable is a valid date or not?9/24/2000 3:28:27 PM53379
17How can I find the position of a certain character (or substring) within a string?9/24/2000 3:38:31 PM65717
18How can I easily display a nicely formatted date variable?9/24/2000 3:44:52 PM75619
19How can I return the current date/time?9/24/2000 3:47:26 PM58677
20What is recursion?9/24/2000 3:53:00 PM43629
21How can I determine the upper or lower bounds of an array?9/24/2000 3:59:10 PM55032
22How can I validate an email adress using Jscript?9/24/2000 11:48:56 PM74792
23What, exactly, is the FileSystemObject?9/25/2000 2:24:49 AM80731
24How can I read the contents of a text file through an ASP page?9/25/2000 2:41:06 AM60479
25How can I send an email using CDONTS?9/25/2000 2:52:34 AM90575
26How can I convert the contents of an array into a string?9/25/2000 3:02:45 AM69457
27How do I get SQL to accept an apostrophe in various queries, such as when I try to add a user named "O'Brien"?9/26/2000 4:14:14 PM79199
28When I put a value from my database into the value of a form text field, only the first word appears in the browser? What am I doing wrong?9/26/2000 4:42:43 PM50878
29How can I determine if a visitor has cookies support enabled in his/her browser?9/26/2000 11:03:29 PM94329
30For session variables to work, must the Web visitor have cookies enabled?9/26/2000 11:13:06 PM133635
31How do I set a string variable so that it has quotes in it? I want a variable to equal: Bob said, "Hello."9/27/2000 3:26:14 AM57236
32Why are there certain times when I can use parenthesis when calling a Subroutine, and other times not?9/27/2000 3:40:41 AM51170
33I've seen function definitions that include ByRef and ByVal statements. What are these?9/27/2000 3:53:34 AM52331
34How can I copy a file using ASP?9/27/2000 3:14:54 PM65249
35How can I display dates in a completely custom format (that might not be supported by FormatDateTime)?9/27/2000 5:10:40 PM71759
36How can I rename a file on my Web site through an ASP page?9/28/2000 2:42:11 PM65425
37How can I page database results N records at a time?9/30/2000 6:04:13 AM56067
38How can I dynamically change the action of a form?9/30/2000 8:55:10 PM92236
41How can I delete an existing cookie?9/30/2000 10:58:08 PM82522
42How can I make an argument of a Function or Sub optional?10/4/2000 11:58:59 AM66690
43How can I determine the number of days in a given month?10/6/2000 3:22:59 PM51270
44How can I display Recordset data in an HTML TABLE? How can I get the names of all the fields in a table?10/6/2000 3:44:19 PM64285
45How can I ensure that a form field has a particular value? (That is, how can I perform form validation?)10/6/2000 7:50:00 PM58110
46How can I get the path of the system temporary folder? Also, how can I get a filename for a temporary file?10/7/2000 6:52:33 PM46902
47How can I validate an email address using VBscript?10/8/2000 2:07:02 PM115252
48How can I add or subtract time from a date?10/9/2000 4:32:35 PM80966
49Can I send my output to a different window or frame than the one that asked for the current ASP page?10/11/2000 2:19:13 AM48632
50Can I disable the use of session variables on an ASP page-by-page basis?10/15/2000 2:21:43 AM47054
51How can I order the results from a database query?10/15/2000 2:32:52 AM49357
52How can I display all of the cookies/cookie values for a user through a Web page?10/15/2000 2:48:59 AM59803
53How can I change a string like $51,234.98 into a number (51234.98) 10/15/2000 6:15:46 PM64982
54How do I convert an all uppercase or all lowercase string into mixed case (first letter of each word uppercase, all other letters lower case)? 10/16/2000 4:35:39 PM57082
55How can I conditionally #include files? How can I tell if a file is already #included so I won't include it again? How do #include files work?10/16/2000 4:39:11 PM53350
57Okay, if I can't do a conditional #include then how can I conditionally include things in an ASP page?10/16/2000 4:48:01 PM44193
58Why do I get a type mismatch error from my query? Why do I get the message Syntax error (missing operator) from my query? Why do I get a Too few parameters expected 1 error from my query? 10/16/2000 5:49:54 PM61107
59(1) How can I have two dates and select all records that have a date field with a value between those two dates? (2) How can I select all records in a given month?10/16/2000 6:24:22 PM63400
60How can I retrieve emails from a service like Yahoo! Mail?10/16/2000 6:55:37 PM55581
61How can I treat some other web page as data on my own site?10/16/2000 7:14:30 PM91306
62How can I read the contents of a file, line-by-line, into an array?10/16/2000 8:36:37 PM61441
63How do I put an array into a Session or Application "variable"? How do I then use it on the next ASP page?10/16/2000 8:36:56 PM108962
64How can I retrieve the fields in my form in the order they appear in the form?10/16/2000 8:51:08 PM88194
65How can I pick random records from a table?10/16/2000 9:13:54 PM59580
66Is there a way to replace pattens of text in a string so that the replacement text includes whatever the pattern matches? For example: enclose all words ending in "ing" in tags? 10/17/2000 3:25:07 AM46427
67How can I call an executable program from my ASP page?10/17/2000 2:59:44 PM51498
68How can I eliminate duplicates from a recordset?10/17/2000 8:09:42 PM50160
69When I display the result from a TEXTAREA in a Web page, the line breaks are removed. How can I maintain a TEXTAREAs line breaks in an Web page?10/24/2000 3:47:31 AM65826
70How big can my Access Database be?10/24/2000 6:09:29 PM55031
71Using CDONTS, how can I set a Reply To email address?10/25/2000 2:17:14 AM61760
72When I try to do a Reponse.Redirect, it get an error message about "the HTTP headers are already written..." Why? What can I do?10/25/2000 9:17:39 PM47395
73How can I time the execution speed of my ASP pages to the millisecond?10/28/2000 4:08:29 AM54273
74This question comes from Steve: How can I kill one session variable at a time? "I don't want to have to abandon the whole session, just kill off a few session variables that were used?"10/29/2000 4:21:59 PM65649
75Will I ruin the performance of my application if I use session variables?10/29/2000 5:18:45 PM58918
76How can I run a stored procedure from ASP using only the ADO connection object?10/31/2000 1:16:21 AM63507
77Is there a better way to show debug values from my ASP pages than to Response.Write them in the middle of my page or use a JavaScript alert to pop them up?10/31/2000 2:44:31 AM47057
78How do I find out if a file exists?11/1/2000 4:22:14 PM53170
79**UPDATED 1 April 2002** How do I get the record number of a just added record? 11/3/2000 9:07:08 PM97514
80My memo fields show up as blank! OR When I test my memo field for a value and then try to show it only if it is not blank [or only if it contains some string or or or], then when I display it I get nothing!11/7/2000 9:12:07 PM58032
81Why do I get the message Microsoft JET Database Engine error ' 80040e14' Syntax error in INSERT INTO statement?? 11/8/2000 9:01:21 PM61560
82I let my users put in search strings for any number of fields. How do I convert their inputs into a usable SQL search string?11/10/2000 5:42:39 PM52361
83How can I quickly sort a VBScript array? 11/12/2000 1:58:41 AM230033
84How can I display all of the contents of a single-dimension array?11/13/2000 4:34:35 AM60990
85How can I disable the back button?11/15/2000 3:02:05 AM46537
86How do I extract the contents of an array without knowing the size of the array?11/15/2000 6:32:06 PM53906
87Q: How can I round or slide a number to the nearest integer multiple of my choice? (For example rounding 13.3, 22.75 and 6 to the nearest 5 = 15, 25, 5 respectively)11/21/2000 1:17:44 PM53456
88Is there a way I can automate the retrieval of a recordset from a database (so I don't have to type the same code over and over)? 11/21/2000 10:50:01 PM52238
89How can I sort a VB Script array in ways other than by case-sensitive alphanumeric order, like numeric value, length of string, or even randomly?11/22/2000 12:41:40 AM105385
90How can I programmatically list the contents of a directory on the Web server?11/24/2000 3:57:35 PM69860
91How can I replace each occurrence of a particular pattern in a string with another pattern?11/25/2000 1:00:14 AM61234
92How can I determine the position of a particular string in an Array?11/26/2000 2:11:11 PM58403
93How can I quickly determine if a string exists within an array?11/26/2000 2:32:57 PM73272
94How can I convert a string into an array?11/27/2000 3:53:50 AM65198
95What is Transact-SQL (T-SQL)?11/30/2000 4:12:36 AM65401
96How can I capture and use (i.e. save to disk, let the browser download) the output of one of my own ASP pages?11/30/2000 7:45:48 AM75978
97How can I get a count of the number of dimensions in an array?12/1/2000 12:43:26 AM58949
98What scripting languages can utlitize regular expressions?12/3/2000 8:36:30 PM42931
99How can I use regular expressions to dynamically place delimiters around certain strings?12/3/2000 8:44:26 PM51999
100How can I list all of the existing Session variables for a particular user?12/5/2000 3:54:05 PM70443
101How can I list all of the Application variables on a Web page?12/5/2000 3:59:34 PM77615
102How do I use CASE in Transact SQL?12/9/2000 4:18:33 AM75026
105How can I make inheritable classes in ASP?12/13/2000 2:25:37 PM42515
106How can I find out if a record already exists in a database? If it doesn't, I want to add it.12/14/2000 4:59:11 PM183511
107** UPDATED 24 June 2003 **: Why am I getting "Operation must use an updateable query" errors?12/21/2000 6:38:47 PM119416
108How can I delete a file from the Web server's file system?12/27/2000 9:04:40 PM63028
109How can I create a folder (directory) on the Web server?1/7/2001 3:00:04 PM63362
110What is CrossPosting? Why is it considered a bad thing?1/8/2001 9:52:49 PM47497
111What is the VBScript Integer Division operator and how can it help my code?1/14/2001 2:36:26 AM55681
112How do I send variables and data from client-side JavaScript to server-side ASP?1/14/2001 3:10:58 AM57524
113How can I use RecordSet.GetString to create a drop down list? How about other more complex output using GetString? 1/16/2001 7:56:41 PM59771
114How can I shuffle a deck of cards or similar set of sequential numbers? How can I reorder a list of things in random order? 1/17/2001 9:44:20 PM46259
115I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem?1/17/2001 11:38:49 PM233530
116I am performing a currency calculation and would like to display the value as 7.30, but it is showing up as 7.3. How can I force two demial places?1/18/2001 6:31:07 PM50969
117How can I insert Printer Page Breaks in ASP/HTML?1/19/2001 2:54:42 PM56203
118When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... 1/22/2001 5:10:01 PM168750
121How can I pop up a message from ASP? An alert box, when something goes wrong. A "confirm" dialog showing details of a record to make sure the user really wants to delete the record?1/22/2001 7:09:01 PM62109
122How can I create a chart or graph that I can display to the user?1/24/2001 2:34:42 PM61935
123What is adovbs.inc? Why should I use it? What benefits does it have?1/28/2001 5:10:19 AM87088
124How can I perform a Reverse DSN Lookup through ASP?1/31/2001 2:34:09 AM50596
125How can I determine the file size of a file on the Web server?2/2/2001 12:34:49 AM53875
126How do I populate an HTML dropdown box from the results of a database table?2/4/2001 5:14:40 PM84038
127How can I count the number of substrings in a string?2/8/2001 1:47:26 AM63085
128How can I convert binary data into a string? 2/11/2001 2:00:04 PM76971
129Can I create an application-level Dictionary object?2/11/2001 10:08:09 PM76235
130How can I use regular expressions to help validate that a user-entered value is in a proper format?2/17/2001 11:37:02 PM55747
131How can I have my Web visitors upload a file from their computer to the Web server without using a component?2/25/2001 4:13:15 AM76615
132How can I create my own file uploading component?2/25/2001 4:32:55 AM56763
133How do I insert more information into the middle of a text file? How do I delete something from a text file? How do I add to the front of a text file?2/25/2001 2:28:52 PM48185
134How can I execute ASP pages on Windows 9X? (How do I install Personal Web Server?)3/2/2001 12:41:10 AM59898
135I've got ASP installed on my computer. What version of ASP am I using, though?3/2/2001 12:53:29 AM45039
136How can I perform advanced networking functions through an ASP page, such as SNMP, FTP, WhoIs lookups, etc.?3/2/2001 1:47:54 AM49531
137Why do I get an error when I try to insert a value into a database that contains an apostrophe?3/2/2001 3:58:24 PM49332
138I'm getting an error (80020009) when trying to work with a value from a Memo (or text or blob) column. For example, I get this error when I do: Response.Write objRS(MemoColumnName)3/2/2001 4:10:13 PM54771
139Why does the .RecordCount property return a value of -1?3/2/2001 4:29:29 PM63999
140How can I show a table of records from a recordset and let my users choose which field (column) to sort on?3/4/2001 1:03:44 AM66963
141How can I effectively use a checkbox to update data in a database? (A typical example might be changing the status of members in a roster from active to inactive or vice versa.) 3/4/2001 10:17:09 PM83811
142How can I use application-level variables to cache information?3/4/2001 11:24:03 PM97894
143How can I create a file with a random file name?3/6/2001 5:21:20 AM58382
145How is an array stored in memory? I am interested in learning about the internals of an array!3/23/2001 7:19:14 PM56577
146How is a two-dimensional laid out in memory?3/23/2001 7:26:46 PM53696
147I want to repeat a character or string X number of times, is there a way I can do this without expensive looping operations?3/24/2001 6:21:07 PM49031
148How can I create a text file on the Web server's file system?3/26/2001 1:04:56 AM71033
149How can I remove multiple spaces between words in a string? That is, if I have: Hi    there how can I get: Hi there?3/31/2001 5:02:54 PM129557
150What databases can ASP work with? How do technologies like ADO, OLE-DB, and ODBC fit in?4/5/2001 11:49:46 PM88900
151How can I count the total number of words that appear in a string?4/7/2001 8:08:33 PM88303
152What are some issues that may arise when using dates and working with databases?4/9/2001 5:22:37 PM47730
153How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3?4/19/2001 3:15:34 PM128350
154I have a database with a table of categories and a table of subcategories. How can I easily make a display that will show each category with its related subcategories grouped under it?4/20/2001 10:29:25 PM64205
155How can I strip the HTML tags from a text string using regular expressions?4/24/2001 1:22:14 PM49795
156What third-party file uploading components are available?4/27/2001 2:22:50 PM45329
157What is ASP.NET?6/10/2001 4:15:38 PM49574
158What do I need to start working with ASP.NET?6/10/2001 4:24:49 PM51057
159Where can I learn more about ASP.NET?6/10/2001 4:52:29 PM55617
160Why do I get the error "The application is using arguments that are of the wrong type, are out of acceptable range, or are in conflict with one another" when I try to open a recordset?6/11/2001 3:24:50 PM91729
161How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset?6/14/2001 5:02:02 PM238089
162What are the differences between ASP.NET Beta 1 and Beta 2?6/19/2001 3:54:01 PM52117
163How do I convert a string into an Integer?7/10/2001 3:54:30 PM123762
164How can I print a web page in ASP?/Can I bypass the printer dialog box?7/27/2001 3:19:29 PM68295
165How can I rename a directory on my Web server?8/14/2001 12:24:18 PM54063
166How can I secure an Access database that I use on my Web site? That is, I don't want users to be able to guess the URL of my Access .mdb file and download my database!8/24/2001 10:12:25 AM81254
169Fundamentals of client-server ASP:   How can my ASP code call a JavaScript (or VBScript) function in the browser? How can I use a function or event in the browser to call a VBScript function in ASP? 9/21/2001 3:24:19 PM61858
170How do I post in a message board (and get answers)?9/30/2001 7:12:40 PM48358
171How can I get all Field Names or Column Names from a recordset or database table ? ..or.. How can I write a general "table dump" for all records and fields in a recordset or table?10/2/2001 3:52:00 PM61000
172How can I display images that reside in my database through an ASP page?11/12/2001 12:49:12 PM110298
173How can I pick a random record from a table, using a stored procedure?11/21/2001 7:46:30 AM66022
174Why do I get a closed recordset from my stored procedure? Why don't I get the expected fields from the stored procedure?11/21/2001 8:17:02 AM57541
175Is ASP.NET output compatible with all browsers? If not, what browsers is the output compatible with? 12/4/2001 12:02:09 AM53711
176How can I delete files by extension in a given directory? What if I have multiple file extension names I want to delete?12/16/2001 12:47:35 PM81511
177How much will ASP.NET (the .NET Framework) cost once Microsoft is finished with Beta testing and ready to roll a "final" version?12/30/2001 2:11:15 PM46764
178How can I output the records from my recordset in columns in a table? That is, instead of just one record per row in a table, how can I have multiple records per row?1/7/2002 7:16:35 PM71539
179How can I determine the location of user by their IP, or validate the identity of a user based on his IP?1/8/2002 11:37:07 AM72898
180How can I install Personal Web Server (PWS) onto Windows XP Home edition?1/10/2002 2:13:09 PM116884
181How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.1/19/2002 3:12:07 PM964702
182What are the differences between ASP.NET Beta 2 and Version 1.0?1/28/2002 7:19:13 PM43770
183How can I calculate the years, months, and days between two dates?2/4/2002 12:05:46 AM72979
184How can I randomly select n lines from a text file?2/8/2002 12:31:14 PM51502
185What are the differences between C# and VB.NET, and which language should I use to create my ASP.NET Web pages?2/8/2002 12:43:02 PM64405
186I have many duplicate records in my database table. I want to delete all of them except one. How can I do this?2/8/2002 12:52:36 PM63368
187If I deploy an ASP.NET Web application will the sysadmin of the Web server where the code in installed be able to see my ASP.NET Web page source code?3/11/2002 5:19:42 PM73651
188How do I use regular expressions from an ASP.NET Web page?3/12/2002 12:28:26 PM70022
189How are dates and times stored in the various components of ASP? Why does the appearance of a date and/or time change depending on what I use to display it?3/22/2002 4:35:40 PM75392
190How do I display data on a web page using arrays instead of Do...While...MoveNext...???...4/11/2002 1:14:41 PM186006
191How do I make my forms show what the user chose before? That is, how do I show which OPTION the user previouly selected? Which checkbox? Which radio button? 4/26/2002 7:02:24 PM61683
192When using the FSO, my ASP page tends to "hang", but doesn't report any errors.4/30/2002 1:20:32 PM59461
193How can I restrict access to a page or a portion of my Web site?6/2/2002 2:18:24 PM83896
194What is the SQL Injection Attack? Am I at risk? How do I fix such a security hole?6/18/2002 5:42:57 PM51675
195Can I store VBScript class objects in a Session variable?9/3/2002 10:23:05 PM61976
196How can I share cookies between my server-side scripts (ASP) and my client-side scripts (e.g., JavaScript in the browswer)?11/15/2002 8:34:54 PM47592
197What is the Cross Site Scripting security vulnerability? Am I at risk? How do I plug this security hole?11/26/2002 10:44:15 PM45510
198What is XML?2/17/2003 1:12:57 PM50061
199How can I convert database data into XML (in both ASP and ASP.NET)?2/17/2003 1:39:29 PM62520
200What is the easiest way of extracting certain data from an XML document?2/18/2003 10:23:18 PM54493
201What are some of the more advanced regular expression features available with .NET?2/25/2003 10:13:20 PM39351
202I have installed the .NET Framework, but my Web server won't serve ASP.NET Web pages. Help!4/2/2003 1:08:03 PM84035
203What is XSLT and how does it relate to XML?4/6/2003 8:48:31 PM68298
204How can I display XSL-formatted XML data in an ASP.NET Web page?4/7/2003 6:17:13 PM87519
205Why do I get an error message, "Access to the path is denied?"9/3/2003 12:22:43 AM65271
206How do I delete a file from the Web server's file system?9/3/2003 12:24:34 AM62340
207How do I copy or move a file from one location to another?9/3/2003 12:27:35 AM85453
208How do I rename a file or directory?9/3/2003 12:29:08 AM62371
209How do I determine if a file or directory exists?9/3/2003 12:30:01 AM59557
210How can I read the entire contents of a text file into a string?9/3/2003 12:30:37 AM43849
211How can I read the contents of a text file one line at a time?9/3/2003 12:32:03 AM56379
212How can I create an XML document from an ASP.NET Web page?9/21/2003 8:04:57 PM39878
213In ASP.NET, how can I create an XmlDocument instance from an XML string? Similarly, given an XML string, how can I display the data in a data Web control?10/16/2003 12:34:41 PM73586
214How do I send the information a user enters into a form as an email message?3/9/2004 6:40:35 PM47491
215How can I easily work with/manipulate/parse file paths in .NET?5/24/2005 7:25:03 PM31569


Source Code
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    BindData()
  End Sub
	
	
  Sub BindData()
    '1. Create a connection
    Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))

    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "SELECT FAQID, Description, DateEntered, ViewCount " & _
                             "FROM tblFAQ ORDER BY FAQID"
    Dim myCommand as New SqlCommand(strSQL, myConnection)

    '3. Create the DataAdapter
    Dim myDA as New SqlDataAdapter()
    myDA.SelectCommand = myCommand

    '4. Populate the DataSet
    Dim myDS as New DataSet()
    myDA.Fill(myDS)

    'Set the datagrid's datasource to the dataset and databind
    dgPopularFAQs.DataSource = myDS
    dgPopularFAQs.DataBind()	
  End Sub
</script>

    <asp:datagrid id="dgPopularFAQs" runat="server" BorderWidth="0"
       CellPadding="2" Width="100%"
       Font-Name="Verdana"
       Font-Size="Smaller"
       
  		HeaderStyle-HorizontalAlign="Center"
		HeaderStyle-Font-Bold="True"
		HeaderStyle-BackColor="Navy"
		HeaderStyle-ForeColor="White"
				
		AlternatingItemStyle-BackColor="#dddddd">
    </asp:datagrid>

	


[Return to the article]