Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Generic list and null datefields

Posted on 2009-04-16
15
Medium Priority
?
275 Views
Last Modified: 2013-12-17
Greetings all

I have an object , populated via a datareader. All is fine until I hit hit a record where the date is null.

Here I am getting "Error      10      Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'System.DateTime'"

What can I do to resolve this?


In advance, thanks!!!

     allanmark
public class Couple
    {
        private int id;
        private string name;
        private DateTime wedAnniv;
       ........
       .........
 
    Couple theCouple = new Couple();
   
   ......................
   ......................
    sqlConn.Open();
    SqlDataReader drCouple = sqlGetCouple.ExecuteReader();
    
    while (drCouple.Read())
    {
                    theCouple.Id = drCouple.GetInt32(0);
                    theCouple.Name = drCouple.GetString(1);
                  // Line below is the problem!!
                    theCouple.WedAnniv = drCouple.IsDBNull(3) ? null : drCouple.GetDateTime(3);

Open in new window

0
Comment
Question by:allanmark
  • 6
  • 5
  • 4
15 Comments
 
LVL 9

Accepted Solution

by:
Sreedhar Vengala earned 300 total points
ID: 24156204
What if you change theCouple.WedAnniv to Nullable DateTime
0
 
LVL 39

Assisted Solution

by:abel
abel earned 700 total points
ID: 24156206
> theCouple.WedAnniv = drCouple.IsDBNull(3) ? null : drCouple.GetDateTime(3);

I think the error is not coming from the null in your statement, because that should give a different error. But still, you are right in that null cannot be assigned to DateTime because it is not nullable. To change the DateTime to being nullable, change it like this:

   private DateTime? wedAnniv;

which will make the type nullable. Now you can do this:

   wedAnniv = null;

-- Abel --
0
 

Author Comment

by:allanmark
ID: 24156210
You mean, like this:

theCouple.WedAnniv = drCouple.IsDBNull(3) ? Convert.ToDateTime(DBNull.Value) l : drCouple.GetDateTime(3);


If that is so, could I not apply a similiar logic to all other field type?
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24156225
just this will do :
private DateTime? wedAnniv;
0
 
LVL 39

Expert Comment

by:abel
ID: 24156341
> You mean, like this:

no, I mean you leave that statement intact and make the wedAnniv nullable. See the copy of my code in the last comment of sree_ven (or just see my other comment ;)
0
 

Author Comment

by:allanmark
ID: 24156407
Sorry ... having a really "I-Feel-Stupid" day.


Make the changes (as I understood them) and ran - got an exception error - "ex = {"Data is Null. This method or property cannot be called on Null values."}"

What have I missed?


 public class Couple
    {
        private int id;
        private DateTime? wedAnniv;
 
   public DateTime? WedAnniv
        {
            get { return wedAnniv; }
            set { wedAnniv = value; }
        } 
}
 
theCouple.WedAnniv = drCouple.GetDateTime(3);

Open in new window

0
 

Author Comment

by:allanmark
ID: 24156449
Apologies ... something weird going on  - somehow missed abel's most recent post.


Put this line back in:
 theCouple.WedAnniv = drCouple.IsDBNull(3) ? null : drCouple.GetDateTime(3);

Compiler gave this error: "Error      10      Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'      "


0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24156463
public class Couple
    {
        private int id;
        public DateTime? wedAnniv;

        public DateTime? WedAnniv
        {
            get { return wedAnniv; }
            set { wedAnniv = value; }
        }
    }
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24156471
or Just:
 public class Couple
    {
        public int id { get; set; }
        public DateTime? wedAnniv { get; set; }
    }

Open in new window

0
 
LVL 39

Assisted Solution

by:abel
abel earned 700 total points
ID: 24156908
> Apologies ... something weird going on  - somehow missed abel's most recent post.

np, it is a weird day, I have weird things going on too, full moon perhaps? lol

On topic: you are getting a more sensible response from the compiler now. I don't know why it is complaining (it shouldn't need to, but I see the same behavior on my system), but a fix is as follows:

 theCouple.WedAnniv = drCouple.IsDBNull(3) ? (DateTime?) null : drCouple.GetDateTime(3);

which makes the implicit conversion explicit.
0
 

Author Comment

by:allanmark
ID: 24157577
Cool!

Am I correct in saying that the same can be applied to types string and int?

0
 

Author Comment

by:allanmark
ID: 24157626
Silly me. Should just have tried i, instead of just asking the question ....

Anyway, tried it and got this: "The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'      "

 public class Couple
    {
        ...................
        private DateTime? wedAnniv;
        private string? comments;   <=============== ERROR

Open in new window

0
 
LVL 39

Assisted Solution

by:abel
abel earned 700 total points
ID: 24157807
> Am I correct in saying that the same can be applied to types string and int?

no, not that simple. String is a reference type (i.e., you can make objects of type string). Int, Long, DateTime, Double are all value types, and you cannot make objects of them. Reference types can be null (a string can be null), but value types cannot.

At some point Microsoft decided that such behavior was not handy and created a value type that can also be null: the Nullable type. To define a nullable type, use the "?" mark.

These will work:
private int? myInt = null;
private DateTime? myTime = null;
private string myString = null;        // no question mark here!
private double? myDbl = 0.0;           // also nullable
private Couple mySpecialCouple = null  // your class type is a reference type (all are)

Open in new window

0
 

Author Closing Comment

by:allanmark
ID: 31571068
A most educational experience! Many thanks!!
0
 
LVL 39

Expert Comment

by:abel
ID: 24159559
> A most educational experience! Many thanks!!

you're welcome, glad we could be of some help
0

Featured Post

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!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

580 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