 |
| ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

September 24th, 2007, 02:48 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|| is the string concatenation operator in PostgreSQL (probably the equivalent for SQL Server is +). I've placed the concatenation in parenthesis just to make it look pretty.
Finally, "as Combined" basically means something like "name the field as 'Combined' in the resulting set". So, if you'd execute that, you'd see something like this:
Name | Email | Combined
--------------------------------------
foo | [email protected] | foo [email protected]
bar | [email protected] | bar [email protected]
...
In PostgreSQL if I don't do the "as Combined", the resulting field name would be "?column?", which I must say, isn't that pretty.
|
|

September 24th, 2007, 03:06 AM
|
|
Authorized User
|
|
Join Date: Aug 2007
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you for replying.
if i want to display the combined one, then whe the user have selected, i'll take this value, then connect to the database again to retrieve the email address?
|
|

September 24th, 2007, 03:12 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Eh... Not exactly. :)
Just do what you have already done (combined the name and email address) , but in addition, select those two fields separately, so you can access them.
Now, I think you have something like this:
SELECT PK, Name + ' ' + Email From MyTable
You change it to:
SELECT PK, Name + ' ' + Email, Name, Email From MyTable
After that, I think you have what you wanted: you have name & email fields combined for display in DropDownList and you can access name and email separately if you need.
|
|

September 24th, 2007, 03:37 AM
|
|
Authorized User
|
|
Join Date: Aug 2007
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi! Thanks again for replying.
I have another concern uprising from this statement.
SELECT PK, Name + ' ' + Email, Name, Email From MyTable
If I use this Name+" "+Email, won't the database think that I'm extracting from the field "Name Email"? Then won't this give me an error saying that they cannot find this field?
|
|

September 24th, 2007, 03:45 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Technically speaking the database will think you want to select a field, which is a result of concatenating fields Name AND Email. So, the answer is no, it won't think that you are selecting a field "Name Email", which doesn't exist.
Have you considered trying it out, just to see what it produces? :)
|
|

September 24th, 2007, 03:48 AM
|
|
Authorized User
|
|
Join Date: Aug 2007
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks again for your fast reply. My concern happened.
The database told me that there was an error in retrieving "Name Email".
|
|

September 24th, 2007, 04:02 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hmm... That's weird. It works with PostgreSQL though and the syntax shouldn't be much different with SQL Server. Are you sure you got the column names right in your SQL query?
I actually did this in SQL Server Express and it worked:
CREATE TABLE Customers (
Name VARCHAR(255),
Email VARCHAR(255)
);
INSERT INTO Customers (Name, Email) VALUES ('foo', ' [email protected]');
And finally, SELECT Name, Email, (Name + ' ' + Email) AS Combined FROM Customers, which resulted in:
Name Email Combined
----------------------------------
foo [email protected] foo [email protected]
And make sure that you use single quote, not double quote. :) Name + " " + Email won't work, but Name + ' ' + Email works.
|
|

September 24th, 2007, 04:25 AM
|
|
Authorized User
|
|
Join Date: Aug 2007
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks! I'll try them now.
|
|

September 24th, 2007, 04:36 AM
|
|
Authorized User
|
|
Join Date: Aug 2007
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hey Miko2!
Thank you! It works! Yup, I used the double quotes & typed the Combined in caps, that's why it didn't work. However, when I choose the say particularly Remy| [email protected], how do I reference to " [email protected]" if I don't refer back to the DB again?
Thank you so much!
|
|

September 24th, 2007, 05:19 AM
|
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Eehm... I think I didn't quite think that far about this. As you select the "Remy| [email protected]" item from the DropDownList and submit the form, of course you need to access the database again to retrieve the email address for that particular entry based on the DropDownList's SelectedValue property.
You probably could read the email address from the datasource, but I'm not sure how or even if it's the preferred way of doing it.
|
|
 |