Getting multiple SELECTED values from stored procedure into an array?

A

Amy

Hello,

I've been struggling to learn C#.NET for a while now. I've made some
progress, but I'm easily stumped. :(

What's stumping me today is this:
I've got a stored procedure (SQL) that returns one row from a table,
and up to 3 rows from another table. I want to read the values of the
three rows into an array, but I can't figure out how to do it. The
code I have compiles, but I suspect it's not quite what I'm looking
for. Basically, how do I iterate through the contactUIDs that are
being returned from the sp?

Thanks in advance,

amy

code:

if (drProject.Read())
{
p = new Project();
p.ProjName = drProject["projName"].ToString();
p.ProjDescrip = drProject["description"].ToString();
p.ProjRequestedDeadline = drProject["requestedDeadline"].ToString();
p.ProjBasecampURL = drProject["basecampURL"].ToString();
p.ProjStatus = drProject["Status"].ToString();
p.ProjStatusID = Int32.Parse(drProject["statusID"].ToString());
p.ProjFreq = drProject["freqtype"].ToString();
p.ProjFreqID = Int32.Parse(drProject["freqID"].ToString());
p.ProjPriority = drProject["priority"].ToString();
p.ProjPriorityID = Int32.Parse(drProject["priorityID"].ToString());
p.ProjDateProposed = drProject["dateProposed"].ToString();

for (int i=0; i<=3; i++)
{
p.ProjContactUIDs =
Int32.Parse(drProject["contactUID"].ToString());
}


}
 
S

Siva M

Assuming your stored procedure returns multiple resultsets, try this code:

Add drProject.NextResult() before the for loop. This will move the reader to
the next result.

And add drProject.Read() inside the for loop (before assigning to the
array).

Hello,

I've been struggling to learn C#.NET for a while now. I've made some
progress, but I'm easily stumped. :(

What's stumping me today is this:
I've got a stored procedure (SQL) that returns one row from a table,
and up to 3 rows from another table. I want to read the values of the
three rows into an array, but I can't figure out how to do it. The
code I have compiles, but I suspect it's not quite what I'm looking
for. Basically, how do I iterate through the contactUIDs that are
being returned from the sp?

Thanks in advance,

amy

code:

if (drProject.Read())
{
p = new Project();
p.ProjName = drProject["projName"].ToString();
p.ProjDescrip = drProject["description"].ToString();
p.ProjRequestedDeadline = drProject["requestedDeadline"].ToString();
p.ProjBasecampURL = drProject["basecampURL"].ToString();
p.ProjStatus = drProject["Status"].ToString();
p.ProjStatusID = Int32.Parse(drProject["statusID"].ToString());
p.ProjFreq = drProject["freqtype"].ToString();
p.ProjFreqID = Int32.Parse(drProject["freqID"].ToString());
p.ProjPriority = drProject["priority"].ToString();
p.ProjPriorityID = Int32.Parse(drProject["priorityID"].ToString());
p.ProjDateProposed = drProject["dateProposed"].ToString();

for (int i=0; i<=3; i++)
{
p.ProjContactUIDs =
Int32.Parse(drProject["contactUID"].ToString());
}


}
 
A

Amy

I don't *think* it returns multiple resultsets (forgive me, my
background is adabas/NATURAL, I'm still learning the 'modern' lingo) -
it returns one project record (er, row), with multiple ContactUIDs. So
is what you're saying that I should add the drProject.Read() inside
the for loop?

Thanks again,

Amy
 
S

Siva M

Are the project details (except contact ID) same for all records? If so, add
drProject.Read() inside the for loop.

However, note that if the number of contact IDs (that is, number of records
returned) are less than three then Read() will throw exception after reading
available records (because the loop count is hardcoded). This can be fixed
by adding "if (!drProject.Read()) break;" instead of just drProject.Read();

Hope I have not added any confusion..


I don't *think* it returns multiple resultsets (forgive me, my
background is adabas/NATURAL, I'm still learning the 'modern' lingo) -
it returns one project record (er, row), with multiple ContactUIDs. So
is what you're saying that I should add the drProject.Read() inside
the for loop?

Thanks again,

Amy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top