Check for the existence of a table within a dataset

Posted on 2004-11-29
Last Modified: 2012-06-21
I'm building a dataset dynamically and the dataset contains several tables (which are named dynamically).  Obviously when I try to access the data at a later point, if I reference a table that doesn't exist I will get an error.  How can I first check to see if a table exists within the dataset?  (e.g. if exists dataset.tables("table1"))

Question by:jtylerhill
    LVL 10

    Accepted Solution


    u need to use a DataSet, and a DataTable Object

    'here's is the Algo

    dim i!, Found as boolean

    Found = False
    'i stores the number of tables in the dataset

    For each Table in the DataSet

    fo j = 0 to i
    dt= ds.Tables.Item(j)
    if strcomp(dt.TableName(),"nametoSearch",CompareMethod.Text)=0 then
       'Table is Found
        Found = True
       exit for
       Found = False
    end if


    if Found = true then
       'do some thinsg as the table exists in the database
       'Table Doesnot Exists so
       MessageBox.Show("Table Doesnot Exists")
    end if

    Author Comment


    Author Comment

    In case anybody needs this in the future, below is how I implemented  anv's solution:

    Dim x, i As Integer
    Dim dt As DataTable
    x = MyDataSet.Tables.Count - 1
    For i = 0 To x
       dt = dsKeys.Tables(i)
       If StrComp(dt.TableName(), "MyTableName", CompareMethod.Text) = 0 Then
          MsgBox("Table Found")
       End If

    Thanks again anv!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now