Solved

How to move next to next record in a SqlDataReader

Posted on 2007-12-04
5
2,214 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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