Solved

dealing with null value

Posted on 2012-03-29
6
336 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
ID: 37783793
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
ID: 37783889
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
ID: 37783916
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 21

Expert Comment

by:masterpass
ID: 37784518
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
ID: 37785470
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
ID: 37785503
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

17 Experts available now in Live!

Get 1:1 Help Now