Re: Date format in repeater control

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi,

if you don't need to cast to a date first, you could simply have a
databinding expression in the aspx. For example:

<asp:TemplateColumn>
<ItemTemplate>
<%#
DataBinder.Eval(Container.DataItem,"database_field_name","{0:dd.MM.yyyy}")%>
</ItemTemplate>
</asp:TemplateColumn>

where "{0:dd.MM.yyyy}" indicates the formatting. Now, databinding
expressions should not blow up with null values (DBNull), they just produce
empty string.

However, if you need to cast to a date in code, you can nothing but check
for the DbNull value from the field with IsDbNull function (VB) or
Convert.IsDbNull (Framework/C#) which return boolean value indicating if the
value is DbNull. When there is a DbNull (per previous methods) you just
outout empty string (or other predefined string such as -No date-), you
cannot cast it to a Date. So it means that this check should happen before
trying casting.

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke






.



Relevant Pages

  • Re: Delphi 2007 for Win32 Update #1 is Available
    ... Object cannot be cast from DBNull to other types.Server Error in '/' ... Exception Details: System.InvalidCastException: Object cannot be cast from ... sourceControl, String eventArgument) +18 ...
    (borland.public.delphi.non-technical)
  • ASPNet Web - DBNull
    ... currently retriving data from the database. ... Cast from type 'DBNull' to type 'String' is not valid. ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Cant addnew in child grids
    ... your description, I understand that you're receiving an exception, which ... someone is trying to cast a DBNull value to other types like int. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Testing for DBNulls
    ... It tries to cast the XML Node ... value to a string and if the cast fails, ... table is DBNull. ... Fred Chateau ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Better comparing and converting of variant values...?
    ... >> You need to do a lot of checking before trying to compare or cast ... >> variants. ... >> resulting in an empty string value, an empty string should equal an ... A Delphi string type doesn't distinguish between '' and NULL. ...
    (comp.lang.pascal.delphi.misc)