Unable to convert int to string C#

Asked

Viewed 221 times

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.

  • 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.

  • 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.

  • 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?

  • 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.

1 answer

1

Try it this way:

public static List<Led> GetStatus()
{
    try
    {
        using (conn = new NpgsqlConnection(Conexao.GetConexao()))
        {
            conn.Open();
            string sql = "select * from led";
            using (NpgsqlCommand cmd = new NpgsqlCommand(sql, conn))
            {
                using (NpgsqlDataReader reader = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        int id = Convert.ToInt32(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;
}

I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.