Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

dealing with null value

Posted on 2012-03-29
6
338 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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