0
I created this read GET from the database, but when I read the id, it gives the following error "Cannot implicitly convert type 'int' to 'string'", it is telling me that led.id must be string?
it’s as if led.id is a string that can’t convert.
public static List<Led> GetStatus()
{
List<Led> lista = new List<Led>();
try
{
NpgsqlConnection conexao = Conexao.GetConexao();
string sql = "select * from led";
NpgsqlCommand cmd = new NpgsqlCommand(sql, conexao);
NpgsqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
int id = (int)dr["id"];
string status = (string)dr["status"];
Led led = new Led();
led.id = id;
led.status = status;
lista.Add(led);
}
}
catch (NpgsqlException erro)
{
Console.WriteLine("Erro de sql. " + erro.Message);
}
return lista;
}
First you look at the value that’s coming in
dr["id"]and if it is really a whole, try using another form of conversion:Convert.ToInt32(dr["id"]). I hope I’ve helped.– Maycon F. Castro
It says that you cannot convert Object to int, if I put int id = dr["id"]; and if you are like this Convert.Toint32(dr["id"]); it still gives the same error, the error code is CS0029.
– Andre Lino
Then in your variable
dr["id"]not an integer string is coming but an object, check your Datareader to see if it is returning the values as expected.– Maycon F. Castro
How is your table
led? You could describe it and also pass at least one line of data that you store in it as an example?– Murilo Portugal
led table has only one id - bigserial and status - charactere Varying in postgres, has a given id 1 and status 1 if I take the id and let it show in get only the status works the way the status is only that with id is not working that way there.
– Andre Lino