364,112 Members | 2191 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

SQL Stored Procedures and VB.NET

Enyi
P: 38
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
Jul 31 '06 #1
Share this Question
Share on Google+
2 Replies


Petrosoft
P: 5
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
Jul 31 '06 #2

Enyi
P: 38
Thank you very much. Had been getting annoyed with this code. The procedure and code makes a bit more sense now. Thanks.
Aug 1 '06 #3

Post your reply

Help answer this question



Didn't find the answer to your Visual Basic .NET question?

You can also browse similar questions: Visual Basic .NET