Friday, March 23, 2012

problem reading datavalues to an arraylist

I have an SQL command below that works fine

"SELECT sum(Preference) FROM [projectDB].[dbo].[Vote] group by preference;

SqlDataReader

datareader = cmdPres.ExecuteReader();
ArrayList myArr =newArrayList();

when i read the data in the values i get are
10,14,10,17

I want to loop through the reader, then add the values to an arrayList. Then i want to check of the value in the arrayList is greater than 15 but it dosent work. I get an error message when i add
sum(Prefernece) to the arraylist, it will not allow me.

while (dreader.Read())
{
myArr.Add(datareader["sum(Preference)"].ToString());

if(myArr.? > 15)
{
Response.Write("Its greater than 15");
}
}

sorry,

think i posted this into the wrong section...its not really an sql problem.

but if anyone has any ideas they will be appriciated

|||

ArrayList uses index to get the specific position's value. Because you use reader() to get the data, you have to add extra steps to do. Here is the clue

int position = 0;

while (dreader.Read())
{

myArr.Add(datareader["sum(Preference)"].ToString());

if((int)myArr[position] > 15)

{
Response.Write("Its greater than 15");
}

position++;

}

Hope it helps

sql

No comments:

Post a Comment