Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
bağlanti.Open();
SqlCommand komut = new SqlCommand("insert into RezervasyonKayıt (Rezervasyon_İçerik,Yemek_Tercihi,Kişi_Sayısı,İçecek_Tercihi,Giris_Ekstraları,Yetkili_Kisi,Rezervasyon_Tarihi)values(@İçerik,@Yemek,@Kisi,@İcecek,@Giris,@Yetkili,@RezTarih)", bağlanti);
komut.Parameters.AddWithValue("@İçerik", comboBox1.SelectedItem);
komut.Parameters.AddWithValue("@Yemek", comboBox2.SelectedItem);
komut.Parameters.AddWithValue("@Kisi", comboBox3.SelectedItem);
komut.Parameters.AddWithValue("@İcecek", comboBox4.SelectedItem);
komut.Parameters.AddWithValue("@Giris", comboBox5.SelectedItem);
komut.Parameters.AddWithValue("@Yetkili", comboBox6.SelectedItem);
komut.Parameters.AddWithValue("@RezTarih", dateTimePicker1.Value);
komut.ExecuteNonQuery();
comboBox1.Text = "";
comboBox2.Text = "";
comboBox3.Text = "";
comboBox4.Text = "";
comboBox5.Text = "";
comboBox6.Text = "";
bağlanti.Close();
XtraMessageBox.Show("Randevu Başarıyla Kayıt Edilmiştir", "Bilgi Mesajı", MessageBoxButtons.OK, MessageBoxIcon.Information);

What I have tried:

before value saved in database check if Rezervasyon_Tarihi is exist in database


string cmd = @"SELECT COUNT(*) From RezervasyonKayıt WHERE Rezervasyon_Tarihi=@RezTarih))";
         SqlCommand komut = new SqlCommand(cmd, bağlanti);
          komut.Parameters.AddWithValue("@RezTarih", dateTimePicker1.Value);
          bağlanti.Open();
          int records = (int)komut.ExecuteScalar();

          if (records == 0)
          {
              komut.Parameters.Clear();
              cmd = @"insert into RezervasyonKayıt (Rezervasyon_İçerik,Yemek_Tercihi,Kişi_Sayısı,İçecek_Tercihi,Giris_Ekstraları,Yetkili_Kisi,Rezervasyon_Tarihi)values(@İçerik,@Yemek,@Kisi,@İcecek,@Giris,@Yetkili,@RezTarih)";
              komut = new SqlCommand(cmd, bağlanti);
              komut.Parameters.AddWithValue("@İçerik", comboBox1.SelectedItem);
              komut.Parameters.AddWithValue("@Yemek", comboBox2.SelectedItem);
              komut.Parameters.AddWithValue("@Kisi", comboBox3.SelectedItem);
              komut.Parameters.AddWithValue("@İcecek", comboBox4.SelectedItem);
              komut.Parameters.AddWithValue("@Giris", comboBox5.SelectedItem);
              komut.Parameters.AddWithValue("@Yetkili", comboBox6.SelectedItem);
              komut.Parameters.AddWithValue("@RezTarih", dateTimePicker1.Value);
              komut.ExecuteNonQuery();
              comboBox1.Text = "";
              comboBox2.Text = "";
              comboBox3.Text = "";
              comboBox4.Text = "";
              comboBox5.Text = "";
              comboBox6.Text = "";

              XtraMessageBox.Show("Randevu Başarıyla Kayıt Edilmiştir", "Bilgi Mesajı", MessageBoxButtons.OK, MessageBoxIcon.Information);
          }
          else
          {
              MessageBox.Show("Already Exist !");
          }
Posted
Updated 21-Nov-17 8:56am
v2

1 solution

I suggest you change your sql and make it a stored procedure. It will be much easier to write that way. In the stored procedure you can use
SQL
IF EXISTS (SELECT 1 FROM table WHERE field = @value)
  BEGIN
    -- do the update
  END
ELSE
  BEGIN
   -- do the insert
  END 


Or you can use the MERGE statement. MERGE (Transact-SQL) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900