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> | | DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP.NET Articles More By aspfree developerWorks - FREE Tools! | Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance. FREE! Go There Now!
| | | | Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan. FREE! Go There Now!
| | | | Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle. FREE! Go There Now!
| | | | Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
| | | | Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
| | | | This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
| | | | This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
| | | | Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |