How to read a 'Boolean' column using NPGSQL?

Asked

Viewed 30 times

-1

I have the following code at the event Validated textbox:

NpgsqlConnection conn = new NpgsqlConnection(
    "Server=127.0.0.1; port=5432;User; Id=postgres; Password=572600;Database=Sistema");

conn.Open();
NpgsqlCommand cmd = new NpgsqlCommand(@"
        SELECT * FROM usuario 
        WHERE codigo_usuario=" + textBox1.Text, conn);
NpgsqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
    textBox2.Text = dr["nome"].ToString();
    //   checkBox1.Checked = dr["inativo"];
    textBox3.Text = dr["senha"].ToString();
    radioButton1.Text = dr["nivel"].ToString();
           
    conn.Close();
}

The "inactive" field in the database is Boolean and I would like you to return the Checkbox according to true or false that is in the database record.

  • What probably happens is that the value of dr["inactive"] is not a boolean variable, although this field in the database is. Try the following: see, by debugging, the value of dr["inactive"]. If the value is, for example, "True", do: checkBox1.Checked = String.Equals(dr["inactive"]. Tostring(), "True");

  • João Victor Sierra that’s what I really needed thanks for the help I’m beginner in C# In case I need to convert a String field from the database, if true the radiobutton1 is checked, if it is falseu radiobutton2 is checked , how do ? radioButton1.Text = dr["nivel"]. Tostring(); that would be it ?

  • The answer solved your problem?

1 answer

1

If the field in PG really is a boolean, use Convert.ToBoolean

checkBox1.Checked = Convert.ToBoolean(dr["inativo"]);

Browser other questions tagged

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