• Viernes 17 de Mayo de 2024, 04:48

Autor Tema:  RESTRINGIR DATOS DUPLICADO DE FORMULARIO A BASE DE DATOS  (Leído 1531 veces)

Rafa_Code

  • Nuevo Miembro
  • *
  • Mensajes: 7
    • Ver Perfil
RESTRINGIR DATOS DUPLICADO DE FORMULARIO A BASE DE DATOS
« en: Martes 10 de Mayo de 2011, 22:27 »
0
Buenas tardes amigos. Les adjunto mi codigo en el evento Click de button1 que uso para guardar datos ela base de datos! Necesito que me ayuden ha hacr una validacion para que "no me guarde datos dupicados". Mi base de datos ya tiene como CLAVE PRINCIPAL LA CEDULA y ya le di la opcion en de SIN DUPLICADOS (access) cuando lo corro me da un error que no pueden haber campos duplicados ,,,pero yo lo quier es un codigo que si hay camplos duplicados entonces muestre MSGBOX y no que se detenga por un error  en esta linea  int resultado = cmda.ExecuteNonQuery();

gracias espero pronta ayuda!!

Código: C#
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             OpenFileDialog opf = new OpenFileDialog();
  4.             opf.Filter = "Archivos de imagen|*.BMP;*.JPG;*.GIF;*.PNG";
  5.             opf.ShowDialog();
  6.  
  7.             this.textBox9.Text = opf.FileName;
  8.         }
  9.  
  10.         private void button2_Click(object sender, EventArgs e)//validar textbox vacios con CICLO antes de guardar o ACeptar
  11.         {
  12.             for (int i = 0; i < arrText.Length; ++i)
  13.             {
  14.                 if (arrText[i].Text == "")
  15.                 {
  16.                     MessageBox.Show("Debe completar todos los campos", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  17.                     return;
  18.                 }
  19.             }
  20.  
  21.  
  22.  
  23.             OleDbConnection cnn = new OleDbConnection();
  24.             cnn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|db.accdb";
  25.  
  26.             OleDbCommand cmda = new OleDbCommand();
  27.  
  28.             cmda.Connection = cnn;
  29.  
  30.  
  31.             cmda.CommandText = "INSERT INTO ALUMNOS(CI, PrimerNombre, SegundoNombre, PrimerApellido, SegundoApellido, Edad, NumeroContacto, CorreoContacto, Foto, monto_inscripcion,estatus) VALUES(@AA, @BB, @CC, @DD, @EE, @FF, @GG, @HH, @II, 350, 1)";
  32.             cmda.Parameters.AddWithValue("@AA", this.textBox1.Text);
  33.             cmda.Parameters.AddWithValue("@BB", this.textBox2.Text);
  34.             cmda.Parameters.AddWithValue("@CC", this.textBox3.Text);
  35.             cmda.Parameters.AddWithValue("@DD", this.textBox4.Text);
  36.             cmda.Parameters.AddWithValue("@EE", this.textBox5.Text);
  37.             cmda.Parameters.AddWithValue("@FF", this.textBox6.Text);
  38.             cmda.Parameters.AddWithValue("@GG", this.textBox8.Text);
  39.             cmda.Parameters.AddWithValue("@HH", this.textBox7.Text);
  40.             //cmda.Parameters.AddWithValue("@JJ", this.textBox10.Text); //ultimo textbox incluido de Direccion
  41.  
  42.             Byte[] arr = GlamConfig.getArrImg(arrText[arrText.Length - 1].Text.Trim().Length > 0 ? arrText[arrText.Length - 1].Text : Application.StartupPath + @"iconsno.gif");
  43.             OleDbParameter pa = new OleDbParameter();
  44.             pa.Value = arr;
  45.             pa.OleDbType = OleDbType.VarBinary;
  46.             pa.Size = arr.Length;
  47.             pa.ParameterName = "@II";
  48.             cmda.Parameters.Add(pa);
  49.  
  50.             cnn.Open();
  51.  
  52.            
  53.            [b] int resultado = cmda.ExecuteNonQuery();[/b][u][u][i][color=#FF0000][/color][/i][/u][/u]
  54.            
  55.  
  56.             cnn.Close();
  57.  
  58.             if (resultado > 0)
  59.                 MessageBox.Show("Alumna Registrada");
  60.             else
  61.  
  62.                 MessageBox.Show("Alumna no registrada. Ocurrio un Error");
  63.             cmda.Parameters.Clear();
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.         }
  71.