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 PM40586
7How do I dynamically resize an array?9/23/2000 7:53:09 PM58743
8How do you create a database table through a SQL statement (via an ASP page, perhaps)?9/24/2000 12:27:51 AM62148
10How can I tell what version of ADO I am using?9/24/2000 12:40:00 AM31423
11How can I have my ASP page send HTML-formatted email?9/24/2000 12:48:05 AM49058
12How can I display line breaks in an email message?9/24/2000 12:53:09 AM34999
13How do you send email attachments through an ASP page?9/24/2000 1:14:12 AM97684
14Can I send emails without using CDONTS?9/24/2000 1:18:37 AM115089
15How can I determine the length of a string (how many characters are in a string)?9/24/2000 3:22:52 PM32784
16Using VBScript, how can I determine if a variable is a valid date or not?9/24/2000 3:28:27 PM40427
17How can I find the position of a certain character (or substring) within a string?9/24/2000 3:38:31 PM53076
18How can I easily display a nicely formatted date variable?9/24/2000 3:44:52 PM62353
19How can I return the current date/time?9/24/2000 3:47:26 PM45761
20What is recursion?9/24/2000 3:53:00 PM31228
21How can I determine the upper or lower bounds of an array?9/24/2000 3:59:10 PM42108
22How can I validate an email adress using Jscript?9/24/2000 11:48:56 PM61471
23What, exactly, is the FileSystemObject?9/25/2000 2:24:49 AM67676
24How can I read the contents of a text file through an ASP page?9/25/2000 2:41:06 AM47748
25How can I send an email using CDONTS?9/25/2000 2:52:34 AM77288
26How can I convert the contents of an array into a string?9/25/2000 3:02:45 AM56143
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 PM66108
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 PM38336
29How can I determine if a visitor has cookies support enabled in his/her browser?9/26/2000 11:03:29 PM80888
30For session variables to work, must the Web visitor have cookies enabled?9/26/2000 11:13:06 PM118141
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 AM44398
32Why are there certain times when I can use parenthesis when calling a Subroutine, and other times not?9/27/2000 3:40:41 AM38508
33I've seen function definitions that include ByRef and ByVal statements. What are these?9/27/2000 3:53:34 AM39850
34How can I copy a file using ASP?9/27/2000 3:14:54 PM52458
35How can I display dates in a completely custom format (that might not be supported by FormatDateTime)?9/27/2000 5:10:40 PM58444
36How can I rename a file on my Web site through an ASP page?9/28/2000 2:42:11 PM52611
37How can I page database results N records at a time?9/30/2000 6:04:13 AM43630
38How can I dynamically change the action of a form?9/30/2000 8:55:10 PM79277
41How can I delete an existing cookie?9/30/2000 10:58:08 PM68165
42How can I make an argument of a Function or Sub optional?10/4/2000 11:58:59 AM53506
43How can I determine the number of days in a given month?10/6/2000 3:22:59 PM38357
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 PM51490
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 PM45335
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 PM34474
47How can I validate an email address using VBscript?10/8/2000 2:07:02 PM101215
48How can I add or subtract time from a date?10/9/2000 4:32:35 PM67208
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 AM36174
50Can I disable the use of session variables on an ASP page-by-page basis?10/15/2000 2:21:43 AM34150
51How can I order the results from a database query?10/15/2000 2:32:52 AM36660
52How can I display all of the cookies/cookie values for a user through a Web page?10/15/2000 2:48:59 AM46842
53How can I change a string like $51,234.98 into a number (51234.98) 10/15/2000 6:15:46 PM52010
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 PM44061
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 PM40825
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 PM31616
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 PM48078
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 PM50435
60How can I retrieve emails from a service like Yahoo! Mail?10/16/2000 6:55:37 PM42602
61How can I treat some other web page as data on my own site?10/16/2000 7:14:30 PM77848
62How can I read the contents of a file, line-by-line, into an array?10/16/2000 8:36:37 PM48437
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 PM93526
64How can I retrieve the fields in my form in the order they appear in the form?10/16/2000 8:51:08 PM74934
65How can I pick random records from a table?10/16/2000 9:13:54 PM46776
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 AM33767
67How can I call an executable program from my ASP page?10/17/2000 2:59:44 PM38960
68How can I eliminate duplicates from a recordset?10/17/2000 8:09:42 PM37517
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 AM52873
70How big can my Access Database be?10/24/2000 6:09:29 PM42507
71Using CDONTS, how can I set a Reply To email address?10/25/2000 2:17:14 AM48334
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 PM34956
73How can I time the execution speed of my ASP pages to the millisecond?10/28/2000 4:08:29 AM41225
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 PM52346
75Will I ruin the performance of my application if I use session variables?10/29/2000 5:18:45 PM45685
76How can I run a stored procedure from ASP using only the ADO connection object?10/31/2000 1:16:21 AM50926
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 AM34556
78How do I find out if a file exists?11/1/2000 4:22:14 PM40670
79**UPDATED 1 April 2002** How do I get the record number of a just added record? 11/3/2000 9:07:08 PM84261
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 PM45153
81Why do I get the message Microsoft JET Database Engine error ' 80040e14' Syntax error in INSERT INTO statement?? 11/8/2000 9:01:21 PM48777
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 PM39831
83How can I quickly sort a VBScript array? 11/12/2000 1:58:41 AM212145
84How can I display all of the contents of a single-dimension array?11/13/2000 4:34:35 AM47967
85How can I disable the back button?11/15/2000 3:02:05 AM33978
86How do I extract the contents of an array without knowing the size of the array?11/15/2000 6:32:06 PM41359
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 PM40800
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 PM40031
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 AM92046
90How can I programmatically list the contents of a directory on the Web server?11/24/2000 3:57:35 PM56933
91How can I replace each occurrence of a particular pattern in a string with another pattern?11/25/2000 1:00:14 AM48489
92How can I determine the position of a particular string in an Array?11/26/2000 2:11:11 PM45448
93How can I quickly determine if a string exists within an array?11/26/2000 2:32:57 PM59989
94How can I convert a string into an array?11/27/2000 3:53:50 AM51893
95What is Transact-SQL (T-SQL)?11/30/2000 4:12:36 AM52276
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 AM63261
97How can I get a count of the number of dimensions in an array?12/1/2000 12:43:26 AM46135
98What scripting languages can utlitize regular expressions?12/3/2000 8:36:30 PM30379
99How can I use regular expressions to dynamically place delimiters around certain strings?12/3/2000 8:44:26 PM39419
100How can I list all of the existing Session variables for a particular user?12/5/2000 3:54:05 PM57108
101How can I list all of the Application variables on a Web page?12/5/2000 3:59:34 PM64884
102How do I use CASE in Transact SQL?12/9/2000 4:18:33 AM61520
105How can I make inheritable classes in ASP?12/13/2000 2:25:37 PM29882
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 PM167513
107** UPDATED 24 June 2003 **: Why am I getting "Operation must use an updateable query" errors?12/21/2000 6:38:47 PM104733
108How can I delete a file from the Web server's file system?12/27/2000 9:04:40 PM49959
109How can I create a folder (directory) on the Web server?1/7/2001 3:00:04 PM50261
110What is CrossPosting? Why is it considered a bad thing?1/8/2001 9:52:49 PM34910
111What is the VBScript Integer Division operator and how can it help my code?1/14/2001 2:36:26 AM42855
112How do I send variables and data from client-side JavaScript to server-side ASP?1/14/2001 3:10:58 AM45026
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 PM47156
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 PM33489
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 PM217694
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 PM38389
117How can I insert Printer Page Breaks in ASP/HTML?1/19/2001 2:54:42 PM43496
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 PM151850
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 PM49427
122How can I create a chart or graph that I can display to the user?1/24/2001 2:34:42 PM49255
123What is adovbs.inc? Why should I use it? What benefits does it have?1/28/2001 5:10:19 AM73731
124How can I perform a Reverse DSN Lookup through ASP?1/31/2001 2:34:09 AM38047
125How can I determine the file size of a file on the Web server?2/2/2001 12:34:49 AM41040
126How do I populate an HTML dropdown box from the results of a database table?2/4/2001 5:14:40 PM70717
127How can I count the number of substrings in a string?2/8/2001 1:47:26 AM50233
128How can I convert binary data into a string? 2/11/2001 2:00:04 PM63780
129Can I create an application-level Dictionary object?2/11/2001 10:08:09 PM62873
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 PM43036
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 AM63521
132How can I create my own file uploading component?2/25/2001 4:32:55 AM43922
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 PM35529
134How can I execute ASP pages on Windows 9X? (How do I install Personal Web Server?)3/2/2001 12:41:10 AM47193
135I've got ASP installed on my computer. What version of ASP am I using, though?3/2/2001 12:53:29 AM32454
136How can I perform advanced networking functions through an ASP page, such as SNMP, FTP, WhoIs lookups, etc.?3/2/2001 1:47:54 AM36946
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 PM36278
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 PM41735
139Why does the .RecordCount property return a value of -1?3/2/2001 4:29:29 PM51028
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 AM54457
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 PM71015
142How can I use application-level variables to cache information?3/4/2001 11:24:03 PM84381
143How can I create a file with a random file name?3/6/2001 5:21:20 AM45265
145How is an array stored in memory? I am interested in learning about the internals of an array!3/23/2001 7:19:14 PM43801
146How is a two-dimensional laid out in memory?3/23/2001 7:26:46 PM41139
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 PM36134
148How can I create a text file on the Web server's file system?3/26/2001 1:04:56 AM58127
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 PM107857
150What databases can ASP work with? How do technologies like ADO, OLE-DB, and ODBC fit in?4/5/2001 11:49:46 PM76277
151How can I count the total number of words that appear in a string?4/7/2001 8:08:33 PM74454
152What are some issues that may arise when using dates and working with databases?4/9/2001 5:22:37 PM35296
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 PM113621
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 PM51653
155How can I strip the HTML tags from a text string using regular expressions?4/24/2001 1:22:14 PM37049
156What third-party file uploading components are available?4/27/2001 2:22:50 PM32708
157What is ASP.NET?6/10/2001 4:15:38 PM36781
158What do I need to start working with ASP.NET?6/10/2001 4:24:49 PM38320
159Where can I learn more about ASP.NET?6/10/2001 4:52:29 PM42424
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 PM78269
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 PM219334
162What are the differences between ASP.NET Beta 1 and Beta 2?6/19/2001 3:54:01 PM39280
163How do I convert a string into an Integer?7/10/2001 3:54:30 PM107185
164How can I print a web page in ASP?/Can I bypass the printer dialog box?7/27/2001 3:19:29 PM55016
165How can I rename a directory on my Web server?8/14/2001 12:24:18 PM41378
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 AM68272
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 PM48451
170How do I post in a message board (and get answers)?9/30/2001 7:12:40 PM35813
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 PM48113
172How can I display images that reside in my database through an ASP page?11/12/2001 12:49:12 PM96943
173How can I pick a random record from a table, using a stored procedure?11/21/2001 7:46:30 AM52827
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 AM44806
175Is ASP.NET output compatible with all browsers? If not, what browsers is the output compatible with? 12/4/2001 12:02:09 AM40864
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 PM67871
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 PM34176
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 PM58910
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 AM59793
180How can I install Personal Web Server (PWS) onto Windows XP Home edition?1/10/2002 2:13:09 PM101909
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 PM901232
182What are the differences between ASP.NET Beta 2 and Version 1.0?1/28/2002 7:19:13 PM31100
183How can I calculate the years, months, and days between two dates?2/4/2002 12:05:46 AM59630
184How can I randomly select n lines from a text file?2/8/2002 12:31:14 PM38569
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 PM51422
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 PM50532
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 PM60384
188How do I use regular expressions from an ASP.NET Web page?3/12/2002 12:28:26 PM56460
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 PM62387
190How do I display data on a web page using arrays instead of Do...While...MoveNext...???...4/11/2002 1:14:41 PM168093
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 PM49098
192When using the FSO, my ASP page tends to "hang", but doesn't report any errors.4/30/2002 1:20:32 PM46493
193How can I restrict access to a page or a portion of my Web site?6/2/2002 2:18:24 PM70597
194What is the SQL Injection Attack? Am I at risk? How do I fix such a security hole?6/18/2002 5:42:57 PM39075
195Can I store VBScript class objects in a Session variable?9/3/2002 10:23:05 PM48593
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 PM34845
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 PM32028
198What is XML?2/17/2003 1:12:57 PM36878
199How can I convert database data into XML (in both ASP and ASP.NET)?2/17/2003 1:39:29 PM49112
200What is the easiest way of extracting certain data from an XML document?2/18/2003 10:23:18 PM41001
201What are some of the more advanced regular expression features available with .NET?2/25/2003 10:13:20 PM26470
202I have installed the .NET Framework, but my Web server won't serve ASP.NET Web pages. Help!4/2/2003 1:08:03 PM70277
203What is XSLT and how does it relate to XML?4/6/2003 8:48:31 PM53359
204How can I display XSL-formatted XML data in an ASP.NET Web page?4/7/2003 6:17:13 PM71591
205Why do I get an error message, "Access to the path is denied?"9/3/2003 12:22:43 AM51890
206How do I delete a file from the Web server's file system?9/3/2003 12:24:34 AM49001
207How do I copy or move a file from one location to another?9/3/2003 12:27:35 AM70824
208How do I rename a file or directory?9/3/2003 12:29:08 AM48851
209How do I determine if a file or directory exists?9/3/2003 12:30:01 AM45922
210How can I read the entire contents of a text file into a string?9/3/2003 12:30:37 AM30791
211How can I read the contents of a text file one line at a time?9/3/2003 12:32:03 AM43178
212How can I create an XML document from an ASP.NET Web page?9/21/2003 8:04:57 PM27196
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 PM59704
214How do I send the information a user enters into a form as an email message?3/9/2004 6:40:35 PM31735
215How can I easily work with/manipulate/parse file paths in .NET?5/24/2005 7:25:03 PM18871


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]