Solved

dealing with null value

Posted on 2012-03-29
6
335 Views
Last Modified: 2012-04-02
i'm querying sql based on rowid and have an issue when a field is null.

DateReceived1 = (SQLDR.GetValue(39).ToString()).Trim()  != "" ? Convert.ToDateTime(SQLDR.GetValue(39).ToString().Trim()).ToShortDateString() : "";

DateReceived1 has a datatype of datetime in the sql code and i have it as a string variable and the row i pull back has a null value so my error is:  

String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.Convert.ToDateTime(String value)
0
Comment
Question by:fwstealer
6 Comments
 
LVL 7

Expert Comment

by:Lalit Chandra
Comment Utility
yes this error tell you that you are attempting to typecast null to the string,so it's  causing an exception.
C # null is different that the sql server null. So, before typecasting it you have to make sure that there is not null value.
For doing that you can write

DateReceived1 = SQLDR.GetValue(39)  == DbNull.Value  ? Convert.ToDateTime(SQLDR.GetValue(39).ToString().Trim()).ToShortDateString() : "";

Hope this will solve your queries.
0
 

Author Comment

by:fwstealer
Comment Utility
i tried that and still same issue:  mscorlib String was not recognized as a valid DateTime. The value in the row is NULL
0
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 500 total points
Comment Utility
i think there is some error with the code that i have posted befor try with this

DateReceived1 = SQLDR.GetValue(39)  != DbNull.Value  ? Convert.ToDateTime(SQLDR.GetValue(39).ToString().Trim()).ToShortDateString() : "";

Hope this will solve your issue.
0
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.

 
LVL 21

Expert Comment

by:masterpass
Comment Utility
Try Parse Exact feature

string format = "dd/MM/yyyy";  // or MM/dd/yyyy
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime dt = DateTime.ParseExact(dateString, format, provider);

Open in new window

0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
Declare nullable type of DateTime as: (read more at Nullable Types)

The code below is suggestive for explanation only.
// Get value of that row somehow
		YourTypeHere? DateReceived1 = SQLDR.GetValue(39);

                // HasValue is of type bool. It is set to true when the variable contains a non-null value.
		if (DateReceived1.HasValue)
		{
			DateReceived1 = (SQLDR.GetValue(39).ToString()).Trim()  != "" ? Convert.ToDateTime(SQLDR.GetValue(39).ToString().Trim()).ToShortDateString() : "";
		}
                else
               {
                        // Do something if not received date from database
               }

Open in new window


The ability to assign null to value types is particularly useful when dealing with databases. And so the code above would help you to avoid error.
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
There is one more modification you can do in your code to avoid exception. Modify code as:

DateTime dateValue;


DateReceived1 = (Convert.ToString(SQLDR.GetValue(39)).Trim()  != "" ? DateTime.TryParse(Convert.ToString(SQLDR.GetValue(39)).Trim()), out dateValue) : "";

Open in new window


In the code above we have used Convert.ToString() instead of .ToString() because Convert.ToString() handles null value and returns String.Empty if null value found.

Second DateTime.TryParse(string, out DateTime result) returns datetime if string is not empty and if empty then it returns minvalue. Read more at: http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

14 Experts available now in Live!

Get 1:1 Help Now