private void pictureBox2_Click(object sender, EventArgs e) { if (Course_list.SelectedItems.Count == 1) { string c_name = Course_list.SelectedItems[0].Text.Split(new char[] { ',' })[0]; SqlDataReader reader = db.Select("*", "NewSchedule"); while (reader.Read()) { if (reader["Name"].ToString().Trim().Equals(c_name.Trim())) { if (reader["Classroom"].ToString().Trim().Equals(Course_list.SelectedItems[0].Name)) { LectureID.Add(Int32.Parse(reader["LectureID"].ToString().Trim())); break; } } } reader.Close(); if (LectureID.Count == 1) { if (MessageBox.Show("Are you sure to delete?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { DataBase db2 = new DataBase(); SqlCommand cmd = new SqlCommand("DELETE FROM NewSchedule WHERE LectureID = @sn", db2.cnn); cmd.Parameters.AddWithValue("@sn", LectureID[0]); db2.Connect(); try { cmd.ExecuteNonQuery(); MessageBox.Show("Success"); } catch (Exception ex) { MessageBox.Show("Error"); } Course_list.Items.Remove(Course_list.SelectedItems[0]); } } } }