Solved

dealing with null value

Posted on 2012-03-29
6
339 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web site error 3 50
Please explain the parts of these 2 LINQ expressions 3 79
ASP.NET MVC -Add authentication 2 34
Add a Condition in ASP Repeater 6 44
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

680 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