Search code examples
vb.netvisual-studio-2015sql-server-2014-expresssmalldatetime

Very Strange SqlClient error with SmallDatetime parameter in SQL Server 2014 stored proc


I've been stumped with the following error for the past hour:

"Procedure or function 'write_call' expects parameter '@Date', which was not supplied."

The @date parameter is a smalldatetime. I am trying to pass the value #1/27/2009 04:32:00 PM#

I am using SQLExpress 2014.

The same stored proc has been in production on SQLExpress 2008

T-SQL Stored Proc:

 ALTER proc [dbo].[write_call]
(
    @Date smalldatetime, @Duration smallint, @Ring tinyint, @Extension smallint = null, @Number varchar(20) = null, @LineID smallint, 
    @AccountID smallint = null, @AccountName varchar(30) = null, @UnitID smallint = null, @UnitName varchar(30) = null, @AreaName varchar(100) = null, @CallType char(1) = null,
    @CallCode varchar(6) = null, @Rate numeric(5,2) = 0.0, @Cost numeric(12,2) = 0.0, @TAC varchar(10) = null
)

as

declare @id int=0, @old_acctid int=0  --, @old_rate numeric(5,2)=0.0, @old_cost numeric(12,2)=0.0

select @id=o.ID,@old_acctid=o.AccountID from OutboundCalls o where o.Date=@Date and o.Duration=@Duration and o.Number=@Number

if @id=0
    begin
        insert into _OutboundCalls (Date, Duration, Ring, Extension, Number, LineID, AccountID, AccountName, UnitID, UnitName, AreaName, CallType,CallCode, Rate, Cost, TAC,redirected,contactid,CompanyID,CompanyName)
        values (@Date, @Duration, @Ring, @Extension, @Number, @LineID, @AccountID, @AccountName, @UnitID, @UnitName, @AreaName, @CallType, @CallCode, @Rate, @Cost, @TAC,1,0,1,'PricewaterhouseCoopers')
    end
else if @id>0 and @old_acctid<>@AccountID
    update OutboundCalls set AccountID=@AccountID, UnitID=@UnitID,AccountName=@AccountName,UnitName=@UnitName
    where OutboundCalls.ID=@id

VB.NET 2015:

Private Sub write_call(c As CallInfo)
    Using cnn = New SqlClient.SqlConnection(connString)
        Using cmd = New SqlClient.SqlCommand("write_call", cnn)

            cmd.Parameters.AddWithValue("@Date", c.dt)
            cmd.Parameters.AddWithValue("@Duration", c.secDurn)
            cmd.Parameters.AddWithValue("@Ring", 0)
            cmd.Parameters.AddWithValue("@Extension", c.src)
            cmd.Parameters.AddWithValue("@Number", c.dst)
            cmd.Parameters.AddWithValue("@LineID", c.lineId)
            cmd.Parameters.AddWithValue("@AccountID", c.account_id)
            cmd.Parameters.AddWithValue("@AccountName", c.account_name)
            cmd.Parameters.AddWithValue("@UnitID", c.unit_id)
            cmd.Parameters.AddWithValue("@UnitName", c.unit_name)
            cmd.Parameters.AddWithValue("@AreaName", c.area?.Name)
            cmd.Parameters.AddWithValue("@CallType", c.dst_type)
            cmd.Parameters.AddWithValue("@CallCode", c.callcode)
            cmd.Parameters.AddWithValue("@Rate", c.rate?.rate)
            cmd.Parameters.AddWithValue("@Cost", c.Cost)
            cmd.Parameters.AddWithValue("@TAC", c._oC)

            cnn.Open()

            Try
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                Stop
            End Try
        End Using
    End Using
End Sub

Solution

  • It seems that your code misses to set the CommandType property. This property defaults to CommandType.Text and it is appropriate when you pass a sql string as the CommandText.

    If you pass the name of a stored procedure you need to set

    cmd.CommandType = CommandType.StoredProcedure
    

    without this setting the behavior of the SqlCommand is unpredictable.