Results 1 to 5 of 5

Thread: Select Count causes system error

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Select Count causes system error

    This is my code.

    VB Code:
    1. Dim cn2 As New SqlClient.SqlConnection()
    2. cn2.ConnectionString = "Integrated Security=True;" & _
    3.         "Data Source=Brutus;Initial Catalog=Problem Tracking;" & _
    4.         "user id=user;password=pass;"
    5. cn2.Open()
    6. Dim commSQL2 As New SqlClient.SqlCommand()
    7. commSQL2.Connection = cn2
    8. Dim datRead2 As SqlClient.SqlDataReader
    9. commSQL2.CommandText = "Select Count(Store Number) As Exp1 From Stores Order By [Store Number]"
    10. datRead2 = commSQL2.ExecuteReader

    This causes the following error message:
    An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

    Additional information: System error.

    I tried it with a regular Select command like this.
    VB Code:
    1. commSQL2.CommandText = "Select [Store Number] From Stores Order By [Store Number]"
    This worked fine.

    Anyone have any ideas why the select count statement is not working?

    Thanks
    David Wilhelm

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Looks like the Order By clause was causing the error. Apparently, you cannot have an order by clause in a select statement with a Select count in the line.

    Even this doesn't work.
    VB Code:
    1. commSQL2.CommandText = "Select count(*) as Row Count, [Store Number] From Stores Order By [Store Number]"

    This doesn't work either...
    VB Code:
    1. commSQL2.CommandText = "Select count(*) as Row Count, [Store Number] From Stores"
    Anyone have one that does work where you bring back the record count and all the records as well?
    David Wilhelm

  3. #3
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Just a suggestion - you may want to catch SqlExceptions - if you do, you get better messages.

    About your SQL - do you really want a result set that contains the count in every row? Or do you just want the count? You might be better off determining the number of rows after you've fetched them, or issue another statement that just gets that one value.

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Of course order by wouldnt work. There is no reason for it to work at all.

    If you want a listing of records and the count. Just fill a dataset, then use

    datset.Tables("tablename").Rows.Count
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    If the field is going to contain spaces, the name MUST be enclosed in brackets....
    Code:
    commSQL2.CommandText = "Select count(*) as [Row Count], [Store Number] From Stores Order By [Store Number]"
    AND you have to tell it what to group on....
    Code:
    commSQL2.CommandText = "Select count(*) as [Row Count], [Store Number] From Stores GROUP BY [Store Number]"
    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width