Mehmet Fatih

Mehmet Fatih

  • 851
  • 941
  • 42.4k

Conditional registration to database

Sep 12 2023 9:41 AM

I am trying to save tudents's parents phone numbers. I have students’ mother and father's phone numbers in my database. What I want to do is if there is a student’s mother's phone number, it will be registered as the parent's phone number, if there is a student’s father's phone number it will be registered as the father's phone. If both of these are not present, it will not be recorded by giving a warning that “the student's phone number does not exist”.  My priority is to record the mother's phone if she has one, if the mother does not have a phone, then to record the father's phone.    My codes are here :

foreach(int j in ChkedRow)   {

   
  var val1 = dataGridView1.Rows[j].Cells["tcno"].Value; 
  var val2 = dataGridView1.Rows[j].Cells["ono"].Value; 
  var val3 = dataGridView1.Rows[j].Cells["isim"].Value; 
  var val4 = dataGridView1.Rows[j].Cells["soyisim"].Value; 
  var val5 = dataGridView1.Rows[j].Cells["cinsiyet"].Value; 
  var val6 = dataGridView1.Rows[j].Cells["dtarihi"].Value; 
  var val7 = dataGridView1.Rows[j].Cells["sinifi"].Value; 
  var val8 = dataGridView1.Rows[j].Cells["unvan"].Value; 
  var val10 = dataGridView1.Rows[j].Cells["kbaskani"].Value;

   
  if (String.IsNullOrEmpty(dataGridView1.Rows[j].Cells["atel"].Value.ToString()))  {   
    var val9 = dataGridView1.Rows[j].Cells["btel"].Value; 
  } 
  else if (!String.IsNullOrEmpty(dataGridView1.Rows[j].Cells["atel"].Value.ToString()))  {   
    var val9 = dataGridView1.Rows[j].Cells["atel"].Value; 
  } 
  else if (String.IsNullOrEmpty(dataGridView1.Rows[j].Cells["atel"].Value.ToString()) && (String.IsNullOrEmpty(dataGridView1.Rows[j].Cells["btel"].Value.ToString())))  {   MessageBox.Show(" '" + val3 + " " + val4 + "' isimli ögrenciler veli telefonu bulunmamaktadir.");   
    return; 
  } 
  else  {   
    var val9 = dataGridView1.Rows[j].Cells["atel"].Value;

    var cmdText = @"INSERT INTO  gezilistemiz25 (tcno,ono,adi,soyadi,cinsiyet,dtarihi,sinifi,unvani,vtel,kbaskani) VALUES('" + dataGridView1.Rows[j].Cells["tcno"].Value + "'," + dataGridView1.Rows[j].Cells["ono"].Value + ",'" + dataGridView1.Rows[j].Cells["isim"].Value + "', '" + dataGridView1.Rows[j].Cells["soyisim"].Value + "', '" + dataGridView1.Rows[j].Cells["cinsiyet"].Value + "', '" + dataGridView1.Rows[j].Cells["dtarihi"].Value + "','" + dataGridView1.Rows[j].Cells["sinifi"].Value + "', '" + dataGridView1.Rows[j].Cells["unvan"].Value + "','" + val9 + "','" + dataGridView1.Rows[j].Cells["kbaskani"].Value + "')";

    var command = new OleDbCommand(cmdText, conn);
    command.ExecuteNonQuery();
    conn.Close();
    conn.Dispose();


Answers (3)