Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do you modify code so that it will not crash if the database server is unavailable when you attempt to access it? If this happens then there must be an appropriate way to handle the error. I do not know where to put the Try..Catch procedure.

VB


VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim db As New DBWrapper
        Dim l As ArrayList



        If Not IsPostBack Then
            Dim o As New Order
            o = Session("CurrentOrder")
            'Code so the program will not crash if the database server is unavailable when trying to access it.
            ' Try
            'Do something
            'Catch ex As Exception
            'Handle exception by displaying a message box
            'msgbox("Error happened because the database is not available" & ex.tostring())
            'Finally
            'dbwrapper.End
            ' End Try
            l = db.GetCustomersForLookup
            lstCustomer.DataSource = l

            lstCustomer.DataValueField = "Value"
            lstCustomer.DataTextField = "Description"
            lstCustomer.DataBind()


            If o Is Nothing Then

                calOrderDate.SelectedDate = Date.Today

            Else
                calOrderDate.SelectedDate = o.OrderDate


            End If

            SetFormToInitialState()
        End If


    End Sub
Posted
Updated 29-Apr-12 16:54pm
v2

Use error handling such as try...catch

Simply just do this
VB
try
 <some code="" to="" use="" with="" database="">
catch (e as exception)
 <some code="" to="" do="" if="" above="" for="" example="">
 msgbox("Error happened at "+e.toString())
end try</some></some>
 
Share this answer
 
You wrap try-catch around the code that has potential of getting an error.

Here, in your code the line that tries to connect to DB and get back data is one of those places where probability of having an error is high.
Wrap your try-catch around the following line:
C#
l = db.GetCustomersForLookup
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900