Results 1 to 8 of 8

Thread: Custom Errors when unable to connect to database

  1. #1
    Join Date
    Oct 2005
    Location
    Manchester
    Posts
    155
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Custom Errors when unable to connect to database

    I am a bit of an ASP novice but I was wondering if it is possible to display a static HTML page when for example my site is unable to connect to the MSSQL database, or say Hydrogen is not responding?

    Rather than display the default
    Microsoft OLE DB Provider for SQL Server error '80004005'
    [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
    Cheers

  2. #2
    Join Date
    Feb 2004
    Posts
    4,903
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Show me the code that does the connection to your database.
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  3. #3
    Join Date
    Oct 2005
    Location
    Manchester
    Posts
    155
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    I have a include called inc_func.asp which is called at the top of every page.

    This is the code used for accessing or opening the database.

    Code:
    Sub OpenDB()
    
     ' Opens a new ADO connection
     ' sConnLoc = "PROVIDER=" & dbProvider & ";DATA SOURCE="& dbPath & ";"
    
       sConnLoc = "Provider=SqlOLEDB;Network Library=DBMSSOCN;" & _ 
                "Data Source=HYDROGEN;" & _
                "Initial Catalog=xxxxx;" & _
                "User Id=xxxxx;" & _
                "Password=xxxxx"
            
      If Not ConnectionExists() Then
       ' Open a database connection
         Set conn = Server.CreateObject("ADODB.Connection")
       ' Response.Write(sConnLoc)
         conn.Open sConnLoc
       ' The next line is for compatibility with existing code...
         OpenRS
       'OpenCmd
      End If
    
    End Sub
    If I have missed anything out apologies.

  4. #4
    Join Date
    Feb 2004
    Posts
    4,903
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Code:
    Sub OpenDB()
      On Error Resume Next
    
    ' Opens a new ADO connection
    ' sConnLoc = "PROVIDER=" & dbProvider & ";DATA SOURCE="& dbPath & ";"
    
       sConnLoc = "Provider=SqlOLEDB;Network Library=DBMSSOCN;" & _ 
                "Data Source=HYDROGEN;" & _
                "Initial Catalog=xxxxx;" & _
                "User Id=xxxxx;" & _
                "Password=xxxxx"
            
      If Not ConnectionExists() Then
       ' Open a database connection
         Set conn = Server.CreateObject("ADODB.Connection")
       ' Response.Write(sConnLoc)
         conn.Open sConnLoc
       ' The next line is for compatibility with existing code...
         OpenRS
       'OpenCmd
      End If
    
      If Not Err.Number = 0 Then
        Response.Redirect "http://www.google.com"
      End If
    End Sub
    This will redirect when it cant connect.
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  5. #5
    Join Date
    Oct 2005
    Location
    Manchester
    Posts
    155
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    I can't thank you enough Warren

  6. #6
    Join Date
    Oct 2005
    Posts
    256
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Warren Ashcroft
    Code:
    Sub OpenDB()
      On Error Resume Next
    
    ' Opens a new ADO connection
    ' sConnLoc = "PROVIDER=" & dbProvider & ";DATA SOURCE="& dbPath & ";"
    
       sConnLoc = "Provider=SqlOLEDB;Network Library=DBMSSOCN;" & _ 
                "Data Source=HYDROGEN;" & _
                "Initial Catalog=xxxxx;" & _
                "User Id=xxxxx;" & _
                "Password=xxxxx"
            
      If Not ConnectionExists() Then
       ' Open a database connection
         Set conn = Server.CreateObject("ADODB.Connection")
       ' Response.Write(sConnLoc)
         conn.Open sConnLoc
       ' The next line is for compatibility with existing code...
         OpenRS
       'OpenCmd
      End If
    
      If Not Err.Number = 0 Then
        Response.Redirect "http://www.google.com"
      End If
    
      ' ADDED:
      On Error Goto 0
    
    End Sub
    This will redirect when it cant connect.
    I've modified the code above so that it resets the error handler. If you get a subsequent error then it would not be trapped if this didn't exist and you might get unexpected behaviour (or at least a headache working out what is wrong). I spent 5 hours on Thursday fixing one of these

    Hope this helps.

  7. #7
    Join Date
    Feb 2004
    Posts
    4,903
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Yup, well spotted
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  8. #8
    Join Date
    Oct 2005
    Location
    Manchester
    Posts
    155
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thanks cswd, I've made the changes, very much appreciated.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 2nd August 2006, 03:04 PM
  2. Can't connect to SQL 2005
    By cswd in forum MSSQL
    Replies: 13
    Last Post: 3rd June 2006, 03:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •