How to Create ASP.NET inline Table using data from a DataReader?

Customization in a web app is very common. Requirement updates everyday. Depending on this it happens occasionally Gridview is incapable to meet customers demand. In this case we choose ASP.NET inline Table to present data. Commonly there are 2 ways to fetch data from the database DataSet & DataReader. DataSet is connection less architecture. While DataReader is a pointer. DataReader fetch data faster then DataSet mechanism.

Introduction to ASP.NET DataReader

ASP.NET is a powerful framework for building web applications, and one of its key features is the ability to retrieve and display data from databases. The DataReader is a fast, forward-only, read-only cursor designed for retrieving data from a database. It is part of the ADO.NET framework, which provides a bridge between the front end and data sources.

Using a DataReader in ASP.NET allows developers to efficiently read data from a database and display it in various formats. One common requirement is to present this data in an inline table format within a web page. This article will guide you through the process of creating an inline table using data from a DataReader.

Here in this below example I am fetching data from my table PortalWebLink. In side table tag before start the row (tr) I am with a while loop. The condition is until data read is readable bind data. Inside the td I am binding weblinks using dreaderobj(“WebLinkTitle”).ToString().

Table using data from a DataReader

<table border="0" cellpadding="4" cellspacing="1" width="100%" bgcolor="#e3eef6">
<%
Dim connObj As New SqlConnection(ConfigurationManager.AppSettings.Get("DBKey").ToString())
Dim qryStr As String = "SELECT * FROM PortalWebLink WHERE STATUS='ACTIVE' ORDER BY CreatedDate DESC"
'Response.Write(qryStr)
Dim sqlCommObj As New SqlCommand(qryStr, connObj)

Try
connObj.Open()
Dim dreaderobj As SqlDataReader = sqlCommObj.ExecuteReader

While dreaderobj.Read
%>
<tr>
<td bgcolor="#e3eef6">
<table border='0' cellpadding='0' cellspacing='0'>
<tr>
<td>
<img src='../IMAGES/old-edit-redo.png' />
</td>
<td>
&nbsp;<a href='<%= dreaderobj("WebLink").ToString %>' target='blank'><%=dreaderobj("WebLinkTitle").ToString()%></a>
</td>
</tr>
</table>
</td>
</tr>
<%
End While
Catch ex As Exception
Dim TrackError As New ErrorLog()
TrackError.ErrorLog(ex.Message, ex.ToString(), ex.Source, ex.TargetSite.ToString(), "", "../Admin/Weblink.aspx", connObj)
Finally
connObj.Close()
End Try
%>
<%
Dim strChkQry As String = "SELECT Count(*) as CNT FROM PortalWebLink WHERE STATUS='ACTIVE'"
Dim sqlCommandObj As New SqlCommand(strChkQry, connObj)

try
connObj.Open()
Dim str As Object = sqlCommandObj.ExecuteScalar()
Dim i As Integer = str

If i = 0 Then
%>
<table cellpadding="2" cellspacing="2" width="100%" class="fbox">
<tr>
<td>
No Record(s) Found.
</td>
</tr>
</table>
<%
end if
Catch ex As Exception
Dim TrackError As New ErrorLog()
TrackError.ErrorLog(ex.Message, ex.ToString(), ex.Source, ex.TargetSite.ToString(), "", "../Admin/Weblink.aspx", connObj)
Finally
connObj.Close()
End Try
%>
<tr>
<td>
&nbsp;
</td>
</tr>
</table>

Conclusion

Using ASP.NET and a DataReader to create an inline table is a straightforward process that involves connecting to a database, retrieving data, and rendering it in a structured format on a web page. This method is efficient for displaying data due to the DataReader’s lightweight and forward-only nature.

As you continue to develop applications, consider how to expand upon this basic model, such as adding sorting and filtering functionalities or integrating with front-end frameworks for enhanced interactivity. With a solid understanding of these concepts, you are well on your way to building robust data-driven web applications.