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
Solved

How do I use the ExecuteScalar to count number of  rows in my dB

Posted on 2008-10-02
9
723 Views
Last Modified: 2013-12-17
Im trying to excite a scalar comment, to do a count of records but cant get it to work
I  dont know how to write the command.

Thanks for any help!

Code I have:

SqlConnection sqlConnection1 = new SqlConnection("Data Source=s...string works");
        SqlCommand cmd = new SqlCommand();
       
        //OpenFile connection
        sqlConnection1.Open();

        cmd.CommandType = CommandType.Text;
        cmd.Connection = sqlConnection1;
        myv = (int)cmd.ExecuteScalar();
        // close connection
        sqlConnection1.Close();
0
Comment
Question by:bmanmike39
  • 3
  • 3
  • 2
9 Comments
 
LVL 22

Accepted Solution

by:
prairiedog earned 250 total points
ID: 22627914
your select query:
SELECT COUNT(*) FROM myTable
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 250 total points
ID: 22627989
try this

        cmd.CommandType = CommandType.Text;
        cmd.Connection = sqlConnection1;
        cmd.CommandText = "SELECT COUNT(*) FROM myTable";
        myv = (int)cmd.ExecuteScalar();
        // close connection
        sqlConnection1.Close();
0
 

Author Comment

by:bmanmike39
ID: 22628165
It works but it returns 8 rows when i only have 7 records
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 22

Expert Comment

by:prairiedog
ID: 22628191
Do you have duplicate records in DB?
0
 

Author Comment

by:bmanmike39
ID: 22628231
no counted them just 7
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22628251
Try this:
SELECT DISTINCT COUNT(*) FROM myTable
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22628320
run the commandtext query from your commamd object in the database and then check the results
actually they should match
0
 

Author Comment

by:bmanmike39
ID: 22628324
sorry i was looking at the wrong table.  thanks it works
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 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