An error has occurred: System.Data.SqlClient.SqlException: SQL Server does not exist or access denied. at System.Data.SqlClient.ConnectionPool.GetConnection(Boolean& isInTransaction) at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString options, Boolean& isInTransaction) at System.Data.SqlClient.SqlConnection.Open() at ASP.dgwithrownumbers_aspx.Page_Load(Object sender, EventArgs e) ASPAlliance.com : The #1 ASP.NET Developer Community : ASP.NET: Code Snippet - DataGrid with line numbers
ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
Essential ADO .NET
Essential ADO .NET

Find Prices
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software
Click here to return to my article index

ASP.NET: Code Snippet - DataGrid with line numbers

Updated - 5/22/2002
In this article, we will look at a technique that will allow us to generate row numbers in the first column of a DataGrid. To generate the row numbers, we will use the DataGridItem's ItemIndex property. Nikhil Kothari (ASP.NET team member extraordinaire) was kind enough to inform me that the ItemIndex property was created for just such a scenario. As such, the sample below is very simple. We will create an extra TemplateColumn in our DataGrid and bind to it the value of Container.ItemIndex. The ItemIndex property is zero based so we will add one to it and voila, we have a row counter in our DataGrid.

The code sample below uses a couple other best pratices you should understand and be aware of:
*The code below uses structured error handling techniques in the form of the Try...Catch statement. Go here to read more about the great new structured error handling we VBers now have.
*The code below also uses a connection string stored in a web.config configuration file. Go here to read more about the web.config configuration file and how it can be used to store application wide data.

The Code:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="VB" runat="server">

    Sub Page_Load(sender As Object, e As EventArgs) 

	'Create connection object
	Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("DSN_aspa"))

        	'Create command object
	Dim myCommand As New SqlCommand("SELECT PersonName,PersonAge,PersonGender FROM tblPeople", myConnection)

        	'Create SqlDataReader
	Dim myDataReader As SqlDataReader

	Try
	   'Open Connection
	   myConnection.Open()

	   'Fill myDataReader using ExecuteReader method. 
	   'Close connection using CommandBehavior.CloseConnection enumeration
	   myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

	   'Set datasource and bind DataGrid to datasource
	   myDataGrid.DataSource = myDataReader
	   myDataGrid.DataBind()
	Catch myException As Exception
	   Response.Write("An error has occurred: " & myException.ToString())
	Finally
	   If Not myDataReader Is Nothing Then
	     'Close SqlDataReader  
	     myDataReader.Close()
	   End If
	End Try
    
    End Sub

</script>
<html>
   <body>
        <form runat="server">
            <asp:datagrid id="myDataGrid" 
			  runat="server" 
			  AutoGenerateColumns="false" >

                <columns>
	        <asp:templatecolumn headertext="Row Number">
                            <itemtemplate>
                                <span><%# Container.ItemIndex+1 %></span>
                            </itemtemplate>
	        </asp:templatecolumn>
                        <asp:templatecolumn headertext="Name">
                            <itemtemplate>
                                <span><%# Container.DataItem("PersonName") %></span>
                            </itemtemplate>
                        </asp:templatecolumn>
                        <asp:templatecolumn headertext="Age">
                            <itemtemplate>
                                <span><%# Container.DataItem("PersonAge") %></span>
                            </itemtemplate>
                        </asp:templatecolumn>
                        <asp:templatecolumn headertext="Gender">
                            <itemtemplate>
                                <span><%# Container.DataItem("PersonGender") %></span>
                            </itemtemplate>
                        </asp:templatecolumn>
                </columns>

            </asp:datagrid>
        </form>
   </body>
</html>

The Result: