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();

Ajay KumarPosted Sep 12, 2023, 10:20 AM
It seems like you are trying to insert student records into a database and handle the parent's phone numbers based on certain conditions. Your code is close to achieving this, but there are a few issues and improvements you can make:
Declare the
val9variable outside the conditional statements so that it's accessible throughout the scope of your code.Use parameterized queries to prevent SQL injection vulnerabilities.
Here's an improved version of your code:
In this code:
val9outside the conditional statements so that it's accessible later when constructing the SQL command.usingblocks for theOleDbConnectionto ensure proper disposal and management of resources.Please replace
"connectionString"with your actual database connection string.This code will insert student records into the database, taking the mother's phone number if available, the father's phone number if not, and showing a message if neither is available for a student.
Mehmet FatihPosted Sep 12, 2023, 7:16 PM
Thanks both of you. Both of your answers are working very well.
Amit MohantyPosted Sep 12, 2023, 10:35 AM