Solved

How to move next to next record in a SqlDataReader

Posted on 2007-12-04
5
2,207 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now