Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am Using Winform
i want to know how to find that "How many Rows are Return by Sqldatareader" when Query Succeeded.
Here my Code :
C#
if (sqldreader.HasRows>1)
                 {
                txtid.ReadOnly=true;
                txtname.ReadOnly = true;
                mnupdate.Enabled = false;
                txtaddress.ReadOnly = true;
                txtcnic.ReadOnly = true;
                cmbcity.Enabled = false;
                txtmobile.ReadOnly = true;

                  }
                 else{
                     while (sqldreader.Read())
                           {
                    lbstatus.ForeColor = Color.Red;
                    lbstatus.Text = ""
                            }
                     }
Posted
Updated 21-Feb-14 8:19am
v2

In your while loop you'll need to keep track. Just have an int counter that you increment in while (sqldreader.Read())

Since DataReaders only read one record at a time you can't get the full record count until you have read through it all.
 
Share this answer
 
Comments
abdul manan 7 21-Feb-14 14:26pm    
should i use dataset to get row count before read() all the records
ZurdoDev 21-Feb-14 14:29pm    
I wouldn't necessarily change how you are doing things just for a record count. Keeping your own counter is very simple. It's 2 lines of code basically. Declare the variable and then increment it.
First of all, HasRows is a boolean field which lets you know yes/no that there are records.

A SQLdatareader does not have a property or method to get the count of the number of records. That is because it is, as the MSDN documentation says, "forward-only stream of rows from a SQL Server database". This means, the datareader has no idea how many records there are because the records are not read or "there" yet. You have to read them to find out how many there are. This may seem like a limitation, but the fact that the dataread does not fetch all the records at once is what makes it fast and efficient.

If you do want to use the datareader and do need a count, you need to read the records and count them as you go. See this link for an example:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/dc198199-be24-473c-9973-746fe14d65ba/row-count-using-a-sqldatareader?forum=csharpgeneral[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900