Simple DataGrid Demo

This demo shows how to bind the results of a database query to a DataGrid. In future live demos, we'll extend this code to include paging support!


FAQIDDescription
2How do you convert a string to all uppercase or all lowercase?
7How do I dynamically resize an array?
8How do you create a database table through a SQL statement (via an ASP page, perhaps)?
10How can I tell what version of ADO I am using?
11How can I have my ASP page send HTML-formatted email?
12How can I display line breaks in an email message?
13How do you send email attachments through an ASP page?
14Can I send emails without using CDONTS?
15How can I determine the length of a string (how many characters are in a string)?
16Using VBScript, how can I determine if a variable is a valid date or not?
17How can I find the position of a certain character (or substring) within a string?
18How can I easily display a nicely formatted date variable?
19How can I return the current date/time?
20What is recursion?
21How can I determine the upper or lower bounds of an array?
22How can I validate an email adress using Jscript?
23What, exactly, is the FileSystemObject?
24How can I read the contents of a text file through an ASP page?
25How can I send an email using CDONTS?
26How can I convert the contents of an array into a string?
27How do I get SQL to accept an apostrophe in various queries, such as when I try to add a user named "O'Brien"?
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?
29How can I determine if a visitor has cookies support enabled in his/her browser?
30For session variables to work, must the Web visitor have cookies enabled?
31How do I set a string variable so that it has quotes in it? I want a variable to equal: Bob said, "Hello."
32Why are there certain times when I can use parenthesis when calling a Subroutine, and other times not?
33I've seen function definitions that include ByRef and ByVal statements. What are these?
34How can I copy a file using ASP?
35How can I display dates in a completely custom format (that might not be supported by FormatDateTime)?
36How can I rename a file on my Web site through an ASP page?
37How can I page database results N records at a time?
38How can I dynamically change the action of a form?
41How can I delete an existing cookie?
42How can I make an argument of a Function or Sub optional?
43How can I determine the number of days in a given month?
44How can I display Recordset data in an HTML TABLE? How can I get the names of all the fields in a table?
45How can I ensure that a form field has a particular value? (That is, how can I perform form validation?)
46How can I get the path of the system temporary folder? Also, how can I get a filename for a temporary file?
47How can I validate an email address using VBscript?
48How can I add or subtract time from a date?
49Can I send my output to a different window or frame than the one that asked for the current ASP page?
50Can I disable the use of session variables on an ASP page-by-page basis?
51How can I order the results from a database query?
52How can I display all of the cookies/cookie values for a user through a Web page?
53How can I change a string like $51,234.98 into a number (51234.98)
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)?
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?
57Okay, if I can't do a conditional #include then how can I conditionally include things in an ASP page?
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?
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?
60How can I retrieve emails from a service like Yahoo! Mail?
61How can I treat some other web page as data on my own site?
62How can I read the contents of a file, line-by-line, into an array?
63How do I put an array into a Session or Application "variable"? How do I then use it on the next ASP page?
64How can I retrieve the fields in my form in the order they appear in the form?
65How can I pick random records from a table?
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?
67How can I call an executable program from my ASP page?
68How can I eliminate duplicates from a recordset?
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?
70How big can my Access Database be?
71Using CDONTS, how can I set a Reply To email address?
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?
73How can I time the execution speed of my ASP pages to the millisecond?
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?"
75Will I ruin the performance of my application if I use session variables?
76How can I run a stored procedure from ASP using only the ADO connection object?
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?
78How do I find out if a file exists?
79**UPDATED 1 April 2002** How do I get the record number of a just added record?
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!
81Why do I get the message Microsoft JET Database Engine error ' 80040e14' Syntax error in INSERT INTO statement??
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?
83How can I quickly sort a VBScript array?
84How can I display all of the contents of a single-dimension array?
85How can I disable the back button?
86How do I extract the contents of an array without knowing the size of the array?
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)
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)?
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?
90How can I programmatically list the contents of a directory on the Web server?
91How can I replace each occurrence of a particular pattern in a string with another pattern?
92How can I determine the position of a particular string in an Array?
93How can I quickly determine if a string exists within an array?
94How can I convert a string into an array?
95What is Transact-SQL (T-SQL)?
96How can I capture and use (i.e. save to disk, let the browser download) the output of one of my own ASP pages?
97How can I get a count of the number of dimensions in an array?
98What scripting languages can utlitize regular expressions?
99How can I use regular expressions to dynamically place delimiters around certain strings?
100How can I list all of the existing Session variables for a particular user?
101How can I list all of the Application variables on a Web page?
102How do I use CASE in Transact SQL?
105How can I make inheritable classes in ASP?
106How can I find out if a record already exists in a database? If it doesn't, I want to add it.
107** UPDATED 24 June 2003 **: Why am I getting "Operation must use an updateable query" errors?
108How can I delete a file from the Web server's file system?
109How can I create a folder (directory) on the Web server?
110What is CrossPosting? Why is it considered a bad thing?
111What is the VBScript Integer Division operator and how can it help my code?
112How do I send variables and data from client-side JavaScript to server-side ASP?
113How can I use RecordSet.GetString to create a drop down list? How about other more complex output using GetString?
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?
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?
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?
117How can I insert Printer Page Breaks in ASP/HTML?
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...
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?
122How can I create a chart or graph that I can display to the user?
123What is adovbs.inc? Why should I use it? What benefits does it have?
124How can I perform a Reverse DSN Lookup through ASP?
125How can I determine the file size of a file on the Web server?
126How do I populate an HTML dropdown box from the results of a database table?
127How can I count the number of substrings in a string?
128How can I convert binary data into a string?
129Can I create an application-level Dictionary object?
130How can I use regular expressions to help validate that a user-entered value is in a proper format?
131How can I have my Web visitors upload a file from their computer to the Web server without using a component?
132How can I create my own file uploading component?
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?
134How can I execute ASP pages on Windows 9X? (How do I install Personal Web Server?)
135I've got ASP installed on my computer. What version of ASP am I using, though?
136How can I perform advanced networking functions through an ASP page, such as SNMP, FTP, WhoIs lookups, etc.?
137Why do I get an error when I try to insert a value into a database that contains an apostrophe?
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)
139Why does the .RecordCount property return a value of -1?
140How can I show a table of records from a recordset and let my users choose which field (column) to sort on?
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.)
142How can I use application-level variables to cache information?
143How can I create a file with a random file name?
145How is an array stored in memory? I am interested in learning about the internals of an array!
146How is a two-dimensional laid out in memory?
147I want to repeat a character or string X number of times, is there a way I can do this without expensive looping operations?
148How can I create a text file on the Web server's file system?
149How can I remove multiple spaces between words in a string? That is, if I have: Hi    there how can I get: Hi there?
150What databases can ASP work with? How do technologies like ADO, OLE-DB, and ODBC fit in?
151How can I count the total number of words that appear in a string?
152What are some issues that may arise when using dates and working with databases?
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?
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?
155How can I strip the HTML tags from a text string using regular expressions?
156What third-party file uploading components are available?
157What is ASP.NET?
158What do I need to start working with ASP.NET?
159Where can I learn more about ASP.NET?
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?
161How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset?
162What are the differences between ASP.NET Beta 1 and Beta 2?
163How do I convert a string into an Integer?
164How can I print a web page in ASP?/Can I bypass the printer dialog box?
165How can I rename a directory on my Web server?
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!
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?
170How do I post in a message board (and get answers)?
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?
172How can I display images that reside in my database through an ASP page?
173How can I pick a random record from a table, using a stored procedure?
174Why do I get a closed recordset from my stored procedure? Why don't I get the expected fields from the stored procedure?
175Is ASP.NET output compatible with all browsers? If not, what browsers is the output compatible with?
176How can I delete files by extension in a given directory? What if I have multiple file extension names I want to delete?
177How much will ASP.NET (the .NET Framework) cost once Microsoft is finished with Beta testing and ready to roll a "final" version?
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?
179How can I determine the location of user by their IP, or validate the identity of a user based on his IP?
180How can I install Personal Web Server (PWS) onto Windows XP Home edition?
181How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.
182What are the differences between ASP.NET Beta 2 and Version 1.0?
183How can I calculate the years, months, and days between two dates?
184How can I randomly select n lines from a text file?
185What are the differences between C# and VB.NET, and which language should I use to create my ASP.NET Web pages?
186I have many duplicate records in my database table. I want to delete all of them except one. How can I do this?
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?
188How do I use regular expressions from an ASP.NET Web page?
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?
190How do I display data on a web page using arrays instead of Do...While...MoveNext...???...
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?
192When using the FSO, my ASP page tends to "hang", but doesn't report any errors.
193How can I restrict access to a page or a portion of my Web site?
194What is the SQL Injection Attack? Am I at risk? How do I fix such a security hole?
195Can I store VBScript class objects in a Session variable?
196How can I share cookies between my server-side scripts (ASP) and my client-side scripts (e.g., JavaScript in the browswer)?
197What is the Cross Site Scripting security vulnerability? Am I at risk? How do I plug this security hole?
198What is XML?
199How can I convert database data into XML (in both ASP and ASP.NET)?
200What is the easiest way of extracting certain data from an XML document?
201What are some of the more advanced regular expression features available with .NET?
202I have installed the .NET Framework, but my Web server won't serve ASP.NET Web pages. Help!
203What is XSLT and how does it relate to XML?
204How can I display XSL-formatted XML data in an ASP.NET Web page?
205Why do I get an error message, "Access to the path is denied?"
206How do I delete a file from the Web server's file system?
207How do I copy or move a file from one location to another?
208How do I rename a file or directory?
209How do I determine if a file or directory exists?
210How can I read the entire contents of a text file into a string?
211How can I read the contents of a text file one line at a time?
212How can I create an XML document from an ASP.NET Web page?
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?
214How do I send the information a user enters into a form as an email message?
215How can I easily work with/manipulate/parse file paths in .NET?


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 FROM tblFAQs ORDER BY FAQID"
    Dim myCommand as New SqlCommand(strSQL, myConnection)

    'Set the datagrid's datasource to the datareader and databind
    myConnection.Open()
    dgPopularFAQs.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    dgPopularFAQs.DataBind()	
  End Sub
</script>

	<asp:DataGrid runat="server" id="dgFAQs" 
	                BackColor="#eeeeee" Width="85%"
	                HorizontalAlign="Center"
	                Font-Name="Verdana"
	                Font-Size="10pt">
	  <HeaderStyle BackColor="Black" ForeColor="White" Font-Bold="True"
			 HorizontalAlign="Center" />
	  <AlternatingItemStyle BackColor="White" />
	</asp:datagrid>
	


[Return to the article]