Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2006-12-01
9
1,424 Views
Last Modified: 2008-01-09
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]
0
Comment
Question by:anyoneis
  • 3
  • 3
  • 3
9 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 18057911
That looks good enough to me. Do you have any specific concerns about this approach?
0
 
LVL 29

Assisted Solution

by:Nightman
Nightman earned 500 total points
ID: 18057924
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
 
LVL 11

Author Comment

by:anyoneis
ID: 18062561
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 29

Expert Comment

by:Nightman
ID: 18062586
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
 
LVL 1

Expert Comment

by:jagadeeshbandlamudi
ID: 18069998
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
 
LVL 11

Author Comment

by:anyoneis
ID: 18074666
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
 
LVL 1

Expert Comment

by:jagadeeshbandlamudi
ID: 18074851
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
 
LVL 1

Expert Comment

by:jagadeeshbandlamudi
ID: 18074853
public type format(DbType dbType)
              {
                     switch(dbType)
                         case DbType.Boolean: return typeof(bool);
                          break;
                       default:
                             return typeof(string);      
}
0
 
LVL 11

Author Comment

by:anyoneis
ID: 18128457
I guess we are done with this. Thanks for your input, everyone!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question