Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to catch a SqlException?

How to catch a SqlException?

Scheduled Pinned Locked Moved Visual Basic
databasesecuritytutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Benny_Lava
    wrote on last edited by
    #1

    Hi, this is my code: Try Dim DbTest As SqlClient.SqlConnection DbTest = New System.Data.SqlClient.SqlConnection("Data Source=(local)\SQL;Initial Catalog=testiranje123;Trusted_Connection=yes;Integrated Security=True;Asynchronous Processing=true") DbTest.Open() DbTest.Close() Catch ex As SqlException MsgBox("idemo dalje") Catch ex As DataException MsgBox("idemo dalje") Catch ex As Exception MsgBox("idemo dalje") End Try the connection string is wrong, i did this on purpose. So i can catch the exception and handle it and build a connection string change mechanism... the exception is: SqlException: Cannot open database "testiranje123" requested by the login. The login failed. Login failed for user 'test\test'. There is no way I could catch the exception. I tried all kinds of catch exception handlers but no luck the exception just keeps showing up. Please, if anybody knows the answer, it is urgent. Thank you very much!

    B 1 Reply Last reply
    0
    • B Benny_Lava

      Hi, this is my code: Try Dim DbTest As SqlClient.SqlConnection DbTest = New System.Data.SqlClient.SqlConnection("Data Source=(local)\SQL;Initial Catalog=testiranje123;Trusted_Connection=yes;Integrated Security=True;Asynchronous Processing=true") DbTest.Open() DbTest.Close() Catch ex As SqlException MsgBox("idemo dalje") Catch ex As DataException MsgBox("idemo dalje") Catch ex As Exception MsgBox("idemo dalje") End Try the connection string is wrong, i did this on purpose. So i can catch the exception and handle it and build a connection string change mechanism... the exception is: SqlException: Cannot open database "testiranje123" requested by the login. The login failed. Login failed for user 'test\test'. There is no way I could catch the exception. I tried all kinds of catch exception handlers but no luck the exception just keeps showing up. Please, if anybody knows the answer, it is urgent. Thank you very much!

      B Offline
      B Offline
      Burnsys2
      wrote on last edited by
      #2

      You mean that when you run that code the msgbox never appears? instead it stops at DbTest.Open with an exception?

      B 1 Reply Last reply
      0
      • B Burnsys2

        You mean that when you run that code the msgbox never appears? instead it stops at DbTest.Open with an exception?

        B Offline
        B Offline
        Benny_Lava
        wrote on last edited by
        #3

        Yes, that is what I mean, Thank you. I can not catch the exception it keeps showing up. But i want to catch it and handle it. The msgbox is just a test to see if it can catch it. But the msgbox never shows up. Please, if you know the answer share it with me! Thank you.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups