Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Dealing with a non-existant table...

Posted on 2006-05-20
3
Medium Priority
?
186 Views
Last Modified: 2010-04-23
Friends,

I have a piece of code that, when activated by a timer, selects a set of results from a particular database.  It works great if there are records or not, but if the table doesn't exist, it breaks.  What syntax is required to deal with a returned message of "Invalid object name 'Results'."?  Ultimately, I want to return 0's for my values, but...

Syntax help requested please!

Here is my code:

Public Sub GetCurrentSessionResults(ByVal dsnCurrentRun As String)

        Try

            Dim data As New DataSet
            Dim r As DataRow

            Dim con As New SqlClient.SqlConnection(dsnCurrentRun)

            'fill in the data for a lap ...
            data = New DataSet

            Dim dr As SqlClient.SqlDataReader

            con = New SqlClient.SqlConnection(dsnCurrentRun)

            con.Open()

            Dim c As New SqlClient.SqlCommand("Select Laps = Sum(Laps) from Results", con)

            dr = c.ExecuteReader(CommandBehavior.SingleResult)

            Dim found As Boolean
            Dim cmb As ComboBox

            Dim i As Integer
            found = False
       
            If dr.HasRows Then
                While dr.Read()
                    If dr.IsDBNull(i) Then
                        CurrentSessionLaps = 0
                        lblCurrentSession.Text = CurrentSessionLaps
                    Else
                        CurrentSessionLaps = dr.Item("Laps")
                        lblCurrentSession.Text = CurrentSessionLaps
                    End If
                End While

            End If

                con.Close()
                con = Nothing

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

Thanks in advance!
0
Comment
Question by:indy500fan
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 2000 total points
ID: 16726251
The simplest approach would be to put

            dr = c.ExecuteReader(CommandBehavior.SingleResult)

            Dim found As Boolean
            Dim cmb As ComboBox

            Dim i As Integer
            found = False
       
            If dr.HasRows Then
                While dr.Read()
                    If dr.IsDBNull(i) Then
                        CurrentSessionLaps = 0
                        lblCurrentSession.Text = CurrentSessionLaps
                    Else
                        CurrentSessionLaps = dr.Item("Laps")
                        lblCurrentSession.Text = CurrentSessionLaps
                    End If
                End While

            End If

within its own Try ... Catch block, with your alternative code to "return 0's for my values" in the Catch part.  A theoretical downside of this is that it would not only catch the non-existent table situation.  An error arising from ANY part of the code quoted here would result in the alternative code being run, giving 0 for your values.  But would that matter in practice?

Roger

0
 

Author Comment

by:indy500fan
ID: 16728216
Sancler,

Hmm...I will try it.
0
 

Author Comment

by:indy500fan
ID: 16728285
Sancler,

Well, that simple suggestion works.

Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question