Label Date format

I'm pulling data from an aspx sqldatasource to a label.  The date populates as 2015-10-28. I used a function in the code behind to show as a 10-28-2015 but I get an error when there is a null value or when the label is blank.

Public Function BundleDate1Format(input As DateTime) As Object
    Return [String].Format("{0:MM/dd/yyyy}", Input)
End Function

Open in new window


My aspx snippet

<asp:FormView ID="formview4" runat="server" DataSourceID="sqlpbBlackMenSS1">
                <ItemTemplate>
                 <dx:ASPxLabel ID="lblBLAMSSdate" runat="server" Font-Size="Small" ForeColor="Red" Text='<%# BundleDate1Format(Eval("BundleDate1"))%>'>
                </dx:ASPxLabel></ItemTemplate></asp:FormView>

Open in new window

Southern_GentlemanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mr KnackeredCommented:
Cant you do:

if (string.IsNullOrEmpty(Input))
{
     return string.empty;
}
else
{
    return string.format("{0:MM/dd/yyyy}", Input)
}

example is C# but you get the idea.
0
Southern_GentlemanAuthor Commented:
Thank you but now for some reason i'm coming up with

Conversion from string "" to type 'Date' is not valid.

My select statement wouldn't have an issue with it would it:  
SELECT BundleID1, ISNULL(CAST(TRY_CONVERT(date,BundleDate1,101) AS VARCHAR),'') AS BundleDate1 FROM PreBuyBundle WHERE (ID = 17)

Open in new window

0
Ryan ChongCommented:
>>Conversion from string "" to type 'Date' is not valid.
since your function's parameter only accepting DateTime, you will receive such error when you passing an empty string into the function as an empty string is not a valid datetime value.

hence, what if you try:

Public Function BundleDate1Format(input As Object) As Object
        If (string.IsNullOrEmpty(Input)) Then
            Return string.empty
        Else
            Return [String].Format("{0:MM/dd/yyyy}", Input)
        End If
    End Function

Open in new window

?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Southern_GentlemanAuthor Commented:
Thanks Ryan, I was able to get rid of the Conversion error for nulls but the label is still displaying as
2015-10-28  in stead of the what the code behind is requesting  MM/dd/yyyy
0
ArkCommented:
You can use nullable DateTime?:
Public Function BundleDate1Format(input As DateTime?) As String
    If input.HasValue Then
       Return input.Value.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
    Else
        Return "Not set"
    End If
End Function

Open in new window

Note that using "/" date format separator brings you to system date format, so use CultureInfo.InvariantCulture to have format you specify or use literals  mm'/'dd'/'yyyy

See The "/" custom format specifier
0
Southern_GentlemanAuthor Commented:
Hey Ark, so I tried this CultureInfo.InvariantCulture and came up with the "Specified Cast is Not Valid"

at this line:  <dx:ASPxLabel ID="lblBLAMSSdate" runat="server" Font-Size="Small" ForeColor="Red" Text='<%# BundleDate1Format(Eval(“BundleDate1”))%>'>
0
ArkCommented:
Aha. Sorry, I didn't realize you convert date to string in your select statement. Either select date (or null) assuming BundleID1 is SQL DateTime column :
SELECT BundleID1 AS BundleDate1 FROM PreBuyBundle WHERE (ID = 17)

Open in new window

or
Public Function BundleDate1Format(input As String) As String
    Dim dt As Date
    If Date.TryParse(input, dt) Then
        Return dt.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
    Else
        Return "Not set"
    End If
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.