Handling casting of null character

Posted on 2007-08-01
Last Modified: 2008-01-09
Hello, I'm trying to cast information from a database into type string but I get the error that it cannot cast System.DBNull character. So pretty much it can't cast a null character. How can fix this or get around this so that if the info in the database is blank it won't throw this error. Here is my code:

String _Event_message = (String)drCal["Event_message"];
                    sbEvent.Append("<tr><td valign=top width=100>" + Convert.ToDateTime(_EventDate).ToString("MMMM dd yyyy") + "</td><td valign=top width=100>" + _Event_time + "</td><td valign=top width=250><a href=\"" + _Event_message + "\" target=\"blank\">"  + _Event_name + "</td></tr>");                        
Question by:DancingFighterG
    LVL 9

    Expert Comment

    If you are using SQL SERVER , then in your query use ISNULL to replace empty string with null value..something like this.
    ISNULL(YourFieldName,'') as YourFieldName
    LVL 17

    Expert Comment

    String _Event_message = Convert.ToString(drCal["Event_message"]);
    LVL 8

    Accepted Solution

    drCal["Event_message"].ToString() why cant you try like this

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now