Handling casting of null character

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>");                        
DancingFighterGAsked:
Who is Participating?
 
sabeeshCommented:
drCal["Event_message"].ToString() why cant you try like this
0
 
Raju SrivatsavayeSoftware EngineerCommented:
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
0
 
Elvio Lujan.Net Senior DeveloperCommented:
String _Event_message = Convert.ToString(drCal["Event_message"]);
0
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.

All Courses

From novice to tech pro — start learning today.