0

Any time I run this, it turns into an error.

Unable to cast object of type 'System.DBNull' to type 'System.Int32'.

This appears on line: DownUsersList.Add.

I'm trying to develop a dashboard that contains information from users, publications and the tops items they upload, but the result is an error that shows this message:

Unable to cast object of type 'System.DBNull' to type 'System.Int32'.

SqlDataReader reader;
command.Connection = connection;

command.CommandText = "SELECT PublicationDate, dbo.IntervaloFechas(PublicationDate) AS Publicated FROM Publicaciones " +
                         "WHERE PublicationDate BETWEEN @fromDate AND @toDate GROUP BY                 PublicationDate";
                    command.Parameters.Add("@fromDate", System.Data.SqlDbType.DateTime).Value = startDate;
                    command.Parameters.Add("@toDate", System.Data.SqlDbType.DateTime).Value = endDate;
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        TopUsersList.Add(new KeyValuePair<string, int>(reader[0].ToString(), (int)reader[1]));
                    }
                    reader.Close();

                    //Get down stock
                    command.CommandText = @"SELECT Usuario.Uname 'Usuario', 
                                LikesxPublicacion.Publicacion,Fotos,Videos,Comentarios,Likes,PublicationDate FROM Usuarios Usuario 
                                INNER JOIN Publicaciones LikesxPublicacion 
                                ON Usuario.UID = LikesxPublicacion.PID WHERE Likes <= 1000";
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        //Here is the Error : Unable to cast object of type 'System.DBNull' to type 'System.Int32'
//Error in line: DownUsersList.Add
                        DownUsersList.Add(new KeyValuePair<string, int>(reader[0].ToString(), (int)reader[1]));
                    }
                    reader.Close();
Dale K
  • 25,246
  • 15
  • 42
  • 71
  • 1
    Set a breakpoint and look at the value of `reader[1]` – Rufus L Feb 02 '23 at 03:24
  • One of the values coming back from your database is `NULL` which cannot be represented by an `int` hence `(int)reader[1]` fails. How you fix that depends on whether you expect to receive null from the database, if not, debug your data. If you do, you might need to use a `int?` i.e. a nullable int to contain the value, which will require that you check whether the value is null before casting it (all easy stuff to find online). – Dale K Feb 02 '23 at 03:25
  • If your data is nullable then you need to write your code with that in mind. Why do you think you can blindly cast the data from the database as `int` when some rows may contain `NULL`? What do you expect to happen in that case? If the data should always have a value then don't make the column nullable in the database and make sure you save actual numbers. If the data should be nullable then allow for that in your code. Your data reader has a method to check for `NULL` so use it. It's up to you to decide what to do when a `NULL` is encountered. – jmcilhinney Feb 02 '23 at 03:27
  • I tried to fix it using Integer value but I couldn't find a solution =( – Pedro Nuñez Feb 03 '23 at 13:49

0 Answers0