Solved

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

Posted on 2008-10-02
9
694 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
Comment Utility
your select query:
SELECT COUNT(*) FROM myTable
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 250 total points
Comment Utility
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
Comment Utility
It works but it returns 8 rows when i only have 7 records
0
 
LVL 22

Expert Comment

by:prairiedog
Comment Utility
Do you have duplicate records in DB?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:bmanmike39
Comment Utility
no counted them just 7
0
 
LVL 22

Expert Comment

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

Expert Comment

by:Anurag Thakur
Comment Utility
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
Comment Utility
sorry i was looking at the wrong table.  thanks it works
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Expando 4 33
Trouble with References... 5 21
Code works but how can I download the file? 20 44
Round up to 100% in .NET 10 38
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now