T
TJS
trying to get a record count from a stored procedure using a supplied SQL
statement
Error msg:
===========
"The SqlParameterCollection only accepts non-null SqlParameter type
objects,not String objects."
SPROC:
========
CREATE PROCEDURE [dbo].[RecordCount]
@SQLstring varchar(2000)
AS
DECLARE @SQL varchar(2000)
Set @SQL = "Return("+ @SQLstring +")"
EXEC (@SQL)
GO
Calling vb function:
=================
Public Function RecordCount(ByVal vSQL As String) As Integer
' Create Instance of Connection and Command Object
Dim myConnection As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim myCommand As New SqlCommand("NCL_RecordCount", myConnection)
' Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure
' Add Parameters to SPROC
Dim parameterSQL As New SqlParameter("@SQLstring", SqlDbType.NVarChar,
2000)
parameterSQL.Value = vSQL
myCommand.Parameters.Add(parameterSQL)
'record count from table as OUTPUT
Dim parameterRecordCount As New SqlParameter("ReturnValue",SqlDbType.Int)
myCommand.Parameters.Add("parameterRecordCount")
parameterRecordCount.Direction = ParameterDirection.ReturnValue
' Open the database connection and execute the command
myConnection.Open()
Dim temp as Integer
myCommand.ExecuteNonQuery()
temp = myCommand.Parameters("ReturnValue").Value
myConnection.Close()
return Temp
End Function
vSQL:
========
vSQL = "Select count(*) from tableA where tableA .LName = '"+
<txtValueSupplied> + "'"
statement
Error msg:
===========
"The SqlParameterCollection only accepts non-null SqlParameter type
objects,not String objects."
SPROC:
========
CREATE PROCEDURE [dbo].[RecordCount]
@SQLstring varchar(2000)
AS
DECLARE @SQL varchar(2000)
Set @SQL = "Return("+ @SQLstring +")"
EXEC (@SQL)
GO
Calling vb function:
=================
Public Function RecordCount(ByVal vSQL As String) As Integer
' Create Instance of Connection and Command Object
Dim myConnection As New
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Dim myCommand As New SqlCommand("NCL_RecordCount", myConnection)
' Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure
' Add Parameters to SPROC
Dim parameterSQL As New SqlParameter("@SQLstring", SqlDbType.NVarChar,
2000)
parameterSQL.Value = vSQL
myCommand.Parameters.Add(parameterSQL)
'record count from table as OUTPUT
Dim parameterRecordCount As New SqlParameter("ReturnValue",SqlDbType.Int)
myCommand.Parameters.Add("parameterRecordCount")
parameterRecordCount.Direction = ParameterDirection.ReturnValue
' Open the database connection and execute the command
myConnection.Open()
Dim temp as Integer
myCommand.ExecuteNonQuery()
temp = myCommand.Parameters("ReturnValue").Value
myConnection.Close()
return Temp
End Function
vSQL:
========
vSQL = "Select count(*) from tableA where tableA .LName = '"+
<txtValueSupplied> + "'"