title: Returning Max with a stored procedure
body: <p>I have read and retried rebuilding the below in many ways but to keep it clear I will show my last attempt.</p>
  
  <p>Aim - To get the Max value of column "UniqueID" </p>
  
  <p>The column field 'uniqueID' is set as a bigint</p>
  
  <p>I assume the error is in the line with addwithvalue in as I get "int" as the return value</p>
  
  <p>If I run the query <code>SELECT MAX(UniqueID) FROM tblResults in SQL</code> it works</p>
  
  <p>Code</p>
  
  <pre><code> Dim connection5 As SqlConnection
      Dim command5 As New SqlCommand
      Dim ds5 As New DataSet
      Dim ConnectionString5 As String = System.Configuration.ConfigurationManager.ConnectionStrings("mySQLConnectionString").ToString()
      connection5 = New SqlConnection(ConnectionString5)
  
      connection5.Open()
      command5.Connection = connection5
      command5.Parameters.Clear()
  
      command5.CommandText = "spUniqueUserID"
      command5.Parameters.AddWithValue("@UniqueID", SqlDbType.BigInt)
      command5.Parameters("@UniqueID").Direction = ParameterDirection.Output
      command5.CommandType = CommandType.StoredProcedure
      command5.ExecuteNonQuery()
  
      Session.Item("UniqueID") = command5.Parameters("@UniqueID").Value
      connection5.Close()
  
      Dim vShow As String
      vShow = ""
      vShow = Session.Item("UniqueID").ToString
  </code></pre>
  
  <p>SP</p>
  
  <pre><code>USE [DB]
  GO
  /****** Object:  StoredProcedure [dbo].[spUniqueUserID]    Script Date: 09/10/2013 08:51:57 ******/
  SET ANSI_NULLS ON
  GO
  SET QUOTED_IDENTIFIER ON
  GO
  
  ALTER PROCEDURE [dbo].[spUniqueUserID]
  
  @UniqueID bigint OUTPUT
  
  AS
  
  BEGIN
  
  select @UniqueID = (SELECT MAX(UniqueID) FROM tblResults )
  
  Return @UniqueID
  
  END
  </code></pre>