casting problem in DataTable

I'm executing a sql server query to get back a datatable.  one of the columns I am casting into a bit, but what I examine the column.DataType, it is an Int32.  

anyone know if there is a workaround for this?
LVL 5
jjacksnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rbrogan_eeCommented:
What is the query?  What is the datatype for the column in the database and where are you doint the casing?  In T-sql or C#?

With that info I can help out.

Thanks!
jjacksnAuthor Commented:
I am running the query in sql (a big single query with many sub selects, one of them looks like this:

select (cast  (case col1 when 'answer1' then 1 when 'answer2' then 0 else null end) as bit) from registration

when I examine this column in C#'s DataTable object, this column DataType = Int32.  I need to be able to inspec the column type and figure out that it was a bit in the query.  
rbrogan_eeCommented:
jjacksn,

I built a stored proc like this

create proc bitTest as
select cast((case fname when 'Pedro' then 1 when 'Ann' then 0 else null end)as bit)  from employee where fname = 'Ann' or fname = 'Pedro'

I ran this against the pubs sample database in SQL 2K.  I created a dataset like this:  

                  sqlConnection1.Open(); // Connection details ommited
                  SqlDataAdapter da = new SqlDataAdapter(sqlCommand1);  // This executes the stored proc bitTest
                  DataSet ds = new DataSet();
                  da.Fill (ds);
                  Console.WriteLine(ds.Tables[0].Columns[0].DataType.ToString());

The datatype that prints out is System.Boolean as you would expect.

I can recreate your problem.  What OS, version of SQL and version of Visual studio are you using?  I tested this on WinXP pro SP2, SQL 2K with Visual Studio 2003.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

rbrogan_eeCommented:
Sorry I meant I can't recreate your problem.
jjacksnAuthor Commented:
let me inspect further.  to completely replicate my environment, can you confirm that this is the case when you are just passing in the sql as a string.
jjacksnAuthor Commented:
I'm using VS 2003, SQL Server 2K
jjacksnAuthor Commented:
futher, this is the check I am doing:

if(table.Columns[columnIndex].DataType != typeof(string) && table.Columns[columnIndex].DataType != typeof(int))
                  {
                        return table.Columns[columnIndex].DataType;
                  
rbrogan_eeCommented:
I change the stored proc to SQL :select cast((case fname when 'Pedro' then 1 when 'Ann' then 0 else null end)as bit)  from employee where fname = 'Ann' or fname = 'Pedro'
      
and I used this code.
                       sqlConnection1.Open();
                  SqlDataAdapter da = new SqlDataAdapter(sqlCommand1);
                  DataSet ds = new DataSet();
                  da.Fill (ds);

                  Console.WriteLine(ds.Tables[0].Columns[0].DataType.ToString());
                  if(ds.Tables[0].Columns[0].DataType != typeof(string) && ds.Tables[0].Columns[0].DataType != typeof(int)){
                        Console.WriteLine("Column Datatype is not string or int.");
                  }

                  if(ds.Tables[0].Columns[0].DataType == typeof(System.Boolean)  ){
                        Console.WriteLine("Column Datatype is a bit.");
                  }

The output is:
Column Datatype is not string or int.
Column Datatype is a bit.

I am guessing the error is not in this code but somewhere else less obvious.  Is it getting to your if statement?  Is the columnIndex going through the correct range?  Are you using the right table?  If you set a break point on the if statement does it break?

These are just a few places I would check.  You can send me the code if you want and I can test it on my system.  Do you have all the latest service packs and patches installed?

Not sure what else to ask.

Thanks,
Roger

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jjacksnAuthor Commented:
UG.  the columns I was inspecting was using a different sql generator path that had not yet had the casts inserted for that particular sub select.  thanks for all the help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.