asp.net Tips1---merge multiple field values

Source: Internet
Author: User
asp.net Web controls that typically support data binding, such as dorpdownlist controls, CheckBoxList controls, and so on, all contain five properties: DataSource, Datamember,datatextfield, DataValueField and Datatextformatstring. DataSource is used to get the data source, that is, to get the collection containing the data; the other four properties are used to get a field value from the data source. Typically, however, you want to bind multiple field values from these data sources together and assign them to one of these properties, which cannot be implemented directly. Here are two ways to implement this functionality.

The above limitation is that you cannot bind multiple field values together and assign them to one of these four properties, but the SQL statement has no such limitation at all. So, our first approach is to use the SQL statement to bind two or more field values in the source table, using the AS clause to pass to another field, which implements a field that contains more than one field value. Since a field now contains the values of more than one field, the four properties that we assign to the Web control do not have to be considered more. We can refer to the following statement to see the beauty of this approach:

strcmd= "Select Employeeid,lastname +", ' +firstname as ' employeename ' from Employees ';


This method is very concise and one of the most direct methods, but there is a problem of inefficiency, and it is possible to produce duplicate data. Here's another way we can populate a dataset with a source table, the dataset's data is then placed in a DataTable, and then a new column is created for the table, consisting of two or more fields in the source table, so that we also implement the binding of multiple field values. The following statement illustrates the implementation of this method:

Dt. Columns.Add ("EmployeeName", Typeod (String), "lastname+ ', ' +firstname");


The disadvantage of the latter approach is the overhead of creating a new column. However, this method is well organized and is not likely to produce duplicate data, which is recommended.

To illustrate both methods, the following code implements the two methods together:

Place user code here to initialize page

String Strconn,strcmd;

Strconn= "Database=northwind; Server=localhost; Uid=sa; pwd=99133009; ";

If you need to bind both FirstName and LastName and use them separately, the following statement produces duplicate data.

strcmd= "Select Employeeid,firstname,lastname,lastname +", ' +firstname as ' employeename ' from Employees ';

SqlDataAdapter Dsda=new SqlDataAdapter (Strcmd,strconn);

DataSet ds=new DataSet ();

Dsda. Fill (ds, "EmployeeList");

DataTable Dt=ds. tables["EmployeeList"];



The first of these methods

DDL1 is a DropDownList control

DDL1. Datasource=dt. DefaultView;

DDL1. Datatextfield= "EmployeeName";

DDL1. Datavaluefield= "EmployeeID";

DDL1. DataBind ();

/* The second method

Dt. Columns.Add ("EmployeeName", Typeod (String), "lastname+ ', ' +firstname");

DDL1. Datatextfield= "EmployeeName";

DDL1. DataBind ();

* /





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.