Solved

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

Posted on 2006-07-12
7
198 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

22 Experts available now in Live!

Get 1:1 Help Now