Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
C#
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
Posted
Updated 7-Jun-12 7:09am
v2

What about:
C#
TelephoneExt = string.IsNullOrEmpty(c.PhoneExt) ? " ext. " + c.PhoneExt : string.Empty;
 
Share this answer
 
Comments
AspDotNetDev 6-Jun-12 19:24pm    
You have it reversed and Clifford beat you. I recommend you delete your answer.
JChrisCompton 7-Jun-12 13:12pm    
That's okay, I realized and swapped them - thanks again!
Clifford has the correct solution for the IsNull issue, but I thought I'd add an alternative way to parse the integer:
C#
if (!Int32.TryParse(Request.QueryString["id"], out pk))
{
    // The TryParse will default it to 0 on failure,
    // but I put this here in case you want to default to something else.
    pk = 0;
}
 
Share this answer
 
Comments
AspDotNetDev 6-Jun-12 19:27pm    
I'd also add that you don't need to do the query if the parse fails (which would save you a database hit), and that you should not be using Response.Write (use a label or something like that instead).
JChrisCompton 7-Jun-12 12:56pm    
Yes + yes
I did NOT think about the db hit - I knew better though :-)
I did have a clean up the code period (Response.Write is left over from before the controls were there).
Thanks!
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 = c.PhoneExt != null ? " ext. " + c.PhoneExt : string.Empty,
        Email = c.Email }
    );


or

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 = ! string.IsNullOrEmpty(c.PhoneExt) ? " ext. " + .PhoneExt : string.Empty,
        Email = c.Email }
    );
 
Share this answer
 
Comments
Wonde Tadesse 6-Jun-12 20:21pm    
Instead of " ext. " + c.PhoneExt, please do string.Concat("ext.",c.PhoneExt). :)
AspDotNetDev 6-Jun-12 20:36pm    
Why? The compiler will do that for you, and for only 2 strings there is no benefit.
Wonde Tadesse 6-Jun-12 22:16pm    
We know that the compiler will do that. However Good programming skill starts with smallest. Don't you think?
AspDotNetDev 6-Jun-12 22:28pm    
With smallest what?
Wonde Tadesse 6-Jun-12 22:30pm    
Things to do with smallest change of " ext. " + c.PhoneExt to string.Concat("ext.",c.PhoneExt).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900