I already can delete the database and wanted to show the error when there is no data in the selected row (datagridview), but it is only work for the first time when user not entering any data (or the datagridview still have empty row).
private void DeleteDatabase(object sender, EventArgs e)
{
DataTable dt = (DataTable)dataGridView1.DataSource;
if (dt.Rows.Count > 0)
{
int rowNum = dataGridView1.CurrentRow.Index;
int id = Convert.ToInt32(dt.DefaultView[rowNum]["ID"]);
dt.DefaultView[rowNum].Delete();
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Table] WHERE [ID] = @ID";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
cmd.Parameters.AddWithValue("@ID", id);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
else if (dt.Rows.Count <= 0)
{
if (choice.comboBox1.Text == "English")
{
System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
sounds.Play();
MessageBox.Show("There is no Data in the Selected Row!", "Error");
return;
}
}
if (choice.comboBox1.Text == "English")
{
System.Media.SoundPlayer sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
sounds.Play();
MessageBox.Show("Deleted Successfully!", "Deleted");
}
}
When user entering data, and delete it. The data deleted successfully, but when user click "delete" button again after user deleted data, it is gave error like the title.
These below are the screenshots (i mixed 3 screenshots into 1 image):
----Description of Screenshot 1 (Left Image)-----:
when i run the program at 12:38 AM, and when i click "delete" button, it gave like the screenshot (1) above (Note: it is correct).
-----Description of Screenshot 2 (Middle Image)-----:
the program still active at 12:39 AM, and everything still fine (the data has been added, and the "delete" function runs, because there is a data in the selected row).
----Description of Screenshot 3 (Right Image)-----:
the program still active at 12:40 AM, the previous data (see screenshot 2) has been deleted, and the selected rows empty again and when i click the "delete" button, the message box didn't show like the screenshot 1, however it gave the same error. (This is the problem that i am facing).
Note: Sorry for the small images size.
The problem is: Why while user not entering any data, the messagebox of There is no data in the selected row
runs when user click the "delete" button. But when user entering any data (let's say the data has been entered into the first row, and the selection will be full row at the first row) and click the "delete" button, it delete the data in the selected row and when user click "delete" again, it didn't show the messagebox of There is no data in the selected row
, however it show the error like the title.