Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more: , +
Here is the link of datagridview (photo) when i run the program:

[Here is the link]

I want when the user select "ID 9" and click the "delete" button, it will delete the selected row also delete it at database.

How could i do that? I mean, what should i put in my command? I know my command is missing the "WHERE" clause, but i don't know what to put in the "WHERE" clause.

Here is the code that i have been doing right now (the following code is delete all row in a single click)


Here is the code:

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Record]";
conn.Open();

using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
{
DataTable ds = new DataTable();
adapter.Update(ds);
dataGridView.DataSource = ds;
cmd.ExecuteNonQuery();
}
}
}

Now, my problem is, how do i delete it from datagridview and update it to the database when the user select a single row? Please help. Thanks.

i have tried this code:

deleteButton.Click += new System.EventHandler(this.DeleteRecord);

private DeleteRecord(object sender, EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Record] WHERE [ID] = 9";
conn.Open();

using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
{
DataTable ds = new DataTable();
adapter.Update(ds);
dataGridView.DataSource = ds;
cmd.ExecuteNonQuery();
}
}
}
}

The above code is working, but it is select the row where the ID is 9, what if i got ID 10, ID 11 and so on until ID 100, i didn't want to put the query one by one. Is there any ways to do that? I mean, when the user select a row, no matter what ID it is, it will be delete the row and update it to the database.. Thanks
Posted
Updated 11-Sep-13 5:09am
v2

Yes, you should mention WHERE criteria in your query.
C#
string strID = "";//Assign the selected value ... 9
string query = "DELETE FROM [Record] WHERE ID='" + strID + "'";

And wait for OriginalGriff's answer regarding SQL injection.
 
Share this answer
 
Comments
Member 10192100 11-Sep-13 10:40am    
Hai thatraja, what if the selected value is other than 9? How do i let the computer know and when the user select the row which is have 10 in ID, it will be deleted also. Not only 9.
Thanks
thatraja 11-Sep-13 10:48am    
You have to pass the value of selected row. You didn't share the gridview related code.
When you bind data to grid, you're assigning value to ID column too. In runtime, you have to assign the ID value(selected grid row) to strID variable(see my sample code).
Member 10192100 11-Sep-13 11:08am    
and, how do i do that thatraja? i'm sorry, i am new in this matter
thatraja 11-Sep-13 11:16am    
Here an example
How to get the Values of Selected Cell Row DataGridview

BTW you should buy a book for ADO.NET & C#
Member 10192100 11-Sep-13 11:24am    
private void DeleteRecord(object sender, EventArgs e)
{
int i = dataGridView.SelectedCells[0].RowIndex;

if (fifthForm.comboBox1.Text == "English")
{
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
int rowIdToDelete = Convert.ToInt32(row.Cells[i].Value);

using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [Record] WHERE ID = " + rowIdToDelete;

conn.Open();

using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
{
DataTable ds = new DataTable();
adapter.Update(ds);
dataGridView.DataSource = ds;
cmd.ExecuteNonQuery();
}
}
}
}
}

Hai thatraja, i have been doing like above code, but the delete command is not executing. do you know why? Thanks.
You need to use the following,
String query = ("delete from tablename where ID=9,conn)

In here, You can change the ID from 9 to anything. I think You know how to use a variable in it. Else ask.
 
Share this answer
 
Comments
Member 10192100 11-Sep-13 10:35am    
hai @Member 10266163. What if the selected value of "ID" is not 9, but 10 or else more.
How do i do that? I don't know how to change it to the selected value other than 9.
Thanks.
Kaveen Abeywansa 11-Sep-13 10:47am    
Well Then, Just use a variable.
int x = (your ID ex 10,9,etc * this is not a part of the code)
String query = ("delete from tablename where ID="+x,conn)
You can use a textbox to allow the user to enter the Id. If your textbox is named as textBox1,
int x = int.parse(textbox1.text);
sqlconnection conn = new sqlconnection(Connection string);
sqlcommand cmd = new sqlcommand("delete from tablename where ID="+x,conn);
**And the rest
** I used SQL as an example, but you can use another type if you want

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900