Reputation: 21
hello guys i have a problem in the save of information of the absence in the form of absence i had added a datagridview and in there i add tow columns combobox form one for the start time of absence and the other for end time in the datagridview when i click in the bottom of "show" it will add the name and the ID of each student from a table named "Etudiant" and there are a datetimepicker in the corner out of the datagridview so i need when the user choose the time of absence and the date of each student and he click in the bottom "save" it have to be saved in an other table called "Absence" and this is my try and there are an error please help mee it's the baccalaureate Project :)
private void button2_Click(object sender, EventArgs e)
{ // Open the connection using the connection string.
SqlConnection con = new SqlConnection(@"Data Source=WIN-6Q836P8JQ1C\oby;Initial Catalog=Etudiant;Integrated Security=True");
con.Open();
string sqlQuery = "INSERT INTO Abscence (CIN,Heure_debut,Heure_fin,Date)";
sqlQuery += "VALUES ('CIN', 'Heure_debut', 'Heure_fin', 'Date')";
// Insert into the Sql table. ExecuteNonQuery is best for inserts.
using (SqlCommand com = new SqlCommand(sqlQuery, con))
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
com.Parameters.AddWithValue("CIN", dataGridView1.Rows[i].Cells["CIN"].Value);
com.Parameters.AddWithValue("Heure_debut", dataGridView1.Rows[i].Cells["column1"].Value);
com.Parameters.AddWithValue("Heure_fin",dataGridView1.Rows[i].Cells["column2"].Value);
com.Parameters.AddWithValue("Date", dateTimePicker1.Text);
}
com.ExecuteNonQuery();
com.Parameters.Clear();
con.Close();
}
}
}
Upvotes: 0
Views: 47
Reputation: 1538
Please make sure the values you are entering are going into the insert's.
private void button2_Click(object sender, EventArgs e)
{ // Open the connection using the connection string.
SqlConnection con = new SqlConnection(@"Data Source=WIN-6Q836P8JQ1C\oby;Initial Catalog=Etudiant;Integrated Security=True");
con.Open();
string sqlQuery = "INSERT INTO Abscence (CIN,Heure_debut,Heure_fin,Date)";
sqlQuery += "VALUES (@CIN, @Heure_debut, @Heure_fin, @Date)";
// Insert into the Sql table. ExecuteNonQuery is best for inserts.
using (SqlCommand com = new SqlCommand(sqlQuery, con))
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
com.Parameters.AddWithValue("@CIN", dataGridView1.Rows[i].Cells["CIN"].Value);
com.Parameters.AddWithValue("@Heure_debut", dataGridView1.Rows[i].Cells["column1"].Value);
com.Parameters.AddWithValue("@Heure_fin",dataGridView1.Rows[i].Cells["column2"].Value);
com.Parameters.AddWithValue("@Date", dateTimePicker1.TextToString("YYYY-mm-DD");
}
com.ExecuteNonQuery();
com.Parameters.Clear();
con.Close();
}
}
}
Upvotes: 1