473,396 Members | 2,068 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,396 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
BindTheData()
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()
sda.Fill(ds,"Customers")
dtlCustomers.DataSource = ds.Tables("Customers").DefaultView
dtlCustomers.DataBind()

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
DataListCommandEventArgs)

dtlCustomers.EditItemIndex = e.Item.ItemIndex
BindTheData()

End Sub

Public Sub dtlCustomers_Cancel(sender as Object, e as
DataListCommandEventArgs)

dtlCustomers.EditItemIndex = -1
BindTheData()

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
DataListCommandEventArgs)

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

strCompanyName = CType(e.Item.FindControl("txtCompanyName"),
TextBox).Text
strContactName = CType(e.Item.FindControl("txtContactName"),
TextBox).Text
strContactTitle = CType(e.Item.FindControl("txtContactTitle"),
TextBox).Text
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"),
Label).Text

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,
24))
cmdSQL.Parameters("@Fax").Value = strFax
cmdSQL.Parameters.Add(new SqlParameter("@CustomerID",
SqlDbType.NChar, 5))
cmdSQL.Parameters("@CustomerID").Value = strCustomerID

objConn.Open()
cmdSQL.ExecuteNonQuery()
objConn.Close()

dtlCustomers.EditItemIndex = -1
BindTheData()

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 1748

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

Similar topics

3
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...
0
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....
2
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...
1
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...
8
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...
10
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...
3
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...
0
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....
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.