Question

I'm retrieving data from a MySQL table and displaying it on a webpage using MySqlDataReader and .NET (C#), with no trouble retrieving simple text data, but I'm having trouble retrieving data from a boolean column, as the possible values are either "1" or "null" and GetBoolean() doesn't work in this instance.

I'm using the GetString() and GetDateTime() methods for the aforementioned columns (e.g. myReader.GetString("name") & myReader.GetDateTime("startDate")), is there a similar method for retrieving the boolean values when they are label as either "1" or "null"?

Was it helpful?

Solution

Use IsDBNull() in your code:

if(myReader.IsDBNull("BooleanColumnName") == true)
{
    // column contains a null value
    // handle null as you see fit
}
else
{
    var columnValue = myReader.GetBoolean("BooleanColumnName");
    if(columnValue == true)
    {
        // column value is "true"
    }
    else
    {
        // column value is "false"
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top