Format an ADO.NET boolean as "0" or "1"

I am converting answers for a survey to a specified format. I need to handle boolean questions, which can be skipped. Here is the rubric

ADO.NET Value     Output
DBNull                  ""
true                     "1"
false                    "0"

Can someone come up with something simpler than this:

[code]
        const string missing = "";
        private static string GetBoolOutputFromDataRowView(DataRowView drv, string columnName)
        {
            object obj = drv[columnName];
            if (obj == System.DBNull.Value)
                return missing;
            else
                return ((bool)obj == true) ? "1" : "0";
        }
[/code]
LVL 11
anyoneisAsked:
Who is Participating?
 
NightmanConnect With a Mentor CTOCommented:
That looks good enough to me. Do you have any specific concerns about this approach?
0
 
NightmanConnect With a Mentor CTOCommented:
A simpler approach could be to do this in SQL, and not have to handle it in ADO at all.

e.g.
SELECT CASE  WHEN MyColumn IS NULL THEN 'Missing' WHEN MyColumn=1 THEN '1' ELSE '0' END result FROM MyTable
0
 
anyoneisAuthor Commented:
Thanks, Nightman. I have the feeling I am overlooking some formatting idiom or apparatus, so I thought I would ask.

Also, I don't have any control over the SQL, I am getting a dataset and datatable from the vendors library, so changing the SQL is out of the question.

David
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
NightmanCTOCommented:
The only thing that I would do would be to place curly braces around the if else statements ( if {} else {}). This doesn't change the execution path of your code, but it makes it 100% clear to anyone that is reading your code where the scope is.

There are plenty here that will disagree with me, but from experience working in large teams with shared source, having everything clearly defined makes integration, maintenance and debugging easier.
0
 
jagadeeshbandlamudiCommented:
You want to return either true or False right, then why do you want to use  'string missing = "" '. If anything other than 1 just return False
0
 
anyoneisAuthor Commented:
No, I need "missing". if the column is null, I want to return an indication that the field was missing. This might be a "." for a SAS data file, or a blank for a pipe-delimited file.

What I was looking for was some revelation on formatting nullable types, or an approach with a custom format provider.
0
 
jagadeeshbandlamudiCommented:
Then I guess whatever you are doing is right.. This might be of some help to you
  \
            public type format(DbType dbType)
              {
                     switch(dbType)
                         case DbType.Boolean: return typeof(bool);
                          break;
0
 
jagadeeshbandlamudiCommented:
public type format(DbType dbType)
              {
                     switch(dbType)
                         case DbType.Boolean: return typeof(bool);
                          break;
                       default:
                             return typeof(string);      
}
0
 
anyoneisAuthor Commented:
I guess we are done with this. Thanks for your input, everyone!
0
All Courses

From novice to tech pro — start learning today.