Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a datareader and put that value in an array like below
C#
Task[] objOfTask;
             myReader = cmd.ExecuteReader();
             int counter=0;
             int index = 0;

            while (myReader.Read())
            {
                counter++;
            }
            objOfTask = new Task[counter];
            while (index < counter)
            {
                    objOfTask[index] = new Task();

                    objOfTask[index].Goal = myReader["goals"].ToString();
                    objOfTask[index].Note = myReader["notes"].ToString();
                    objOfTask[index].Status = Convert.ToBoolean(myReader["status"]);

                    index++;
            }

            return objOfTask;


But after Read(), its repeating the last result. So is there any way to get no of rows before Read() ?
Posted
Comments
I.explore.code 19-Oct-12 5:57am    
Can you not use List<Task> rather than an Task[]? That way you can populate the List with just one loop.

Each time you call Read, it fetches a new row, or if there are no more rows returns false.
So your while loop:
C#
while (myReader.Read())
{
    counter++;
}
Has successfully fetched and discarded each and every row.
Instead, use a List, and only loop the once:
C#
List<Task> list = new List<Task>();
while (myReader.Read())
{
    Task t = new Task();

    t.Goal = myReader["goals"].ToString();
    t.Note = myReader["notes"].ToString();
    t.Status = Convert.ToBoolean(myReader["status"]);
    list.Add(t);
}
return list.ToArray();
 
Share this answer
 
Comments
I.explore.code 19-Oct-12 6:07am    
Looks like I can't type fast enough ;) I will remove my solution then. my 5! I would use this.
OriginalGriff 19-Oct-12 6:20am    
No need to delete solutions if they are posted at the same time - it happens all the time. Your solution may have a subtle difference or improvement over mine which could help the OP.
I.explore.code 19-Oct-12 6:24am    
fair enough then, I have put it back! although there is not a drastic difference from yours.
I would be tempted to rewrite the code like so, since it reduces two loops to just one and would still work:

C#
List<task>tasks = new List<task>();
            myReader = cmd.ExecuteReader();
            while (myReader.Read())
            {
                Task t = new Task();
                t.Goal = myReader["goals"].ToString();
                t.Note = myReader["notes"].ToString();
                t.Status = Convert.ToBoolean(myReader["status"]);
                tasks.Add(t);
            }

            return tasks;


Because a DataReader is a forward only cursor that can only move one record at a time which is what happens when you call Read() and which is why you need to call it in the first place to see if there is data available. HasRows is another property of DataReader that can only tell whether or not there are rows available not how many of them.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900