I have a results page that displays a single record.
Question:
If there isn't a phone extension, how do I exclude (suppress) the phone extension itself and "ext." label from the returned data?
If there is an extension I want:
• Chris Compton 919-754-6000 ext. 6512 chris@notmymail.net
else
• Chris Compton 919-754-6512 chris@notmymail.net
If I don't get an answer this week I'll just code it into a t-sql stored proc like this:
SELECT ..., Phone, IsNull('ext. '+nullif([PhoneExt],''), ''), Email ...
(but I'd rather keep that logic out of the database)
Here's what I have (shortened) that works, but the label "ext." always shows:
try { pk = Convert.ToInt32(Request.QueryString["id"]); }
catch (Exception) { pk = 0; }
var query = (
from c in context.tEmployees
where (c.pkEmployee == pk)
orderby c.NameLast, c.NameFirst, c.NameMiddle, c.Agency
select new
{ Name = c.First + " " + c.Last,
Telephone = c.Phone ,
TelephoneExt = " ext. " + c.PhoneExt,
Email = c.Email }
);
if (query.Count() == 1)
{ ResultsRepeater.DataSource = query;
ResultsRepeater.DataBind();
}
else
{ Response.Write("I'm sorry we could not find that employee, please try the search page again."); }
Also, if there's a better way to handle the variable "pk" I'm open to suggestions.
NOTE: that the t-sql and the code both work. If I've introduced an error shortening the code to post the question my apologies.
Edit 6/7/12:
ALL THREE solutions below are great and helped me!
I saw the first answer
in time to deploy the working code (in QA) just before the end of my day yesterday.
I actually changed to the second one this morning (I try to avoid using "not" in an if clause).
I made a couple more improvements after reading number three (and the comments).
Thanks again!
-Chris C. 5/7/12 1 PM EDT