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)

有帮助吗?

解决方案 5

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

其他提示

You don't say how you are 'reporting'...

Does this help?

   Control.Text = rep.VideoOnDemand ? "Yes" : "No";

Making changes during storing the value in object is really a bad idea. So do it at the C# level in the grid

Control.Text = rep.VideoOnDemand ? "Yes" : "No";

You can do it this in your Sql Query also.

Eg.

Select

case VideoOnDemand when 1 then 'YES' else 'NO' end as 'VideoOnDemand'

from tblxyz

My approach, with 4 simple properties, gives reusability and cleaning the code:

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; }

    private string toYesNo(bool b)
    {
        return b ? "Yes" : "No";
    }

    public string VideoOnDemandString
    {
        get { return this.toYesNo(this.VideoOnDemand); }
    }

    public string PreviewScreenString
    {
        get { return this.toYesNo(this.PreviewScreen); }
    }

    public string QualityCheckString
    {
        get { return this.toYesNo(this.QualityCheck); }
    }

    public string ArchiveString
    {
        get { return this.toYesNo(this.Archive); }
    }
}

This code can be reused in all your application without repetitions of "yes", "no", "yes", "no", etc.

Final suggestion: bools should be stored in bool properties, strings should be stored in string properties.

Don't persist bool values converted to string: don't have any sense.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top