The query expects the parameter @Name that has not been provided

1
public bool IngresarUsuario(Prueba objeto)
{
    using (SqlConnection cn = Conexion.Conectar("cadenaCon"))
    {

        try
        {
            cn.Open();
            query = ("insert into Pruebas (Id,Nombre,Componente)values (@Id,@Nombre,@Componente)");
            comando = new SqlCommand(query);
            comando.CommandType = CommandType.Text;
            comando.Connection = cn;
            comando.Parameters.AddWithValue("@Id", objeto.Id);
            comando.Parameters.AddWithValue("@Nombre", objeto.Nombre);
            comando.Parameters.AddWithValue("@Componente", objeto.Componente);
            if (Convert.ToBoolean(comando.ExecuteNonQuery()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        finally { cn.Close(); }
    }
}
    
asked by Jesus Galeano 08.11.2016 в 18:47
source

2 answers

3

This error occurs when the value you pass to SqlCommand.AddWithValue() is null .

In this case, objeto.Nombre should be null , and maybe that was not your intention. Make sure that objeto.Nombre has a valid value.

Now, if your intention is really to insert null for @Nombre when objeto.Nombre is null , then this can be achieved like this:

comando.Parameters.AddWithValue("@Nombre", (object)objeto.Nombre ?? DBNull.Value);
    
answered by 08.11.2016 в 19:33
0

I recommend that you occupy the following way:

string query = "INSERT INTO Pruebas (Id,Nombre,Componente) VALUES (@Id,@Nombre,@Componente);
SqlCommand command = new SqlCommand(query , cn);
command.Parameters.Add("@Nombre", SqlDbType.VarChar, 100);
command.Parameters["@Nombre"].Value = objecto.Nombre;
command.Parameters.Add("@Id", SqlDbType.Int);
command.Parameters["@Id"].Value = objecto.Id;
command.Parameters.Add("@Componente", SqlDbType.VarChar, 100);
command.Parameters["@Componente"].Value = objecto.Componente;
cn.Open();
command.ExecuteNonQuery();
    
answered by 08.11.2016 в 22:02