Link to home
Start Free TrialLog in
Avatar of pratikshahse
pratikshahse

asked on

How to move next to next record in a SqlDataReader

I am reading records from a sql database and populating datagrid.  I am checking on a condition and if that is true then I want to go to next record and if its false then I want to continue populating the grid.  I have attached the code snippet. Please let me know what I need to do .

Thanks

while(rsMktPrg.Read())
						{
							//  if checked, only display active marketing programs
							if (!optAllMktProg.Checked)
							{
 
                               
                                    sEffDate = rsMktPrg["eff_dte"].ToString();
                                    sEffDate = System.Convert.ToDateTime(sEffDate).ToString("MM/dd/yy");
                                    sExpDate = rsMktPrg["exp_dte"].ToString();
                                    sExpDate = System.Convert.ToDateTime(sExpDate).ToString("MM/dd/yy");
 
                                    if (Convert.ToDateTime(sNow) < Convert.ToDateTime(sEffDate) || Convert.ToDateTime(sNow) > Convert.ToDateTime(sExpDate))
                                    {
                                      // If the if statement is true then I want to go back to the "while" statement and read the next record.
                                    }
                                    else
                                    {
                                       // if the if statement is not true then I want to continue with populating the grid. 
                                    }
                                }

Open in new window

Avatar of Limbeck
Limbeck

probably a very stupid question on my part; but why dont you use a where clause in your sql statement instead?
Avatar of pratikshahse

ASKER

I cannot. Couple things I have to get each and every record out of sql and second I cannot modify the sql store procs. I have to work with the store procs that are already written

ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Pratik...:D If you want to just loop through records, use

while (r.read())
{
   string sf =   r["FIELD1"].Tostring();

}

r.read() actually advances to the next record, so it acts like calling Next() in ADO in traditional VB.

Wheres the problem?

CT.

Author, You cant be rude here. First of all, you are demanding here. Your question title and the answer expected does NOT seem to be related. And ask in a humble way the next time, better.


CT.