locked
Null data issue RRS feed

  • Question

  • User-937748457 posted

    Hi all,

    i have a field in class as below

     public int remDays { get; set; }

    this get the number of days (days difference betweeen two dates)......start and end date may be null in database.

    when this return null then i m getting exception here(Cannot cast DBNull.Value to type 'System.Int32'. Please use a nullable type)

     var query = from o in dsResult.Tables[0].AsEnumerable()
                                select new ban
                                {
                                   
                                    remDays = o.Field<int>("remDays")


                                };
                    List<ban> lstDisplay = new List<ban>();
                    lstDisplay.AddRange(query);

    Monday, February 27, 2012 3:38 AM

All replies

  • User191633014 posted

    use :

    public int? remDays { get; set; }

    and get the value with:

    remDays.Value

     

    Monday, February 27, 2012 3:55 AM
  • User1283497924 posted

    Hi try this

    public int? remDays { get; set; }
    

    after tis assign value in below way

    remDays = (int)o.Field<int>("remDays") 
    
    Monday, February 27, 2012 4:03 AM
  • User1283497924 posted

    Hi try this

    public int? remDays { get; set; }
    

    after this assign value in below way

    remDays = (int)o.Field<int>("remDays") 
    
    Monday, February 27, 2012 4:03 AM
  • User-937748457 posted

    public int? remDays { get; set; }

    this give me this error

    Cannot cast DBNull.Value to type 'System.Int32'. Please use a nullable type...

    i am sure that i have null in table and it return me null(wehn i run query in sqlserver)

    Monday, February 27, 2012 4:14 AM