Using the ASP.NET Stringbuilder class

Contributed by
Rating: 3 stars3 stars3 stars3 stars3 stars / 35
January 06, 2002
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Using ASP.NET Stringbuilder Class



I (Steve Schofield) recently put together a webpage that wrote out some formatted data. There are many ways the .NET framework presents in formatting the output to the webpage. One of the most efficient and flexible ways of doing such tasks is using the Stringbuilder class. Here is a simple example I used recently to build a simple table to output. This example uses MSDE and the pubs database.

The code

<%@ Page EnableSessionState="False" EnableViewState="False" Debug="False" Trace="False" strict="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import NameSpace="System.Data.SqlClient" %>
<script language="vb" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
Dim i, k As Integer
dim theSQL as string = "SELECT au_id, au_lname, au_fname, phone, address, " & _
"city, state, zip, contract FROM Authors"
'I don't ever embed the connection string inside aspx page.
'Use the System.Configuration class to extract

'the value from the appSettings
Dim myConn as SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))
dim ds as dataset = new dataset()
dim dt as datatable
dim adapter as sqldataadapter = new SqlDataAdapter(theSQL, myConn)

myConn.Open()
adapter.Fill(ds,"DataTable")
myConn.Close()

dt = ds.Tables("DataTable")

'Set two variables to the number of rows and columns the datatable has

Dim RowCount As Integer = dt.rows.count
Dim ColCount As Integer = dt.Columns.Count

'Loop through the datatable to format the output
Dim sb as StringBuilder = New StringBuilder()
sb.append( "<table border='1' width='80%'>" )
For i = 0 To RowCount - 1
sb.Append("<tr>")
For k = 0 To ColCount - 1
sb.Append("<td>")
sb.Append( dt.Rows(i).Item(k, DataRowVersion.Current).toString())
sb.Append( "</td>" )
Next
sb.Append("<tr>")
Next
sb.Append( "</table>")

'Output to the Label control that will show the output
Dim strOutput as String = sb.ToString()
lblCompany.Text = strOutput
'Uses a hyperlink server control to display a link
returnHome.NavigateUrl = "/aspnet/Default.aspx"
End Sub
</script>
<html>
<head><title>Using StringBuilder Class</title></head>
<body>
<asp:hyperlink id=returnHome runat="server">
Back to the Article
</asp:hyperlink>
<asp:label id="lblCompany" runat="server" />
</body>
</html>

Web.Config that holds the connection string

<configuration>
<appSettings>
<add key="DSN_pubs" value="server=localhost;uid=sa;pwd=;database=pubs" />
</appSettings>
</configuration>

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials