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.
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.