• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2024
  • Last Modified:

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

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
0
R8VI
Asked:
R8VI
  • 2
  • 2
  • 2
  • +2
3 Solutions
 
jmwheelerCommented:
int iCountRows = s.Tables[0].Rows.Count;
0
 
CB_ThirumalaiCommented:
It should be
int iCountRows = s.Tables[0].Rows.Count;
0
 
jmwheelerCommented:
You code is returning the number of tables, not the number of rows.  Use above.
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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

            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

0
 
OmerFarukZCommented:
Ohh, no. If dataset contains two or more tables? My code is correct.
0
 
R8VIAuthor 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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now