• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

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>");                        
0
DancingFighterG
Asked:
DancingFighterG
1 Solution
 
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
 
sabeeshCommented:
drCal["Event_message"].ToString() why cant you try like this
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now