Connecting Tech Pros Worldwide Forums | Help | Site Map

SQL Stored Procedures and VB.NET

Member
 
Join Date: Jul 2006
Location: Perth, Western Australia
Posts: 38
#1: Jul 31 '06
Hey there!

I need some help getting some code to work. This particular bit is nearly done, just that it's not working.

Situation:
Trying to get the row count of an table in an SQL database using VB.NET 2003 and stored procedures in SQL Server 2000 Developer Edition. Dam, it's also just started to rain, oh dear. Anyways...

Somewhere in my project is the code:
Expand|Select|Wrap|Line Numbers
  1.         Try
  2.             Dim cmdVideoCount As New SqlCommand
  3.             cmdVideoCount.Connection = sqlConn
  4.             cmdVideoCount.CommandText = "GetVideoCount"
  5.             cmdVideoCount.CommandType = CommandType.StoredProcedure
  6.  
  7.             Dim paramResult As New SqlParameter
  8.             paramResult.ParameterName = "@VideoCount"
  9.             paramResult.DbType = DbType.Int16
  10.             paramResult.Direction = ParameterDirection.Output
  11.             cmdVideoCount.Parameters.Add(paramResult)
  12.  
  13.             sqlConn.Open()
  14.             Dim intVideos As Integer = cmdVideoCount.ExecuteScalar()
  15.             MsgBox(intVideos)
  16.             sqlConn.Close()
  17.         Catch ex As Exception
  18.             Throw ex
  19.         Finally
  20.             sqlConn.Close()
  21.         End Try
  22.  
The code for the stored procedure is:
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE GetVideoCount ( @VideoCount int OUTPUT )
  2. AS
  3. SELECT @VideoCount = Count(Video_ID) FROM Videos
  4. GO
  5.  
At the moment, it has now stopped raining, yay! and the stored procedure returns 0.

There is one test record in the database at the moment.

Any help would be appreciated, thanks.

Enyi

Newbie
 
Join Date: Jul 2006
Posts: 5
#2: Jul 31 '06

re: SQL Stored Procedures and VB.NET


change the procedure
from

CREATE PROCEDURE GetVideoCount ( @VideoCount int OUTPUT )
AS
SELECT @VideoCount = Count(Video_ID) FROM Videos
GO

to

CREATE PROCEDURE GetVideoCount ( )
AS
SELECT Count(Video_ID) FROM Videos
GO

and remove the following lines from your code
Dim paramResult As New SqlParameter
paramResult.ParameterName = "@VideoCount"
paramResult.DbType = DbType.Int16
paramResult.Direction = ParameterDirection.Output
cmdVideoCount.Parameters.Add(paramResult)

ExecuteScalar returns the first field of the first record as an object.
in your case the Count(Video_ID) is returned from the call to ExecuteScalar
Member
 
Join Date: Jul 2006
Location: Perth, Western Australia
Posts: 38
#3: Aug 1 '06

re: SQL Stored Procedures and VB.NET


Thank you very much. Had been getting annoyed with this code. The procedure and code makes a bit more sense now. Thanks.
Reply