如何从texbox c#中获取值以插入数据库

时间:2016-11-21 14:21:14

标签: c# asp.net

我试图在数据库中保存一个值,但是当我进行插入时,数据库不会在texbox从javascript加载时从texbox保存任何数据,这是我的插入内容:

ConectarBD();   
Tabb = new SqlCommand("Insert  into peliculas  Values(@nombre_pelicula,@dias_arriendo,@valor_dia,@total_arriendo,@descuento,@total_pagar) ", Conn);
Dadap.InsertCommand = Tabb;
Dadap.InsertCommand.Parameters.Add("@nombre_pelicula", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("@dias_arriendo", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("@valor_dia", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("@total_arriendo", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("@descuento", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("@total_pagar", SqlDbType.VarChar);


Dadap.InsertCommand.Parameters["@nombre_pelicula"].Value = txtNombrePelicula.Text;
Dadap.InsertCommand.Parameters["@dias_arriendo"].Value = txtNumDias.Text;
Dadap.InsertCommand.Parameters["@valor_dia"].Value = txtValorDia.Text;
Dadap.InsertCommand.Parameters["@total_arriendo"].Value = lblTotalArriendo.Text;
Dadap.InsertCommand.Parameters["@descuento"].Value =txtDescuento.Text;  
Dadap.InsertCommand.Parameters["@total_pagar"].Value = txtTotalPagar.Text;


Dadap.InsertCommand.ExecuteNonQuery();
lblMensaje.Text = "Datos Grabados";
Conn.Close();
Limpiar();
txtRut.Focus();

1 个答案:

答案 0 :(得分:2)

您的插入看起来不对,请尝试以下代码并将column1,2,3 ...更改为数据库中的列名称。

using (SqlCommand cmd = new SqlCommand())
{

cmd.CommandText = "INSERT Into peliculas (Column1, Column1,Column2,Column3,Column4,Column5,Column6) Values(@nombre_pelicula,@dias_arriendo,@valor_dia,@total_arriendo,@descuento,@total_pagar)";

cmd.Connection = con;
cmd.Parameters.AddWithValue("@nombre_pelicula", txtNombrePelicula.Text);
cmd.Parameters.AddWithValue("@dias_arriendo", txtNumDias.Text);
cmd.Parameters.AddWithValue("@valor_dia", txtValorDia.Text);
cmd.Parameters.AddWithValue("@total_arriendo", lblTotalArriendo.Text);
cmd.Parameters.AddWithValue("@descuento", txtDescuento.Text);
cmd.Parameters.AddWithValue("@total_pagar", txtTotalPagar.Text);

}
cmd.ExecuteNonQuery();