Solved

Can I load one dataset with more than one datatables in this way?

Posted on 2006-07-12
7
202 Views
Last Modified: 2010-04-23
I tried the following in vs 2003 win form app.

Dim arParms() As SqlParameter = New SqlParameter(0) {}
        arParms(0) = New SqlParameter("@P1", SqlDbType.VarChar)
        arParms(0).Value = JobID
            dsOrder = SqlHelper.ExecuteDataset(cn_str_W, CommandType.StoredProcedure, "CT_getOrder", arParms)
            dsOrder.Tables(0).TableName = "order"
            dsOrder = SqlHelper.ExecuteDataset(cn_str_Q, CommandType.StoredProcedure, "CT_getProduct", arParms)
            dsOrder.Tables(1).TableName = "Product"

I got index out of range exception on last line. How do I set the index first? The code is fine if I removed last two lines.

Thanks!
0
Comment
Question by:ommer
  • 3
  • 2
  • 2
7 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17094837
We'd need to see SqlHelper. However, you're assigning the output from ExecuteDataset to the dataset twice, which means that you're overwriting the dataset the second time you call it. So, effectively, you're only making the second call.
0
 

Author Comment

by:ommer
ID: 17095472
The sqlHelper class is from Microsoft Data Access Application Block. I don't have the source code right now.

Anyway, as I understand, one dataset can have many data tables in it. This is new to me, given my ado recordset background. How do you typically load the 2nd table to the dataset? Do you have to make the stored procedures returns multiple sets, or do I need to "pre-dimension" the dataset-datatables?

0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17095638
Generally, you'd load the second set of data into a table, then add that table to the dataset...
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17096227
A fan of SqlHelper myself, I've tinkered around and found that you can do the following (assuming that your 2nd execution will return only 1 table):

dsOrder = SqlHelper.ExecuteDataset(cn_str_W, CommandType.StoredProcedure, "CT_getOrder", arParms)
dsOrder.Tables(0).TableName = "order"
dsOrder.Tables.Add(SqlHelper.ExecuteDataset(cn_str_Q, CommandType.StoredProcedure, "CT_getProduct", arParms).Tables(0))
dsOrder.Tables(1).TableName = "Product"
0
 

Author Comment

by:ommer
ID: 17099359
ZeonFlash,

When I use next line from your reply, do I use ...Tables(0) or ...Tables(1)? It seems neither works for me.

dsOrder.Tables.Add(SqlHelper.ExecuteDataset(cn_str_Q, CommandType.StoredProcedure, "CT_getProduct", arParms).Tables(0))

Thank a lot!
0
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 50 total points
ID: 17100174
Yikes, oversight on my part.  You're probably getting a "DataTable already belongs to another DataSet" error.  You need to create a copy of the table to add to the dataset, not use the one returned by SqlHelper directly.  Try this:

dsOrder.Tables.Add(SqlHelper.ExecuteDataset(cn_str_Q, CommandType.StoredProcedure, "CT_getProduct", arParms).Tables(0).Copy)

0
 

Author Comment

by:ommer
ID: 17100786
Yap, that works. It will save me couple datasets. More importantly, I have learnt the dotnet way. Thanks!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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