Alert user if no connection to SQL

johncassell

Well-known member
Joined
Jun 10, 2007
Messages
120
Location
Redcar, England
Programming Experience
Beginner
Hi There,

I was running my application when it crashed on me. I realised that the machine I have my SQL server running on was turned off.

Can someone show me how to code it so that I can alert the user if there is no connection to the DB for whatever reason?

Thanks

John
 
you can just put your code in try blocks and handle your errors like that. Not only will it solve this issue but it is good programming practice to do so. You can catch sql exception and alert the user the message from exception object and it should state that a connection could not be made to the databse.

VB.NET:
Expand Collapse Copy
Try
  connection.open()
Catch ex as SqlClient.SqlException
  msgbox("Connection Failed: " & ex.message)
Finally
'code to execute no matter what
End try
 
Last edited:
Can someone show me how to code it so that I can alert the user if there is no connection to the DB for whatever reason?

Thanks

John

To some extent, you cant. If your sql server jsut dies while your app is sitting there, then you wont find out until the next time you try to use an sql command. As SS says, use try/catch error handling blocks when you establish your connections (or if youre using tableadapters when you Update)
 
Back
Top