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! | Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
| | | | Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services. FREE! Go There Now!
| | | | Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
| | | | As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security. FREE! Go There Now!
| | | | Whether you are creating new applications or modifying existing ones, managing integration of new components with traditional z/OS elements is a critical part of building and deploying modern applications. Listen to this webcast to see how IBM can help you optimize your development process using an IDE like Rational Developer for System z that integrates with management tools, such as ClearCase to manage your application development on mainframes. FREE! Go There Now!
| | | | Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
| | | | The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |