Close database connection : Close Connection « ADO.net Database « ASP.Net

ASP.Net
1. ADO.net Database
2. Asp Control
3. Collections
4. Components
5. Data Binding
6. Development
7. HTML Control
8. Mobile Control
9. Page
10. Request
11. Response
12. Server
13. Session Cookie
14. User Control and Master Page
15. Validation by Control
16. Validation by Function
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
ASP.Net » ADO.net Database » Close Connection 
Close database connection

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
   <title>Try-Catch-Finally Example</title>
   <head>
      <script runat="server">
         Sub Page_Load()
     '    Dim ConnStr As String = "Data Source=(local)\NetSDK;" & _
      '      "Initial Catalog=Pubs;Trusted_Connection=True;"

            Dim ConnStr As String = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=java2suser;Password='password';"
            Dim SQL As String = "SELECT ID, FirstName FROM Employee " & _ 
               "WHERE ID IS NOT NULL"
            Dim PubsConn As New SqlConnection(ConnStr)
            Dim TitlesCmd As New SqlCommand(SQL, PubsConn)
            Dim Titles As SqlDataReader
            Try
               PubsConn.Open()
               Titles = TitlesCmd.ExecuteReader()
               Output.Text = "<table>"
               While Titles.Read()
                  Output.Text &= "<tr>"
                  Output.Text &= "<td>" & Titles.GetString(0"</td>"
'                  Output.Text &= "<td>$" & _
 '                    Format(Titles.GetDecimal(1)"##0.00""</td>"
                  Output.Text &= "</tr>"
               End While
               Output.Text &= "</table>"
            Catch sqlEx As SqlException
               Response.Write("A SqlException has occurred.")
            Catch ex As Exception
               Response.Write("An Exception has occurred.")
            Finally
               If Not Titles Is Nothing Then
                  If Not Titles.IsClosed Then
                     Titles.Close()
                  End If
               End If
               If PubsConn.State = ConnectionState.Open Then
                  PubsConn.Close()
               End If
            End Try
            Response.Write("<br/>The current connection state is: " & _
               PubsConn.State.ToString() ".")
         End Sub
      </script>
   </head>
<body>
   <h1>SqlDataReader Example</h1>
   <asp:label id="Output" runat="server"/>
</body>
</html>
           
       
Related examples in the same category
w___w__w__._j___a_v__a2___s_.___c_o___m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.