473,233 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 software developers and data experts.

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!
Nov 18 '05 #1
0 1740

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Hardy Wang | last post by:
Hi all; I have a DataList with ItemCreated event like below: private void myList_ItemCreated(object sender , DataListItemEventArgs e) { DataRowView myRowView; DataRow myRow; if (e.Item.DataItem...
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format....
by: Mark | last post by:
I have a datalist (see code below). Assume that the datalist is populated with 10 records of data. How do I programatically grab all the data in ALL the columns of the selected record? I've been...
by: Steve Lloyd | last post by:
Hi there, I have built a data list that contains dropdownlists that are populated depending on details of the item in the datalist by adding a datasource to the drop downs as part of the...
by: Adam Billmeier | last post by:
Problem: I am trying to use FindControl to grab a dropdownlist that is in the EditItemTemplate of a Datalist and then Bind it to a dataset. I also then need to put the correct values in all of...
by: Bharat | last post by:
Hi Folks, Suppose I have two link button on a page (say lnkBtn1 and lnkBtn2). On the click event of the lnkbtn1 I have to add a dynamically created control. And On the click event of the lnkBtn2 I...
by: Mirek Endys | last post by:
I have DataList as part of DataList item. DataList in DataList. The parent DataList working well including Edit command, that shows Edit template and correctly bind the data into edit template...
by: Jim in Arizona | last post by:
Is there an easier way to make a datalist aware of line breaks within text fields? I have a text box that users can type into. If they press the enter key a few times, VB sees this as a vbCrLf....
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I have am dynamically loading a web user control based on the click of a tab strip I load the default control for the first tab in the page load event after checking page is not postback. After...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.