Solved

How to move next to next record in a SqlDataReader

Posted on 2007-12-04
5
2,213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 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