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("c onnectionString"))
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> + "'" 4 1635
TJS,
Maybe I'm missing something, but why are you passing the actual sql
statement into your procedure?
Try this:
CREATE PROCEDURE [dbo.[RecordCount]
@LName varchar(255)
AS
SELECT COUNT(tableA.LName) AS LastNameCount
FROM tableA
WHERE tableA .LName = @LName
RETURN
GO
Dim parameterSQL As New SqlParameter("@SQLstring", SqlDbType.NVarChar, 255)
parameterSQL.Value = "Gengo"
myCommand.Parameters.Add(parameterSQL)
'---Your code might work, but it's not very efficient. However it certainly
won't work as is:
' You've used "ExecuteNonQuery" to execute the command. ExecuteNoneQuery
means that
' nothing will be returned. You want to use "ExecuteScalar" (single item
returned).
Dim temp as Integer
temp = myCommand.ExecuteScalar()
Sincerely,
--
S. Justin Gengo, MCP
Web Developer
Free code library at: www.aboutfortunate.com
"Out of chaos comes order."
Nietzche
"TJS" <no****@here.com> wrote in message
news:uZ***************@TK2MSFTNGP11.phx.gbl... 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("c onnectionString")) 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> + "'"
trying to make it a generic procedure for getting record count
"S. Justin Gengo" <sjgengo@[Remove_To_Email]aboutfortunate.com> wrote in
message news:eh**************@TK2MSFTNGP11.phx.gbl... TJS,
Maybe I'm missing something, but why are you passing the actual sql statement into your procedure?
Try this:
CREATE PROCEDURE [dbo.[RecordCount] @LName varchar(255) AS SELECT COUNT(tableA.LName) AS LastNameCount FROM tableA WHERE tableA .LName = @LName RETURN GO
Dim parameterSQL As New SqlParameter("@SQLstring", SqlDbType.NVarChar,
255) parameterSQL.Value = "Gengo" myCommand.Parameters.Add(parameterSQL)
'---Your code might work, but it's not very efficient. However it
certainly won't work as is: ' You've used "ExecuteNonQuery" to execute the command.
ExecuteNoneQuery means that ' nothing will be returned. You want to use "ExecuteScalar" (single
item returned).
Dim temp as Integer temp = myCommand.ExecuteScalar() Sincerely,
-- S. Justin Gengo, MCP Web Developer
Free code library at: www.aboutfortunate.com
"Out of chaos comes order." Nietzche
"TJS" <no****@here.com> wrote in message news:uZ***************@TK2MSFTNGP11.phx.gbl... 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("c onnectionString")) 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> + "'"
Ahhh, in that case this (and using ExecuteScalar instead of ExecuteNonQuery)
should do the trick: http://www.devarticles.com/art/1/4/2
Sincerely,
--
S. Justin Gengo, MCP
Web Developer
Free code library at: www.aboutfortunate.com
"Out of chaos comes order."
Nietzche
"TJS" <no****@here.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... trying to make it a generic procedure for getting record count
"S. Justin Gengo" <sjgengo@[Remove_To_Email]aboutfortunate.com> wrote in message news:eh**************@TK2MSFTNGP11.phx.gbl... TJS,
Maybe I'm missing something, but why are you passing the actual sql statement into your procedure?
Try this:
CREATE PROCEDURE [dbo.[RecordCount] @LName varchar(255) AS SELECT COUNT(tableA.LName) AS LastNameCount FROM tableA WHERE tableA .LName = @LName RETURN GO
Dim parameterSQL As New SqlParameter("@SQLstring", SqlDbType.NVarChar, 255) parameterSQL.Value = "Gengo" myCommand.Parameters.Add(parameterSQL)
'---Your code might work, but it's not very efficient. However it certainly won't work as is: ' You've used "ExecuteNonQuery" to execute the command. ExecuteNoneQuery means that ' nothing will be returned. You want to use "ExecuteScalar" (single item returned).
Dim temp as Integer temp = myCommand.ExecuteScalar() Sincerely,
-- S. Justin Gengo, MCP Web Developer
Free code library at: www.aboutfortunate.com
"Out of chaos comes order." Nietzche
"TJS" <no****@here.com> wrote in message news:uZ***************@TK2MSFTNGP11.phx.gbl... 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("c onnectionString")) 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> + "'"
I'm not seeing any difference from that link. I'm also passing in a string
?
"S. Justin Gengo" <sj*****@aboutfortunate.com> wrote in message
news:eo**************@TK2MSFTNGP11.phx.gbl... Ahhh, in that case this (and using ExecuteScalar instead of
ExecuteNonQuery) should do the trick:
http://www.devarticles.com/art/1/4/2
Sincerely,
-- S. Justin Gengo, MCP Web Developer
Free code library at: www.aboutfortunate.com
"Out of chaos comes order." Nietzche
"TJS" <no****@here.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl... trying to make it a generic procedure for getting record count
"S. Justin Gengo" <sjgengo@[Remove_To_Email]aboutfortunate.com> wrote in message news:eh**************@TK2MSFTNGP11.phx.gbl... TJS,
Maybe I'm missing something, but why are you passing the actual sql statement into your procedure?
Try this:
CREATE PROCEDURE [dbo.[RecordCount] @LName varchar(255) AS SELECT COUNT(tableA.LName) AS LastNameCount FROM tableA WHERE tableA .LName = @LName RETURN GO
Dim parameterSQL As New SqlParameter("@SQLstring", SqlDbType.NVarChar, 255) parameterSQL.Value = "Gengo" myCommand.Parameters.Add(parameterSQL)
'---Your code might work, but it's not very efficient. However it certainly won't work as is: ' You've used "ExecuteNonQuery" to execute the command. ExecuteNoneQuery means that ' nothing will be returned. You want to use "ExecuteScalar" (single item returned).
Dim temp as Integer temp = myCommand.ExecuteScalar() Sincerely,
-- S. Justin Gengo, MCP Web Developer
Free code library at: www.aboutfortunate.com
"Out of chaos comes order." Nietzche
"TJS" <no****@here.com> wrote in message news:uZ***************@TK2MSFTNGP11.phx.gbl... > 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("c onnectionString")) > 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> + "'" > > > > > > > > > > > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: berthelot samuel |
last post by:
Hi everyone,
I am currently trying to write a report based on a View of SQL Server.
Basically, I have 3 tables : Hardware, SoftwareInstalled and Software
with SoftwareInstalled that keeps track of...
|
by: CJM |
last post by:
I have page that starts a transaction and runs several StoredProcs before
committing or rollingback. An initial SP create a header records, and then
the code goes into a loop and runs 4 other SP's...
|
by: Rhino |
last post by:
I've written several Java stored procedures now (DB2 V7.2) and I'd like to
write down a few "best practices" for reference so that I will have them
handy for future development.
Would the...
|
by: Mihaly |
last post by:
I have a stored procedure in SQL Server 2000, and I want to read the error
messages from this stored procedure.
Please help me for this question:
This is the stored procedure. Please supose than...
|
by: Jack |
last post by:
Hi,
I am trying to run an example code from a book. However I am getting the
following error message:
Number: -2147217900
Description: Syntax error or access violation
Source: Microsoft OLE...
| |
by: karups |
last post by:
Hi,
I've got listbox in my .aspx page where the users can make multiple
selection.
So, Users can select any number of items in listbox, I have to take
value from items and
pass it to stored...
|
by: barmatt80 |
last post by:
I am stumped on the error reporting with sql server. I was told i
need to return @SQLCode(code showing if successful or not) and
@ErrMsg(and the message returned). I am clueless on this.
I...
|
by: preejith |
last post by:
I am getting the following error while running a stored procedure in mysql5.0
Error Code : 1329
No data - zero rows fetched, selected, or processed.
I have an stored procedure SP1 which calls...
|
by: gamaz |
last post by:
Hi
I have a web form which is a part of asp.net application. This process updates a row of a table. This works fine. However I needed to add a DueDate field in the form and update the DueDate...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |