SQL DataGrid..Creating a Delete Button
Hello,
I am having trouble making my code for my delte button to work.
I am connected to an SQL database, and am viewing the data via a data grid, I've made a delete button on my datagrid, i just need to know the code to put in the DeleteButtonEvent()
Thanks for your help everyone!
Re: SQL DataGrid..Creating a Delete Button
Quote:
Originally Posted by joefox
Hello,
I am having trouble making my code for my delte button to work.
I am connected to an SQL database, and am viewing the data via a data grid, I've made a delete button on my datagrid, i just need to know the code to put in the DeleteButtonEvent()
Thanks for your help everyone!
I hope you are not maintaining this connection to the database at all times :(.
You are connecting to the database only when you need to and cleaning up resources right after that right ? Well anyways in any event, a delete from a database requires that there are fields which make the row unique.
Let me give you an example of a BAD or POORLY designed db where the db cannot delete the single row. Say a db has not defined some sort of key and 10 records have the value "hello". If you try to tell your db grid to delete that row, it doesn't really know which row to delete...so it deletes them all. So first off determine whether your datagrid has a datakeyfield. In addition, an ID number.
Now to delete a record simply call a stored procedure (if you are using SQL Server) to delete the record passing in this IDentifier.
Code:
DELETE FROM myTable WHERE myID = @ID
After the delete is done, simply rebind that datagrid. You could create a BindGrid() function that simply refreshes that datagrid by calling a SELECT stmnt to place all the rows into the grid now. You will notice that the deleted row is no longer there.