?
Solved

How to move next to next record in a SqlDataReader

Posted on 2007-12-04
5
Medium Priority
?
2,220 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 2000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

850 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