Concatenate fields for use in a DataTable/TypedListDAO

Posts   
 
    
Posts: 14
Joined: 14-Jan-2009
# Posted on: 21-Jan-2010 06:16:02   

Hello all,

I am trying to create a concatenated group of fields to use in a DropDownList as the DataTextField and discovered a way to deal with different data types. Previously, I was getting SqlClient exceptions because there are several different types, namely nvarchar, datetime and bigint.

The exception was System.Data.SqlClient.SqlException: Error converting data type nvarchar to bigint.

I solved it with the following, after trying both ExpressionToApply and defining a new EntityField:


            fields.DefineField(CustomerFields.CustomerId, 0);
            fields.DefineField(CustomerFields.Name, 1, "CustomerDataTextField");
            fields[1].ExpressionToApply = new DbFunctionCall("{0} + ', ' + CONVERT(nvarchar(255), {1})", new object[] { fields[1], fields[0] });

This yields Name + CustomerId in the CustomerDataTextField column in the DataTable.

Hope this is helpful, and of course alternate ways of doing the same are welcome.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 21-Jan-2010 10:26:12   

Thanks for the contribution.