[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

Check for the existence of a table within a dataset

Hi,
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"))

Thanks.
0
jtylerhill
Asked:
jtylerhill
  • 2
1 Solution
 
anvCommented:
hi

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
else
   Found = False
end if

next

if Found = true then
   'do some thinsg as the table exists in the database
else
   'Table Doesnot Exists so
   MessageBox.Show("Table Doesnot Exists")
end if
0
 
jtylerhillAuthor Commented:
Thanks!
0
 
jtylerhillAuthor Commented:
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
Next

Thanks again anv!
0

Featured Post

Independent Software Vendors: 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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now