We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

dataset count number of rows, c#, asp.net

R8VI
R8VI asked
on
Medium Priority
2,040 Views
Last Modified: 2013-12-17
I have the following code


SqlConnection conndataset = new SqlConnection(url);
             SqlDataAdapter a = new SqlDataAdapter
            ("select distinct Name from Categories", conndataset);
             DataSet s = new DataSet();
             a.Fill(s);

             foreach (DataRow dr in s.Tables[0].Rows)
             {
                 Console.WriteLine(dr[0].ToString());
                 
             }

            int iCountRows = s.Tables.Count;

I want the number of rows returned by the query i know it should be 6 but it keeps returning 1 in iCountRows

please help

Thanks,

R8VI
Comment
Watch Question

int iCountRows = s.Tables[0].Rows.Count;

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
It should be
int iCountRows = s.Tables[0].Rows.Count;
You code is returning the number of tables, not the number of rows.  Use above.

Commented:
You are returning the number of tables.
You want the number of rows.
s.Tables.Rows.Count();
Commented:
oops, forgot the index try
s.Tables[0].Rows.Count;();

            SqlConnection conndataset = new SqlConnection(url);
             SqlDataAdapter a = new SqlDataAdapter
            ("select distinct Name from Categories", conndataset);
             DataSet s = new DataSet();
             a.Fill(s);
 
             int RowCount = 0;
 
             foreach (DataTable dt in s.Tables)
             {
                 RowCount + = dt.Rows.Count
             }
 
             Response.Write("Dataset is contains rows count : " + RowCount );

Open in new window

Ohh, no. If dataset contains two or more tables? My code is correct.

Author

Commented:
Hi Omer,

I suppose you are right but
1) it will never have more than one table and
2) i tried the solution the other guys gave me and it worked and then you posted you code.

Sorry

R8VI
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.