J
Julio Allegue
I am getting the wrong Count(*) on vb.net using the ExecuteScalar . It
returns all the rows. It doesn't seem to look at the WHERE clause. At
the same time, I am getting the correct count on "SQL Enterprise
Manager" or "SQL Query Analyser" using the same SQL statement.
Thanks in advance.
Julio.
Here is the SQL statement plus the source to the GetNumOfRow sub.
------------------------
SQL statement
------------------------
sSQL = "SELECT COUNT(*) FROM Rail_Detail d INNER JOIN
Rail_TaxDeduction t " & _
"ON d.DeductionCode=t.Code " & _
"WHERE d.MFReportRunDate Is NULL"
------------------------
Sub call GetNumOfRows
------------------------
iCounter = GetNumOfRows("METROPASS", "Rail_Detail", sSQL)
-----------------------------------
Source code GetNumOfrows
-----------------------------------
Private Function GetNumOfRows(ByRef sDBName As String, ByVal sTBName
As String, Optional ByVal sCondition As String = "") As Integer
'**************************************************************************
'GetNumOfRows - Returns the number of rows
'**************************************************************************
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand
Dim sConnectionString As String
Dim sSQL As String
sConnectionString = "Integrated Security=SSPI;" & _
"Data Source=" & sgSQLServerName & ";" &
_
"Initial Catalog=" & sDBName
sSQL = "SELECT COUNT(*) FROM " & sTBName & " " & sCondition
objConnection = New SqlConnection(sConnectionString)
Try
objConnection.Open()
objCommand = New SqlCommand(sSQL, objConnection)
GetNumOfRows = objCommand.ExecuteScalar
Catch e3 As Exception
Call Display_Error(e3.Message, "GetNumOfRows")
Finally
If (objConnection.State = ConnectionState.Open) Then
objConnection.Close()
End If
End Try
objCommand.Dispose()
objCommand = Nothing
objConnection.Dispose()
objConnection = Nothing
End Function
returns all the rows. It doesn't seem to look at the WHERE clause. At
the same time, I am getting the correct count on "SQL Enterprise
Manager" or "SQL Query Analyser" using the same SQL statement.
Thanks in advance.
Julio.
Here is the SQL statement plus the source to the GetNumOfRow sub.
------------------------
SQL statement
------------------------
sSQL = "SELECT COUNT(*) FROM Rail_Detail d INNER JOIN
Rail_TaxDeduction t " & _
"ON d.DeductionCode=t.Code " & _
"WHERE d.MFReportRunDate Is NULL"
------------------------
Sub call GetNumOfRows
------------------------
iCounter = GetNumOfRows("METROPASS", "Rail_Detail", sSQL)
-----------------------------------
Source code GetNumOfrows
-----------------------------------
Private Function GetNumOfRows(ByRef sDBName As String, ByVal sTBName
As String, Optional ByVal sCondition As String = "") As Integer
'**************************************************************************
'GetNumOfRows - Returns the number of rows
'**************************************************************************
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand
Dim sConnectionString As String
Dim sSQL As String
sConnectionString = "Integrated Security=SSPI;" & _
"Data Source=" & sgSQLServerName & ";" &
_
"Initial Catalog=" & sDBName
sSQL = "SELECT COUNT(*) FROM " & sTBName & " " & sCondition
objConnection = New SqlConnection(sConnectionString)
Try
objConnection.Open()
objCommand = New SqlCommand(sSQL, objConnection)
GetNumOfRows = objCommand.ExecuteScalar
Catch e3 As Exception
Call Display_Error(e3.Message, "GetNumOfRows")
Finally
If (objConnection.State = ConnectionState.Open) Then
objConnection.Close()
End If
End Try
objCommand.Dispose()
objCommand = Nothing
objConnection.Dispose()
objConnection = Nothing
End Function