• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

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

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?
0
SullivanMIS
Asked:
SullivanMIS
  • 3
1 Solution
 
SullivanMISAuthor Commented:
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.
0
 
RouchieCommented:
First of all create the delete button and make sure it has a Delete command

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

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
		Try
			conn.Open()
			cmd.ExecuteNonQuery()
		Catch ex As Exception
			Throw ex
		Finally
			conn.Close()
			cmd = Nothing
			conn = Nothing
		End Try
	End Sub

Open in new window

0
 
SullivanMISAuthor Commented:
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?
0
 
SullivanMISAuthor Commented:
I found the solution I proposed worked efficiently and saw no posts suggesting why the alternative would be a better solution.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now