Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to Do Something if No Server Connection 1

Status
Not open for further replies.

dataman86

Technical User
Oct 11, 2008
104
0
0
US
I have a Sql Statement that is pulling records based on Parameters entered in. I need to place a Try and Catch before the oCn.Open() in my coding so as to handle an exception that occurs when the Sql Connection is down.

My Coding:

Public Class Pull
Dim Irows As Integer
Dim _g As New PublicVars
Private Function TestIt() As Array

Dim sParm As String = Chr(39) & Me.TextBox1.Text.ToString & Chr(39)
Dim tParm As String = Chr(39) & Me.TextBox2.Text.ToString & Chr(39)
Dim uParm As String = Chr(39) & Me.TextBox3.Text.ToString & Chr(39)
Dim vParm As String = Chr(39) & Me.TextBox4.Text.ToString & Chr(39)
Dim wParm As String = Chr(39) & Me.TextBox5.Text.ToString & Chr(39)
Dim connectionString As String = "Data Source=10.3.3.123; " & _
"Initial Catalog=PROD; " & _
"Persist Security Info=True; " & _
"User ID=MakOne; " & _
"Password=MakOne"

Dim sSQL As String = "SELECT LINE_NBR, COMPANY, VAR_LEVELS, OBJ_ID, ACCT_UNIT, ACCOUNT, TRAN_AMOUNT FROM GLTRANS WHERE COMPANY = " & sParm & " AND FISCAL_YEAR = " & tParm & " AND ACCT_PERIOD = " & uParm & " AND CONTROL_GROUP = " & vParm & " AND POSTING_DATE = " & wParm & ";"
Dim oCn As New SqlConnection(connectionString)
Dim oCm As New SqlCommand(sSQL, oCn)
oCm.CommandType = CommandType.Text

Dim oDa As New SqlDataAdapter(oCm)
Dim oTable As New DataTable()


oCn.Open()


I need to place the oCn.Open inside a Try and Catch, but I do not know how to word the coding to catch an Exception when the Server Connection is down.

Any suggestions. I need an error Message to be placed somewhere inside the Try and Catch as well that lets the user know the Server is Down.

Dataman86.
 

Try
oCn.Open()
Catch ex As Exception
MsgBox("Database Connection Failed. Error Message: " & ex.Message)
End Try

This will work for any error returned.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top