I'm returning over 30 tables in a sql stored procedure which come back in a dataset.

I store this in session state and hold for 20 mins before refreshing.

My problem is, I need to be able to search the dataset for a particular table by name.

At the moment I am hard coding numbers e.g. dataset.Tables[0], where as I need to be able to say dataset.Tables["AgeRanges"].

I know that my tables are coming back from sql with default names of Table, Table1, Table2 etc.

Can someone help me out with this? Is there is a better way to do it?

thanks

Recommended Answers

All 11 Replies

//check condition
if(dataSet1.Tables[0].TableName == "Table_Name")

//getting tables

and if you have all the 30 tables together in the DataSet then yes you can use them by

dataSet1.Tables["Table_name"]......

but you must be very accuratly use the names of tables which are present in dataset to avoid exceptions :)

I know I can use this syntax but my problem is, the tables are returned from sql with default names of Table, Table1, Table2.... Table29 etc.

Whereas my tables in the database are named e.g. tableAgeRanges, tableCustomers, tableAccounts etc.


So if I say dataset.Tables["tableAgeRanges"] the outcome is null as this table has been named "Table" by c#

How they are named by Table1, Table 2?/?????
I am using sql and when I get the tables their names remain same as they are in databases I can't understand can you please explain may be than i can explain any thing?

How they are named by Table1, Table 2?/?????
I am using sql and when I get the tables their names remain same as they are in databases I can't understand can you please explain may be than i can explain any thing?

In my sql stored procedure i call;

select * from firstTable
select * from secondTable
select * from thirdTable.

I then use SqlDataAdapter.Fill(dataset) to fill my dataset.

This dataset then has all the tables that are returned from my stored procedure but they are named Table, Table1, Table2 etc.

ok how you pass you stored procedure the names of tables?

ok how you pass you stored procedure the names of tables?

At the start of the session I call a method getTables()

This calls the stored procedure which returns all my tables.

In the stored procedure i call "select * from" for each table.

ok now you have two choices either write in the stored procedure queries like this

Select * from table_name as table_name

or you can access your tables by default names i.e.

dataSet1.Tables["Table1"]....

i think you must have known the sequence of tables ?

Select * from table_name as table_name

I've tried this and does not work. I have also tried

Select * from table_name as [table_name]

Doing it just from sequence would be impossible as I have more than 30 tables and want to add a lot more in future.

How they are named by Table1, Table 2?/?????
I am using sql and when I get the tables their names remain same as they are in databases I can't understand can you please explain may be than i can explain any thing?

If you don't mind me asking, what method do you use to get your tables?

Maybe I can try that to see if it works for me?

I never used stored procedure I wrote different queries for each of the table and add them in database and this is something new for me I am checking on this hopefully will find a solution :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.