0x6C38
0x6C38

Reputation: 7076

SQL exception despite providing parameters

I am getting the following exception when trying to store data into an sql database:

Procedure or function 'myFunction' expects parameter '@username', which was not supplied.

This is the stored procedure i'm calling:

@username varchar(20),
@password varchar(20),
@puntos int,
@partidaGanada bit
AS
BEGIN
DECLARE @id int
SET @id = (SELECT COUNT(*) AS id FROM Juegos)

if @id > 0
BEGIN
    SET @id = (SELECT MAX(Juegos.id) FROM Juegos)
END

SET @id = @id + 1

INSERT INTO Juegos(Juegos.id, jugador_id, Juegos.partidaGanada, Juegos.puntos) values (@id, (SELECT (Jugador.id) FROM Jugador WHERE Jugador.username = @username AND Jugador.password = @password), @partidaGanada, @puntos)

END

This is 'myFunction' where I provide the parameters:

...
Dim parametros(3) As SqlParameter
parametros(0) = New SqlParameter("@username", game.Jugador.Nombre) //string
parametros(1) = New SqlParameter("@password", game.Jugador.Password) //string
parametros(2) = New SqlParameter("@puntos", game.Puntos) //integer
parametros(3) = New SqlParameter("@partidaGanada", game.PartidaGanada) //boolean

    Dim cmd As New SqlCommand

    cmd.Connection = cn
    cmd.CommandType = CommandType.Text
    cmd.CommandText = procedureName

    If (parameters IsNot Nothing) Then //parameters isnot nothing, I checked
        cmd.Parameters.AddRange(parameters)
    End If


    openCn() //there are no issues with the connection to the database, I checked that aswell
    cmd.ExecuteNonQuery()
    closeCn()
...

These are the values for the parameters right before the exception happens:

@username = "hello"
@password = "123"
@puntos = 50
@partidaGanada = false

Why am I getting this error despite providing all of the parameters with values?

Upvotes: 0

Views: 66

Answers (3)

Sean Lange
Sean Lange

Reputation: 33581

Change your command type to stored procedure. :)

Upvotes: 1

Tim
Tim

Reputation: 4101

Did you include CommandType on your command?

Upvotes: 1

user4492311
user4492311

Reputation:

As vishal mentioned int the comments you need to add these parameters to the command.

Add below statements after creating/defining all your parameters.

cmd.Parameters.AddRange(parametros)

Upvotes: 1

Related Questions