I have an error while executing my project. I have two commands in this button. Can someone help me with this? I can't detect the problem. It is working fine before but now it occurred a problem. I'm developing an library system
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "INSERT INTO Borrowbook ([Student ID], ISBN, Title, Date, [Due Date]) VALUES (@StudentID, @ISBN, @Title, @Date, @DueDate)";
Control[] controls = { textBox2, textBox3, textBox4 };
foreach (Control c in controls)
{
if (c.Text.Trim() == "")
{
MessageBox.Show("Please complete the fields", "Information...",
MessageBoxButtons.OK, MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1);
return;
}
}
SqlParameter p1 = new SqlParameter("@StudentID", SqlDbType.NChar);
p1.Value = textBox2.Text;
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("@ISBN", SqlDbType.NVarChar);
p2.Value = textBox4.Text;
cmd.Parameters.Add(p2);
SqlParameter p3 = new SqlParameter("@Title", SqlDbType.VarChar);
p3.Value = textBox3.Text;
cmd.Parameters.Add(p3);
SqlParameter p4 = new SqlParameter("@Date", SqlDbType.DateTime);
p4.Value = dateTimePicker1.Text;
cmd.Parameters.Add(p4);
SqlParameter p5 = new SqlParameter("@DueDate", SqlDbType.DateTime);
p5.Value = duedate.Text;
cmd.Parameters.Add(p5);
cmd.ExecuteNonQuery();
cmd.CommandText = ("UPDATE Books SET Availability = 'Borrowed' WHERE ISBN = '" + textBox4.Text + "'");
cmd.ExecuteNonQuery();
cmd.CommandText = ("INSERT INTO Penalty([Student ID], Title, [Date Borrow], [Due Date]) VALUES (@StudentID1, @Title1, @Date1, @Date2)");
SqlParameter pp1 = new SqlParameter("@StudentID1", SqlDbType.VarChar);
pp1.Value=textBox2.Text;
cmd.Parameters.Add(pp1);
SqlParameter pp3 = new SqlParameter("@Title1", SqlDbType.NVarChar);
pp3.Value = textBox3.Text;
cmd.Parameters.Add(pp3);
SqlParameter pp4 = new SqlParameter("@Date1", SqlDbType.DateTime);
pp4.Value = dateTimePicker1.Text;
cmd.Parameters.Add(pp4);
SqlParameter pp5 = new SqlParameter("@Date2",SqlDbType.DateTime);
pp5.Value=duedate.Text;
cmd.Parameters.Add(pp5);
cmd.ExecuteNonQuery();
MessageBox.Show("The books has been successfully borrowed!", "Information ... ", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);