none
can someone help me to get out of the problem .... problem is when i insert the date in text it always inser null value at date time field of table code is given below RRS feed

  • Question

  •  
            if (dr.IsNull("Exit_Date"))
            {
                parameterValue = DBNull.Value;
                
            }
            else
            {
                parameterValue = ((DateTime)dr["Exit_Date"]).ToShortDateString();
            }
    Wednesday, March 13, 2013 9:26 AM

Answers

  • Your code implies that you use in your database for Dates not a Date type like DateTime, DateTime2 or SmallDate but one of the Character types (strings) and that the string column is set that it is allowed not to be not be used (Allow Null).

    If your code is a Date type, than simply avoid this kind of code in your datatable if Allow Null is true otherwise set it to an accaptable date for DateTime2 for instance 1/1/1, for DateTime 1/1/1753 and for SmallDate 1/1/1900 (the oldest possible dates).

    Be aware that this C# code says  

    parameterValue = ((DateTime)dr["Exit_Date"]).ToShortDateString();

    Set parameterValue to first to a short string which contains the date in a short date format and Cast it then to DateTime 

    It should throw an error because a String can be converted to a DateTime but not casted to a DateTime


    Success
    Cor




    Wednesday, March 13, 2013 3:15 PM

All replies

  • We need more information than that...

    What is dr (I'm assuming datareader)? What is the query you're using to get "Exit_Date"? How do you know it's not null? Have you debugged using a breakpoint on the if statement?


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Wednesday, March 13, 2013 1:56 PM
  • Your code implies that you use in your database for Dates not a Date type like DateTime, DateTime2 or SmallDate but one of the Character types (strings) and that the string column is set that it is allowed not to be not be used (Allow Null).

    If your code is a Date type, than simply avoid this kind of code in your datatable if Allow Null is true otherwise set it to an accaptable date for DateTime2 for instance 1/1/1, for DateTime 1/1/1753 and for SmallDate 1/1/1900 (the oldest possible dates).

    Be aware that this C# code says  

    parameterValue = ((DateTime)dr["Exit_Date"]).ToShortDateString();

    Set parameterValue to first to a short string which contains the date in a short date format and Cast it then to DateTime 

    It should throw an error because a String can be converted to a DateTime but not casted to a DateTime


    Success
    Cor




    Wednesday, March 13, 2013 3:15 PM