Search code examples
c#booleanoledbdatareader

Displaying bool value as Yes or No


I am quering a database and assigning the values to an object which I serialize and display in a report.

Thing is the bool variables are displayed in the report as true or false. How can get the values to be displayed as "Yes" or "No".

This is my class

public class ProductReportView
{
    public int Count { get; set; }
    public string ProductCode { get; set; }
    public string ProductTitle { get; set; }
    public string Producer { get; set; }

    public bool VideoOnDemand { get; set; }
    public bool PreviewScreen { get; set; }
    public bool QualityCheck { get; set; }
    public bool Archive { get; set; }
}

This is how I assign the values

OleDbDataReader dbreader = cmd.ExecuteReader();

while (dbreader.Read())
{
     Console.WriteLine("Record " + totalCount++);
     ProductReportView rep = new ProductReportView();
     rep.Count = ++totalCount;
     rep.ProductCode = (string)dbreader["CODE"];
     rep.ProductTitle = (string)dbreader["TITLE"];
     rep.Producer = (string)dbreader["PRODUCER"];
     rep.VideoOnDemand = (bool)dbreader["VideoOnDemand"];
     rep.PreviewScreen = (bool)dbreader["PreviewLibraryChecked"];
     rep.QualityCheck = (bool)dbreader["QualityCheck"];
     rep.Archive = (bool)dbreader["Archive"];
     lst.Add(rep);
}

The values are based on checkboxes that is checked and unchecked(VideoOnDemand, PreviewScreen QualityCheck, Archive)


Solution

  • Use ternary operator while storing your value in the object

    rep.VideoOnDemand = (bool)dbreader["VideoOnDemand"] ? "Yes" : "No";  
    

    And make VideoOnDemand as string

    public string VideoOnDemand { get; set; }
    

    use the same approach for rest of the variables for which you need YES/ NO