Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-02
9
Medium Priority
?
751 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 1000 total points
ID: 22627914
your select query:
SELECT COUNT(*) FROM myTable
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 1000 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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

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 …
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

916 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