Choosing a Row to Delete

This demo illustrates how to handle the event the Delete command button raises when clicked. Note that we know which DataGrid row was clicked, but we've yet to determine a means to know what the value of the primary key field (FAQID) for the row is. (We would need the row's FAQID in order to delete the record from the database...

 FAQ IDQuestion
Delete2How do you convert a string to all uppercase or all lowercase?
Delete12How can I display line breaks in an email message?
Delete14Can I send emails without using CDONTS?
Delete15How can I determine the length of a string (how many characters are in a string)?
Delete17How can I find the position of a certain character (or substring) within a string?
Delete19How can I return the current date/time?
Delete20What is recursion?
Delete23What, exactly, is the FileSystemObject?
Delete25How can I send an email using CDONTS?
Delete8How do you create a database table through a SQL statement (via an ASP page, perhaps)?
Delete16Using VBScript, how can I determine if a variable is a valid date or not?
Delete18How can I easily display a nicely formatted date variable?
Delete24How can I read the contents of a text file through an ASP page?
Delete7How do I dynamically resize an array?
Delete11How can I have my ASP page send HTML-formatted email?
Delete10How can I tell what version of ADO I am using?
Delete13How do you send email attachments through an ASP page?
Delete21How can I determine the upper or lower bounds of an array?
Delete22How can I validate an email adress using Jscript?

Source Code
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
  'Create a connection
  Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
  Dim ddlDataSet as DataSet = New DataSet()

  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
    End If
  End Sub
  Function GetCategories() as DataSet
    'Populate the ddlDataSet
    Const strSQLDDL = "SELECT FAQCategoryID, Name FROM tblFAQCategory ORDER BY Name"    
    Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQLDDL, myConnection)    
    myDataAdapter.Fill(ddlDataSet, "Categories")

    Return ddlDataSet
  End Function

  Sub BindData()
    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "SELECT FAQID, F.Description FROM tblFAQ F WHERE FAQID <= 25"

    'Set the datagrid's datasource to the datareader and databind
    Dim resultsDataSet as New DataSet()
    Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(strSQL, myConnection) 

    dgPopularFAQs.DataSource = resultsDataSet
  End Sub

  Sub dgPopularFAQs_Delete(sender As Object, e As DataGridCommandEventArgs)
    lblResults.Text = "Opted to delete DataGrid row " & e.Item.ItemIndex
  End Sub

  <form runat="server">
    <asp:label id="lblResults" Font-Bold="True" runat="server"
			Font-Italic="True" ForeColor="#aa0000" />
    <asp:datagrid id="dgPopularFAQs" runat="server"

		<asp:ButtonColumn Text="Delete" CommandName="Delete" />
		<asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID"
		     ItemStyle-HorizontalAlign="Center" />
		<asp:BoundColumn DataField="Description" HeaderText="Question" />

[Return to the article]