Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Nullable types giving error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tshad - 22 Feb 2008 23:01 GMT
I have the following in c# VS 2005.

fn.ProjectTableID = dbReader["ProjectTableID"] == DBNull.Value ? null :
(int?)dbReader["ProjectTableID"];

This works fine as long as dbReader["ProjectTableID"] is null.

If it is a value I get an error:

Specified cast is not valid.

Why is that?

How do I assign the value?

I tried:

     fn.ProjectTableID.Value = dbReader["ProjectTableID"] == DBNull.Value ?
null : (int?)dbReader["ProjectTableID"];

but that didn't work either.

Thanks,

Tom
Jon Skeet [C# MVP] - 22 Feb 2008 23:12 GMT
> I have the following in c# VS 2005.
>
[quoted text clipped - 17 lines]
>
> but that didn't work either.

Have you tried casting to "int" instead of "int?" ?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

tshad - 23 Feb 2008 00:21 GMT
>> I have the following in c# VS 2005.
>>
[quoted text clipped - 20 lines]
>
> Have you tried casting to "int" instead of "int?" ?

Yes.

If I change it to:

     fn.ProcessingType = dbReader["ProcessingType"] == DBNull.Value ? null
: (int)dbReader["ProcessingType"];

I get:

   Type of conditional expression cannot be determined because there is no
implicit conversion between '<null>' and 'int'

Thanks,

Tom
Kalpesh - 23 Feb 2008 00:43 GMT
tshad,

I am guessing that fn.ProcessingType is of type nullable int.
See if this works

   fn.ProcessingType = dbReader["ProcessingType"] == DBNull.Value ?
(int?)0
: (int)dbReader["ProcessingType"];

Does that work?

Kalpesh
Kalpesh - 23 Feb 2008 00:46 GMT
My bad.
I am not too good with the concept of nullable type

Either above or this should work.

   fn.ProcessingType = dbReader["ProcessingType"] == DBNull.Value ?
(int?)0
: (int?)dbReader["ProcessingType"];
Jon Skeet [C# MVP] - 22 Feb 2008 23:13 GMT
> I have the following in c# VS 2005.
>
[quoted text clipped - 17 lines]
>
> but that didn't work either.

Ignore my previous response.

Find out what dbReader["ProjectTableID"].GetType() shows. I suspect it
won't be System.Int32.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

tshad - 23 Feb 2008 00:35 GMT
>> I have the following in c# VS 2005.
>>
[quoted text clipped - 23 lines]
> Find out what dbReader["ProjectTableID"].GetType() shows. I suspect it
> won't be System.Int32.

It isn't.

It depends.  The type in SQL is tinyint.  In my record fn.ProjectTableID is
an "int?".  The value will either be null or a number from 1-25.

If it is null, which is the case for the first 12 records, I get the
following in the debugger watch window

  {Name = "DBNull" FullName = "System.DBNull"} System.Type
{System.RuntimeType}

:If it is a value like 10, I get:

{Name = "Byte" FullName = "System.Byte"} System.Type {System.RuntimeType}

I would assume that I would be casting it to either int? or int since I am
putting it in an int? field.  I can't change the fn.ProjectTableID to int
(or byte) as it can be null and I need to keep track of that.

Thanks,

Tom
Arne Vajhøj - 23 Feb 2008 01:07 GMT
> It depends.  The type in SQL is tinyint.  In my record fn.ProjectTableID is
> an "int?".  The value will either be null or a number from 1-25.
[quoted text clipped - 8 lines]
>
> {Name = "Byte" FullName = "System.Byte"} System.Type {System.RuntimeType}
                             ^^^^^^^^^^^

> I would assume that I would be casting it to either int? or int since I am
> putting it in an int? field.  I can't change the fn.ProjectTableID to int
> (or byte) as it can be null and I need to keep track of that.

Try cast it to byte.

Arne
tshad - 24 Feb 2008 06:58 GMT
>> It depends.  The type in SQL is tinyint.  In my record fn.ProjectTableID
>> is an "int?".  The value will either be null or a number from 1-25.
[quoted text clipped - 15 lines]
>
> Try cast it to byte.

That works fine.

Since a tinyint ranges from 0 to 255, I assume that is why it is assumed to
be a byte.

fn.ProcessingType = dbReader["ProcessingType"] == DBNull.Value ? null :
(byte?)dbReader["ProcessingType"];

This works ok

I would have assumed that int would also work but I guess not.

Thanks,

Tom

> Arne
Jon Skeet [C# MVP] - 24 Feb 2008 07:23 GMT
<snip>

> That works fine.
>
[quoted text clipped - 7 lines]
>
> I would have assumed that int would also work but I guess not.

No, because you're unboxing. When you unbox, you have to specify the
correct type, not just one which the correct type can be implicitly
converted to.

(You can also specify the nullable version of the correct type.)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Arne Vajhøj - 24 Feb 2008 16:57 GMT
>>> It depends.  The type in SQL is tinyint.  In my record fn.ProjectTableID
>>> is an "int?".  The value will either be null or a number from 1-25.
[quoted text clipped - 25 lines]
>
> I would have assumed that int would also work but I guess not.

Maybe you will find this code snippet illustrative:

using System;

namespace E
{
    public class Program
    {
        public static void Main(string[] args)
        {
            byte b = 123;
            object o = b;
            Console.WriteLine("Cast to byte works: " + (byte)o);
            try{
                Console.WriteLine("Cast to int works: " + (int)o);
            }
            catch
            {
                Console.WriteLine("Cast to int did not work");
            }
            Console.WriteLine("Cast to byte cast to int works: " + (int)(byte)o);
            Console.ReadKey();
        }
    }
}

Arne
tshad - 24 Feb 2008 18:35 GMT
>>>> It depends.  The type in SQL is tinyint.  In my record
>>>> fn.ProjectTableID is an "int?".  The value will either be null or a
[quoted text clipped - 53 lines]
> }
> }

(int)(byte)o

So what you did here was unbox the object and then implicitly convert it to
int?

And why is dbReader["ProcessingType"] considered boxed?

Thanks,

Tom

> Arne
Jon Skeet [C# MVP] - 24 Feb 2008 19:07 GMT
<snip>

> (int)(byte)o
>
> So what you did here was unbox the object and then implicitly convert it to
> int?

Well, explicitly convert it to int - but yes.

> And why is dbReader["ProcessingType"] considered boxed?

Because its return type is "object" - i.e. it will return a reference,
so the only way of returning value type data is to return a boxed form.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Jon Skeet [C# MVP] - 23 Feb 2008 07:24 GMT
<snip>

> It isn't.
>
[quoted text clipped - 10 lines]
>
> {Name = "Byte" FullName = "System.Byte"} System.Type {System.RuntimeType}

Right - that's the important part.

> I would assume that I would be casting it to either int? or int since I am
> putting it in an int? field.

The important thing is what you're casting. You're trying to unbox,
which means the type has to be right. You can cast it to byte? (or
indeed byte) and it'll be fine. Cast the null to int? so that compiler
knows what to make the overall expression type.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.