Visual Studio 2008 asp vb code: SQLDatasource using "delete" button with additional commands?

Posted on 2012-08-29
Last Modified: 2012-09-09
I presently have a gridview set up and I have a "delete" button column. It seems I need to use the "configure data source" set up and specify a delete script on the "delete tab" to run upon use of this button...

That all sounds find and good and I understand that-I can definitely write up an SQL statement that covered deletion of the row associated with the botton clicked, BUT...I need to perform multiple table updates and generally do other stuff when the delete button associated with a given row is pressed. Its not a matter of simply dropping the selected row from the one table and moving on...

How does one use an SQLDatasource with a Delete Button column in the gridview AND also do things on button click in the codebehind subs?
Question by:SullivanMIS

    Author Comment

    I am still experimenting here, but I think if I make the button a "select button" and have it display the word "delete", I gain access to a sub for "on selected index changed". Then I can do whatever I want. This seems to give me options to me than having a "delete command button".

    I will report back with my results, but this may be an effective work around.
    LVL 25

    Expert Comment

    First of all create the delete button and make sure it has a Delete command

        <asp:Button id="btn_delete" runat="server" CommandName="Delete" />

    Open in new window

    Then make sure you GridView uses the DataKeyNames property which includes the rowID in each GridViewRow:

    <asp:GridView ID="myGV" runat="server" DataKeyNames="IDColumnName" .... >

    Open in new window

    Then create the Delete event for the GridView which looks up the row ID.  At this point if you need to use multiple commands you should use a stored procedure rather than lines of T-SQL:

    	Protected Sub myGV_RowDeleting(sender As Object, e As GridViewDeleteEventArgs) Handles myGV.RowDeleting
    		Dim ID As Integer = Convert.ToInt32(myGV.DataKeys(e.RowIndex).Value)
    		Dim conn As New SqlConnection("ConnectionStringHere")
    		Dim cmd As New SqlCommand
    		cmd.CommandText = "myStoredProcedureName"
    		cmd.CommandType = Data.CommandType.StoredProcedure
    		cmd.Connection = conn
    		Catch ex As Exception
    			Throw ex
    			cmd = Nothing
    			conn = Nothing
    		End Try
    	End Sub

    Open in new window


    Accepted Solution

    The method I described above is working within my code and was fairly simple to set up. Is there any advantage to using the method described by Rouchie as opposed to how I prosposed to do (and am currently doing) this?

    Author Closing Comment

    I found the solution I proposed worked efficiently and saw no posts suggesting why the alternative would be a better solution.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
    In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now