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();