Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

dealing with null value

Posted on 2012-03-29
6
Medium Priority
?
343 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
[X]
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
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 2000 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
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!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

660 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