Solved

VB.net Error trying to count the records in a query before running it

Posted on 2014-10-30
2
205 Views
Last Modified: 2014-10-30
Hi
I use the VB.net code function below to count the number of records on a query before I run it. It has worked fin up until the following SQL string that I passed into it. I get the error "No column name was specified for column 1 of 'X' "

      oSQL      "SELECT Sum([Order Details].[Quantity]), [Order Details].[Product ID] FROM [Order Details] GROUP BY [Order Details].[Product ID] HAVING Sum([Quantity]) < 100000"      String


    Function oCount_Rows_in_Query(ByVal oSQL As String) As String

        Try
            Dim DBType As String = Globals.ThisAddIn.oRIGHT.lblDataBaseType.Text
            Dim oConnectionString As String = Globals.ThisAddIn.oRIGHT.lblConnectionString.Text


            'If there are sort criteria then chop this off the end as doesn't work
            'in a count, nor is it relevant
            'If InStr(oSQL, "ORDER BY") > 0 Then
            'Dim arrSplit As Object = Split(oSQL, "ORDER BY")
            'oSQL = arrSplit(0) 'Use SQL before the ORDER BY
            'End If


            Dim sSQL As String


                sSQL = "SELECT Count(*) As MyCount FROM (" & oSQL & ") As X" 'Have to alias the sub query

                Dim cn As New SqlConnection(oConnectionString)
                Dim cmd As New SqlCommand(sSQL, cn)
                cn.Open()

                oCount_Rows_in_Query = cmd.ExecuteScalar.ToString

                If cn.State <> ConnectionState.Closed Then
                    cn.Close()
                End If

        Catch ex As Exception
            oCount_Rows_in_Query = "Error"
            MsgBox(Err.Description & " 17azn")
        End Try
    End Function
0
Comment
Question by:murbro
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40413243
>"No column name was specified for column 1 of 'X' "
The knee-jerk SQL answer to the above error message would be to include an alias (QuantitySum below) in the first column
oSQL      "SELECT Sum([Order Details].[Quantity]) as QuantitySum, [Order Details].[Product ID] FROM [Order Details] GROUP BY [Order Details].[Product ID] HAVING Sum([Quantity]) < 100000"      String

Open in new window

Can't help you on the VB.NET part..
0
 

Author Closing Comment

by:murbro
ID: 40413264
Great answer! Thanks Jim
Murray
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

776 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