Conversion from type 'DBNull' to type 'String' is not valid

  • Thread starter Thread starter Cirene
  • Start date Start date
C

Cirene

I'm setting a databound label's backcolor and forecolor like this...

<asp:Label ID="lblSampleColorScheme" runat="server"

BackColor='<%#
System.Drawing.Color.FromName(Eval("AttachmentEntityBackColor")) %>'
Font-Bold="True"

ForeColor='<%#
System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")) %>'
Text="CURRENT COLOR SCHEME"></asp:Label>


The problem is when the value is NULL. I get the above error. How can I
say, if it's null in the db then use "#ffffff"?

Thanks!
 
This doesn't seem to work...

<asp:Label ID="lblSampleColorScheme" runat="server"

BackColor='<%# iif(eval("AttachmentEntityBackColor").equals(DBNull.value),
"#ffffff", System.Drawing.Color.FromName(Eval("AttachmentEntityBackColor")))
%>' Font-Bold="True"

ForeColor='<%# iif(eval("AttachmentEntityForeColor").equals(DBNull.value),
"#000000", System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")))
%>' Text="CURRENT COLOR SCHEME"></asp:Label>
 
u can't use IIF()
System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")) will
still be executed, resulting in error

(IIF is not equaivalent to C's '?' command)

u need to write another function

public function BackColor(AColor as Object) as String
if typeof AColor is DBNull then
return "#FFFFFF"
else
return System.Drawing.Color.FromName(AColor)
end if
end function

BackColor='<%#BackColor(eval("AttachmentEntityBackColor"))%>'
 
Back
Top