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

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!
 
C

Cirene

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>
 
N

nick chan

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"))%>'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top