Solved

How to move next to next record in a SqlDataReader

Posted on 2007-12-04
5
2,210 Views
Last Modified: 2013-12-16
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

0
Comment
Question by:pratikshahse
5 Comments
 
LVL 19

Expert Comment

by:Limbeck
ID: 20404826
probably a very stupid question on my part; but why dont you use a where clause in your sql statement instead?
0
 

Author Comment

by:pratikshahse
ID: 20404934
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

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20405082
I think you need:

                                    if (Convert.ToDateTime(sNow) < Convert.ToDateTime(sEffDate) || Convert.ToDateTime(sNow) > Convert.ToDateTime(sExpDate))
                                    {
                                            continue; //  go back to the "while" statement and read the next record.
                                    }
                                    else
                                    {
                                           //  continue with populating the grid.
                                    }
0
 
LVL 3

Expert Comment

by:Aurora27
ID: 20405101
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.

0
 
LVL 3

Expert Comment

by:Aurora27
ID: 20415656
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.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question