Velocity Reviews

Velocity Reviews (
-   ASP .Net Web Controls (
-   -   Use the DataList Control to Present and Edit Data PART 2 (

Alex 12-07-2003 03:09 AM

Use the DataList Control to Present and Edit Data PART 2
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Configuration

Public Class Main : Inherits Page

Private strConn As String =
ConfigurationSettings.AppSettings("ConnectionStrin g")
Public dtlCustomers As DataList

Public Sub Page_Load(sender as Object, e as EventArgs)

If Not IsPostBack Then
End If

End Sub

Private Sub BindTheData()

Dim objConn as new SqlConnection(strConn)
Dim strSQL as String
strSQL = "SELECT Top 5 * FROM Customers"
Dim sda as new SqlDataAdapter(strSQL, objConn)
Dim ds as new DataSet()
dtlCustomers.DataSource = ds.Tables("Customers").DefaultView

End Sub

Next are two short bits of code to handle the dtlCustomers_Edit and
dtlCustomers_Cancel subroutines. Remember in the aspx file above we
set several properties of the Datalist control to call events in the
code-behind file. Edit and Cancel were two of those. We set the
OnEditCommand property equal to "dtlcustomers_Edit". We also created a
button with a CommandName of "edit". The combination of the two brings
us to the edit subroutine presented below. We use the ItemIndex
property to know which row is to be edited. We also created a Cancel
button (and set the OnCancelCommand property) to get us out of edit
mode if we want to abandon changes rather than going ahead with the
update of the row. Cancel is handled easily simply by setting the
EditItemIndex property to -1.

Public Sub dtlCustomers_Edit(sender as Object, e as

dtlCustomers.EditItemIndex = e.Item.ItemIndex

End Sub

Public Sub dtlCustomers_Cancel(sender as Object, e as

dtlCustomers.EditItemIndex = -1

End Sub

The last section of code presented is the dtlCustomers_Update
subroutine and is by far the longest section. As you may recall in the
aspx page EditItemTemplate we created TextBoxes to present data for
editing. The value in those TextBox controls are used to change the
data. The values of the textboxes are gathered and placed in string
variables in the code immediately below using the FindControl method.
We now have the data after any editing that took place. Immediately
below that is our update statement which is constructed using
parameters for the column values. Below that we Add parameters to the
sqlCommand object and set the parameter values to the string variables
holding our edited data. Following that we simply do the update and
then rebind the DataList control.

Public Sub dtlCustomers_Update(sender As Object, e As

Dim strCompanyName, strContactName, strContactTitle, strCustomerID
As String
Dim strAddress, strCity, strPostalCode, strCountry, strPhone,
strFax As String

strCompanyName = CType(e.Item.FindControl("txtCompanyName"),
strContactName = CType(e.Item.FindControl("txtContactName"),
strContactTitle = CType(e.Item.FindControl("txtContactTitle"),
strAddress = CType(e.Item.FindControl("txtAddress"), TextBox).Text
strCity = CType(e.Item.FindControl("txtCity"), TextBox).Text
strPostalCode = CType(e.Item.FindControl("txtPostalCode"),TextBox) .Text
strCountry = CType(e.Item.FindControl("txtCountry"),TextBox).Te xt
strPhone = CType(e.Item.FindControl("txtPhone"),TextBox).Text
strFax = CType(e.Item.FindControl("txtFax"),TextBox).Text
strCustomerID = CType(e.Item.FindControl("lblCustomerID"),

Dim strSQL As String
strSQL = "Update Customers " _
& "Set CompanyName = @CompanyName," _
& "ContactName = @ContactName," _
& "ContactTitle = @ContactTitle, " _
& "Address = @Address, " _
& "City = @City, " _
& "PostalCode = @PostalCode, " _
& "Country = @Country, " _
& "Phone = @Phone, " _
& "Fax = @Fax " _
& "WHERE CustomerID = @CustomerID"

Dim objConn As New SqlConnection(strConn)
Dim cmdSQL As New SqlCommand(strSQL, objConn)
cmdSQL.Parameters.Add(new SqlParameter("@CompanyName",
SqlDbType.NVarChar, 40))
cmdSQL.Parameters("@CompanyName").Value = strCompanyName
cmdSQL.Parameters.Add(new SqlParameter("@ContactName",
SqlDbType.NVarChar, 30))
cmdSQL.Parameters("@ContactName").Value = strContactName
cmdSQL.Parameters.Add(new SqlParameter("@ContactTitle",
SqlDbType.NVarChar, 30))
cmdSQL.Parameters("@ContactTitle").Value = strContactTitle
cmdSQL.Parameters.Add(new SqlParameter("@Address",
SqlDbType.NVarChar, 60))
cmdSQL.Parameters("@Address").Value = strAddress
cmdSQL.Parameters.Add(new SqlParameter("@City",
SqlDbType.NVarChar, 15))
cmdSQL.Parameters("@City").Value = strCity
cmdSQL.Parameters.Add(new SqlParameter("@PostalCode",
SqlDbType.NVarChar, 10))
cmdSQL.Parameters("@PostalCode").Value = strPostalCode
cmdSQL.Parameters.Add(new SqlParameter("@Country",
SqlDbType.NVarChar, 15))
cmdSQL.Parameters("@Country").Value = strCountry
cmdSQL.Parameters.Add(new SqlParameter("@Phone",
SqlDbType.NVarChar, 24))
cmdSQL.Parameters("@Phone").Value = strPhone
cmdSQL.Parameters.Add(new SqlParameter("@Fax", SqlDbType.NVarChar,
cmdSQL.Parameters("@Fax").Value = strFax
cmdSQL.Parameters.Add(new SqlParameter("@CustomerID",
SqlDbType.NChar, 5))
cmdSQL.Parameters("@CustomerID").Value = strCustomerID


dtlCustomers.EditItemIndex = -1

End Sub

End Class

Conclusion: You have seen a lot coding necessary to presenting a
DataList, placing it in edit mode, and then canceling or updating the
data after making changes. If you take the code one section at a time
and see what each section actually does, I believe you will find that
you can finely tune how your data is presented both for viewing and
for editing, and how to accomplish the update. Best of luck!

All times are GMT. The time now is 04:01 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.