stored procedure output to generic list, noob

  • Thread starter Thread starter jason
  • Start date Start date
J

jason

I've got this really simple table with two fields

book:
name varchar(50)
phone varchar(50)

and simple stored procedure jcp1:

ALTER procedure "jcp1"
@name varchar(50) AS
SELECT phone FROM book where name = @name

from ASP.NET I have no problem using the stored procedure as a
datasource to bind rows found into a datalist control.

As a test, I wanted to try and pull the result of the stored procedure
to a list from C# .. this code isn't exactly doing that, but i I
expected it to work:

TExecuteReaderCmd<string>(sqlCmd, TGeneratephone<string>, ref
userList);
foreach (string phonex in userList)
{
if (!userList.Contains(phonex))
{
Label1.Text = phonex;
userList.Add(phonex);
}


However this code failes to retreive anything or ever execute
Label1.Text = phonex; A watch of userList shows a count of 1 in
userList when it gets to this part.

If I remove the if (!userList.Contains(phonex)) condition as follows ,
I get a "collection was modified error" at the If line - it happens on
a subsiquent second pass throught the for loop. When I do this I can
see that Label1.Text gets set to a valid phonex before crashing.

//if (!userList.Contains(phonex))
//{
Label1.Text = phonex;
userList.Add(phonex);
//}

I know the above for loop does not make sense, but I was expecting it
to work and set Label1 to the last phone returing from the stored
procedure.
 
I've got this really simple table with two fields

book:
name varchar(50)
phone varchar(50)

and simple stored procedure jcp1:

ALTER procedure "jcp1"
@name varchar(50) AS
SELECT phone FROM book where name = @name

from ASP.NET I have no problem using the stored procedure as a
datasource to bind rows found into a datalist control.

As a test, I wanted to try and pull the result of the stored procedure
to a list from C# .. this code isn't exactly doing that, but i I
expected it to work:

TExecuteReaderCmd<string>(sqlCmd, TGeneratephone<string>, ref
userList);
foreach (string phonex in userList)
{
if (!userList.Contains(phonex))
{
Label1.Text = phonex;
userList.Add(phonex);
}


However this code failes to retreive anything or ever execute
Label1.Text = phonex; A watch of userList shows a count of 1 in
userList when it gets to this part.

If I remove the if (!userList.Contains(phonex)) condition as follows ,
I get a "collection was modified error" at the If line - it happens on
a subsiquent second pass throught the for loop. When I do this I can
see that Label1.Text gets set to a valid phonex before crashing.

//if (!userList.Contains(phonex))
//{
Label1.Text = phonex;
userList.Add(phonex);
//}

I know the above for loop does not make sense, but I was expecting it
to work and set Label1 to the last phone returing from the stored
procedure.

Firstly, you cannot modify the collection in a foreach loop. And since
phonex is always in the list by virtue of the foreach, it makes no
sense to check to see if it is there. Since you say that the userList
has an 1 item in it, that seems to indicate that the SQL is getting
executed since you have a result.

You might modify your loop like this:

foreach (string phonex in userList)
{
Label1.Text += phonex + "\r\n";
}

That should at least show the results inside the userList.

Chris
 

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

Back
Top