Displaying the FAQs with a Category Name and Turning on the DataGrid's Editing Features

Note that when you click on the "Edit" button for a particular row, there are TextBoxes for the Category name. Remember, though, that this information is stored as a foreign-key interger in our tblFAQ table!


 FAQ IDCategoryFAQ Question
Edit2StringsHow do you convert a string to all uppercase or all lowercase?
Edit12EmailHow can I display line breaks in an email message?
Edit14EmailCan I send emails without using CDONTS?
Edit15StringsHow can I determine the length of a string (how many characters are in a string)?
Edit17StringsHow can I find the position of a certain character (or substring) within a string?
Edit19Dates and TimesHow can I return the current date/time?
Edit20Functions and SubroutinesWhat is recursion?
Edit23FileSystemObjectWhat, exactly, is the FileSystemObject?
Edit25EmailHow can I send an email using CDONTS?
Edit8Databases, GeneralHow do you create a database table through a SQL statement (via an ASP page, perhaps)?
Edit16Dates and TimesUsing VBScript, how can I determine if a variable is a valid date or not?
Edit18Dates and TimesHow can I easily display a nicely formatted date variable?
Edit24FileSystemObjectHow can I read the contents of a text file through an ASP page?
Edit7ArraysHow do I dynamically resize an array?
Edit11EmailHow can I have my ASP page send HTML-formatted email?
Edit10Databases, GeneralHow can I tell what version of ADO I am using?
Edit13EmailHow do you send email attachments through an ASP page?
Edit21ArraysHow can I determine the upper or lower bounds of an array?
Edit22EmailHow 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"))


  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
      BindData()
    End If
  End Sub

	
  Sub BindData()
    '2. Create the command object, passing in the SQL string
    Const strSQL as String = "SELECT FAQID, F.FAQCategoryID, F.Description, FC.Name AS CategoryName FROM tblFAQ F INNER JOIN tblFAQCategory FC ON F.FAQCategoryID = FC.FAQCategoryID 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) 
    myDataAdapter.Fill(resultsDataSet)   

    dgPopularFAQs.DataSource = resultsDataSet
    dgPopularFAQs.DataBind()
  End Sub


  Sub dgPopFAQs_Edit(sender As Object, e As DataGridCommandEventArgs)
    dgPopularFAQs.EditItemIndex = e.Item.ItemIndex
    BindData()
  End Sub

  Sub dgPopFAQs_Cancel(sender As Object, e As DataGridCommandEventArgs)
    dgPopularFAQs.EditItemIndex = -1
    BindData()
  End Sub

  Sub dgPopFAQs_Update(sender As Object, e As DataGridCommandEventArgs)
    Response.Write("<b>The Update Command has no effect on this live demo...</b>")
    
    dgPopularFAQs.EditItemIndex = -1
    BindData()
  End Sub

</script>

    <asp:datagrid id="dgPopularFAQs" runat="server"
		AutoGenerateColumns="False"
		HeaderStyle-HorizontalAlign="Center"
		HeaderStyle-BackColor="Red"
		HeaderStyle-ForeColor="White"
		HeaderStyle-Font-Bold="True"
		HeaderStyle-Font-Name="Verdana"
		AlternatingItemStyle-BackColor="#dddddd"
		ItemStyle-Font-Name="Verdana"
		
		OnEditCommand="dgPopFAQs_Edit"
		OnCancelCommand="dgPopFAQs_Cancel"
		OnUpdateCommand="dgPopFAQs_Update">
	
	  <Columns>
	    <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="OK" />
	    <asp:BoundColumn DataField="FAQID" ItemStyle-Width="10%" ReadOnly="True"
				ItemStyle-HorizontalAlign="Center" HeaderText="FAQ ID" />
	    
		<asp:BoundColumn DataField="CategoryName" HeaderText="Category" />
	    <asp:BoundColumn DataField="Description" HeaderText="FAQ Question" />	  
	  </Columns>	
	</asp:datagrid>
	


[Return to the article]