Exception when registering C # SQL [duplicated]


I want to register movies, where you have the fields: txtname, txtanio, txtweb, txt description.

In the database the Id is incredable.

But when I press the high button I get the following error:

And the following code for when you load the form is:

 private void Alta_Load(object sender, EventArgs e)

            conexion = new SqlConnection("DataSource = EMILIANO-PC; InitialCatalog = VideoClub; Integrated Security = True"); 
            adaptador = new SqlDataAdapter();
            SqlCommand alta = new SqlCommand("insert into peliculas values (@nombre_pelicula, @web_pelicula, @descripcion, @anio)", conexion);
            adaptador.InsertCommand = alta;
            adaptador.InsertCommand.Parameters.Add(new SqlParameter("@nombre_pelicula", SqlDbType.VarChar));
            adaptador.InsertCommand.Parameters.Add(new SqlParameter("@web_pelicula", SqlDbType.VarChar));
            adaptador.InsertCommand.Parameters.Add(new SqlParameter("@descripcion", SqlDbType.VarChar));
            adaptador.InsertCommand.Parameters.Add(new SqlParameter("@anio", SqlDbType.Int));


I can not find the error.

asked by Ibarra Emiliano 08.11.2018 в 01:59

1 answer


You do not need to define anything in the Load, you can in the same click of the high button assign the SqlCommand object and the parameters

Something like this

private void btnAlta_Click(object sender, EventArgs e)
    string connstring = "DataSource = EMILIANO-PC; InitialCatalog = VideoClub; Integrated Security = True";
    usig(var conexion = new SqlConnection(connstring)) 
        string query = "insert into peliculas values (@nombre_pelicula, @web_pelicula, @descripcion, @anio)";
        SqlCommand alta = new SqlCommand(, conexion);
        alta.Parameters.Add("@nombre_pelicula", SqlDbType.VarChar).Value = txtnombre.Text;
        alta.Parameters.Add("@web_pelicula", SqlDbType.VarChar).Value = txtweb.Text;
        alta.Parameters.Add("@descripcion", SqlDbType.VarChar).Value = txtdescripcion.Text;
        alta.Parameters.Add("@anio", SqlDbType.Int).Value = Convert.ToTnt32(txtanio.Text);



Neither do you need any adapter, use the direct command

answered by 08.11.2018 в 04:44