Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/09/2013, 18:45
Avatar de MLDGATO
MLDGATO
 
Fecha de Ingreso: marzo-2011
Ubicación: Guatemala
Mensajes: 113
Antigüedad: 13 años, 1 mes
Puntos: 2
Pregunta Datos se duplican en SQL Server cuando se Insertan

Hola yo aquí aprendiendo a realizar conexiones entre visual studio y SQL Server, mi aplicación de ASP.NET con C# tiene un formulario y cuando ingreso los datos en mi formulario y hago clic en el boton los datos se guarda en mi tabla de mi base pero se insertan dos veces, este es el codigo que tengo:

Código C:
Ver original
  1. protected void btnGuardar_Click(object sender, EventArgs e)
  2.     {
  3.         string producto, imagen, marca, descripcion;
  4.         int cantidad;
  5.         double costo, precio;
  6.  
  7.         producto = txtProducto.Text;
  8.         imagen = fluImagen.FileName.ToString();
  9.         marca = txtMarca.Text;
  10.         cantidad = int.Parse(txtCantidad.Text);
  11.         costo = double.Parse(txtCosto.Text);
  12.         precio = double.Parse(txtPrecio.Text);
  13.         descripcion = txtDescripcion.Text;
  14.  
  15.         SqlConnection miconexion = new SqlConnection("Data Source=MLDGATO-PC; Initial Catalog=WebApp; User ID=sa; Password=100050500");
  16.         miconexion.Open();
  17.  
  18.         SqlCommand insertar = new SqlCommand(string.Format("INSERT INTO Productos(NombreProducto, ImagenProducto, MarcaProducto, CantidadProducto, CostoProducto, PrecioProducto, DescripcionProducto)VALUES('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')", producto, imagen, marca, cantidad, costo, precio, descripcion), miconexion);
  19.         insertar.ExecuteNonQuery();
  20.        
  21.  
  22.         if (insertar.ExecuteNonQuery() > 0)
  23.         {
  24.             miconexion.Close();
  25.             lblMensaje.Text = "Los datos se han almacenado satisfactoriamente";
  26.             txtCantidad.Text = "";
  27.             txtCosto.Text = "";
  28.             txtDescripcion.Text = "";
  29.             txtMarca.Text = "";
  30.             txtPrecio.Text = "";
  31.             txtProducto.Text = "";
  32.             txtProducto.Focus();
  33.         }
  34.         else
  35.         {
  36.             lblMensaje.Text = "Ha ocurrido un error los daton no se almacenaron";
  37.             miconexion.Close();
  38.         }
  39.  
  40.     }

Muchas gracias por su ayuda.