-2
if (comboBox2.Text == "A") {
  baglanti.Open();
  cmd = new MySqlCommand("SELECT `Sifreler`FROM `9sinifgiris` WHERE 1", baglanti);
  dr = cmd.ExecuteReader();
  if (dr.Read()) // Veriyi çektik
  {
    if (textBox1.Text == dr["Sifreler"].ToString()) {
      baglanti.Close();
      MessageBox.Show("Parola aynı giriş yap");
      button1.Enabled = true;
      baglanti.Open();
      cmd = new MySqlCommand("insert into 9sinifgiris (9A) Values ('TestText')", baglanti);
      cmd.ExecuteNonQuery(); // GETTİNG ERROR FROM HERE 
    } else {
      MessageBox.Show("Parola yanlış");
      textBox1.Clear();
    }
  }
  baglanti.Close();
}

I'm getting an error from cmd.ExecuteNonQuery();:

MySql.Data.MySqlClient.MySqlException: 'Field 'Sifreler' doesn't have a default value'

I don't want to write Sifreler into the database, but the error is with this line.

I used to work with this code for my old project and it worked well. I want to add the text I write into the database.

Please help.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Error is pretty clear. Populate that field or change the restrictions on the schema level. – tadman Aug 16 '17 at 22:17
  • "I don't want to write Sifreler into the database" Well, your database says you _have to fill it_ so either fill it or change your database. By the way, just close your connection when you're done. Right now it is very confusing that you close and open it inside your inner `if` block. – oerkelens Aug 16 '17 at 22:21
  • Do you want to add the text to the existing record, or actually insert a new record, by the way? – oerkelens Aug 16 '17 at 22:22
  • Please read [ask] and take the [tour]. `I am beginner...` is a terrible title - it tells us nothing about the *problem*. – Ňɏssa Pøngjǣrdenlarp Aug 16 '17 at 22:26
  • Yes sir i understand. Actually i didn't know how do i explain my problem. Next time i will be better. – Mehmet Can Aug 16 '17 at 23:01

1 Answers1

0

Did you make sure that field in your database design allows null? Or maybe you need to take it out of strict mode: https://stackoverflow.com/a/18751364/1830512.

midoriha_senpai
  • 177
  • 1
  • 16