Ilyas Zahir

Ilyas Zahir

  • NA
  • 86
  • 25.2k

Check if record exists in DB Mysql c#

Jul 18 2017 1:51 PM
hi experts my code is like that check on table1 if textbox1.Text exist if yes go to table2 and check if textbox1.text exist and if textbox2.Text exist if they exist open the form if not insert them and open the form and if textbox1.text exist and textbox2.text doesn't exist close form

i wrote this :
  1. MySqlConnection con = new MySqlConnection(connectionString);  
  2.             i = 0;  
  3.             con.Open();  
  4.             MySqlCommand cmd = con.CreateCommand();  
  5.             cmd.CommandType = CommandType.Text;  
  6.             cmd.CommandText = "select * from informat where u_ser = '" + metroTextBox2.Text + "' ";  
  7.             cmd.ExecuteNonQuery();  
  8.             DataTable dt = new DataTable();  
  9.             MySqlDataAdapter da = new MySqlDataAdapter(cmd);  
  10.             da.Fill(dt);  
  11.             i = dt.Rows.Count;  
  12.             int userial = metroTextBox2.Text.Length;  
  13.             if (userial >= 5 || metroTextBox2.Text == null)  
  14.             {  
  15.   
  16.                 MySqlConnection conet = new MySqlConnection(connectionString);  
  17.                 conet.Open();  
  18.                 MySqlCommand cmsd = conet.CreateCommand();  
  19.                 cmsd.CommandType = CommandType.Text;  
  20.                 cmsd.CommandText = "select * from alred  where u_ser = '" + metroTextBox2.Text + "'and u_ip =  '" + textBox1.Text + "'";  
  21.                 cmsd.ExecuteNonQuery();  
  22.                 DataTable ddts = new DataTable();  
  23.                 MySqlDataAdapter ddas = new MySqlDataAdapter(cmsd);  
  24.                 ddas.Fill(ddts);  
  25.   
  26.                 if (textBox1.Text != null)  
  27.                 {  
  28.                     MessageBox.Show("Your Serial is used in another computer");  
  29.                     this.Close();  
  30.                 }  
  31.                 else if (metroTextBox2.Text == null & textBox1.Text == null)  
  32.                 {  
  33.                     ireview frms = new ireview();  
  34.                     this.Hide();  
  35.                     frms.Show();  
  36.                 }  
  37.                  
  38.                 else  if (metroTextBox2.Text != null && textBox1.Text != null)  
  39.                 {  
  40.                     MySqlConnection conect = new MySqlConnection(connectionString);  
  41.                     conect.Open();  
  42.                     MySqlCommand cmsdd = conet.CreateCommand();  
  43.                     cmsdd.CommandType = CommandType.Text;  
  44.                     cmsdd.CommandText = "INSERT INTO alred  (u_ser, u_ip) " + " Values ('" + metroTextBox2.Text + "', '" + textBox1.Text + "')";  
  45.                     cmsdd.ExecuteNonQuery();  
  46.                     DataTable sddts = new DataTable();  
  47.                     MySqlDataAdapter sddas = new MySqlDataAdapter(cmsdd);  
  48.                     sddas.Fill(sddts);  
  49.                     ireview frms = new ireview();  
  50.                     this.Hide();  
  51.                     frms.Show();  
  52.                 }  
  53.             }  
  54.             else  
  55.             {  
  56.                 MessageBox.Show("You Serial Doesn't Exist");  
  57.             }  
  58.         }  
but it doesn't work its always go directly to insert and it insert it two times

Answers (3)