0
       try
        { var sql = "SELECT ti_id_10888, ti_summary_10888, ti_description_10888, ti_estimation_10888, ti_priority_10888, ti_status_10888, ti_sprint_id_10888 FROM ti_ticket";
            var command = new SqlCeCommand(sql, connection);
            connection.Open();
            var reader = command.ExecuteReader();
            while (reader.Read())
            {
                var t = GetFromReader(reader);
                result.Add(t);
            }
        }

        return result;
    }

    private Ticket GetFromReader(SqlCeDataReader reader)
    {
        var t = new Ticket
        {
            Id = Convert.ToInt32(reader.GetValue(0)),
            Summary = reader.GetValue(1).ToString(),
            Description = reader.GetValue(2).ToString(),
            Estimation = Convert.ToInt32(reader.GetValue(3)),
            Priority = reader.GetValue(4).ToString(),
            Status = reader.GetValue(5).ToString(),
            Sprint = new SprintManager().GetById(Convert.ToInt32(reader.GetValue(6)))
        };

        return t;
    }

I have null values in the data, please help? I have to get all rows that are null or not null values

The error is "Object cannot be cast DBNULL to other types"

  • You are doing a `ToString()` which doesn't work: `null.ToString()` won't compile. Or you are casting with `Convert`. So you are acting as if nothing in the database is null. Heads up, `GetValue` returns `DBNull.Value` if you want to check for it. – Zer0 Mar 07 '20 at 21:48
  • In my table sprint_id is null, and cannot get value. It should show empty cell in the row – Abror Anvarov Mar 07 '20 at 21:51
  • https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldatareader.isdbnull?view=netframework-4.8 – Steve Mar 07 '20 at 21:54
  • The other thing to realize is that a SQL database can have any column nullable. You need to make sure that you handle things like integer columns that are nullable. You may bind using an ORM (object relational manager) like Dapper or Entity Framework easier to use than raw ADO.NET (where you need to do this all yourself) – Flydog57 Mar 07 '20 at 22:24
  • 1
    Does this answer your question? [Object cannot be cast from DBNull to other types](https://stackoverflow.com/questions/6098646/object-cannot-be-cast-from-dbnull-to-other-types) – Barns Mar 07 '20 at 23:03
  • A simple search would have revealed that this question has been already (many times, in fact) [Object cannot be cast from DBNull to other types. error--duplicate](https://stackoverflow.com/questions/40036145/object-cannot-be-cast-from-dbnull-to-other-types-error) :: [C#: Object cannot be cast from DbNull to other types](https://stackoverflow.com/questions/33740382/c-object-cannot-be-cast-from-dbnull-to-other-types) – Barns Mar 07 '20 at 23:08
  • Can you please specify yout Ticket class? – Mehdi Daustany Mar 08 '20 at 02:07

1 Answers1

-1

What type of data do you store if you don't mind adding it, plus please add Ticket object structure, what data should be nulled. if it is int, double... make sure to set it as "int?" to make it nullable value. here's reference to nullable value types: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types

another similar answer I found that might fit is: https://www.codeproject.com/Questions/686283/Object-cannot-be-cast-from-DBNull-to-other-types-e

EDIT:

if you have sprint_id null the "Convert.ToInt32(reader.GetValue(6))" will be "Convert.ToInt32(null)", which should cause error, so make sure it returns actual number before you try to convert it.