IfEmpty error when empty

kyle shapiro asked on May 4, 2015 22:02

Hi everyone, I have the following line of code within one of my transformations:

<%# IfEmpty(Eval("OfficeFax"),"",(string.Format("{0:Fax: (###) ###-####}", Int64.Parse(Eval("OfficeFax").ToString())))) %>

here it is again expanded for easier reading:

<%#
    IfEmpty(
        Eval("OfficeFax"),
        "",
        (string.Format(
            "{0:Fax: (###) ###-####}",
            Int64.Parse(
                Eval("OfficeFax").ToString()
            )
        ))
    )
%>

Is there a mistake in my logic? This works great for formatting 10 digit fax or phone numbers, but when I leave the field empty, I get a databinding error.

[CMSAbstractTransformation.DataBind]: Input string was not in a correct format. 

I think the IfEmpty method doesn't like all the stuff I'm throwing into it. Is there a better way of writing the same thing? The field is stored as text, not a number. Thanks.

Correct Answer

Jim Spillane answered on May 5, 2015 17:17

That is strange. IfEmpty() should return the second parameter if the first is null.

Here is one without the IfEmpty()... <%# (Eval("OfficePhone") != null && Eval("OfficePhone").ToString().Length > 0) ? Convert.ToInt64(Eval("OfficePhone")).ToString("Fax: (###) ###-####") : "" %>

0 votesVote for this answer Unmark Correct answer

Recent Answers


Jim Spillane answered on May 4, 2015 22:44

try escaping the colon in the format...

{0:Fax\: (###) ###-####}

0 votesVote for this answer Mark as a Correct answer

kyle shapiro answered on May 4, 2015 22:48

Thank you Jim, but unfortunately escaping the colon didn't change the databind error.

0 votesVote for this answer Mark as a Correct answer

Jim Spillane answered on May 4, 2015 23:24

Sorry about that. Give this a try...

<%# IfEmpty(Eval("OfficeFax"), "", Convert.ToInt64(Eval("OfficeFax")).ToString("Fax: (###) ###-####")) %>

0 votesVote for this answer Mark as a Correct answer

kyle shapiro answered on May 5, 2015 16:21

Hey Jim, for that code, it gives a different error.

[CMSAbstractTransformation.DataBind]: Object cannot be cast from DBNull to other types.

I'll play with it some today and post back here if I find the solution. I feel like the problem is related to IfEmpty getting confused about all that stuff in the 3rd parameter. It seems as though IfEmpty is returning "no this is not empty" due to something in the syntax for that 3rd parameter.

0 votesVote for this answer Mark as a Correct answer

kyle shapiro answered on May 5, 2015 18:18

Thanks again Jim. Your solution works! I fully understand the problem as well. Int64.Parse() or Convert.ToInt64() will not take empty strings as a parameter. This would be fine if the IfEmpty() function didn't look at the 3rd parameter in cases where the first parameter has already been found to be empty. However this is not the case, and it tries to do Int64.Parse("").

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.