error nullable type although specified ? in prop

gal mor 141 Reputation points
2022-12-18T10:37:07.227+00:00

hello. so Im trying to get a query select statement from db, read it and attach it to an object , like so:

 using (OracleDataReader reader = cmd.ExecuteReader())  
                    {  
                        //for every row  
                        while (reader.Read())  
                        {  
                            //creating an instance of record class and filling with column's values  
                            RecordObject item = new RecordObject  
                            {  
                                Id = reader.GetInt32(0),  
                                System_Id = reader.GetString(1),  
                                System_Name = reader.GetString(2),  
                                Company_Id = reader.GetInt32(3),  
                                Company_Name = reader.GetString(4),  
                                Interface = reader.GetInt32(5),  
                                Service_Id = reader.GetInt32(6),  
                                Order_Company_Id = reader.GetString(7),  
                                Time_Stamp = reader.GetString(8),  
                                Zehut = reader.GetString(9),  
                                First_Name = reader.GetString(10),  
                                Last_Name = reader.GetString(11),  
                                Tik_Date = (string?)reader.GetString(12),  

                            };  
                            //adding object to list  
                            list.Add(item);  
                        }  
}  

everything works fine except to TIK_DATE(Last property) given error: 'Column contains NULL data'
in my class, I define it the property like: public string? Tik_Date{ get; set; }
what could be the problem??

edit
i added the following extension to support nulls of strings:

public static string SafeGetString(this OracleDataReader reader, int colIndex)  
        {  
            if (!reader.IsDBNull(colIndex))  
                return reader.GetString(colIndex);  
            return string.Empty;  
        }   

but dont know how to do the same with integer, any ideas?

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-12-19T03:04:53.553+00:00

    Hi @gal mor , Welcome to Q&A.

    For checking null values you should use a ternary method similar to the following.

    int columnValue = reader.IsDBNull(0) ? 0 : reader.GetInt32(0);  
    

    For the code you write:

     public static string SafeGetString(this OracleDataReader reader, int colIndex)  
             {  
                 if (!reader.IsDBNull(colIndex))  
                     return reader.GetString(colIndex);  
                 return string.Empty;  
             }   
    

    You could call it like this.

    string columnValue = reader.SafeGetInt32(0);  
    

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.