Hi,

I had a look at MSDN and it clearly sais that SqlGuid can be casted as Guid.
To execute a stored procedure I have the following function (note the s.proc
does not return any recordsets, just sets the output value):

function updateEntry(ref System.Guid valueID, ...){

...
cmd.ExecuteNonQuery();
//get the output value now:
valueID = (System.Guid)cmd.Parameters["@valueID"].Value;
....
}

Please note that the valueID is passed by reference. The code works fine
if I don't pass a value (i.e. set the parameter to DBNull): the valueID gets
set as expected. The problem occurs when I do pass a value. I get an
Invalid Cast exception?!

If I use the following sequence for getting the valueID, the code works
fine, but as you can guess, it seems a bit too much:

....
//get the output value now:
object o = cmd.Parameters["@valueID"].Value;
valueID = new Guid(o.ToString());
...

Any ideas what I am missing in the first version?

Thanks,
Goran

Re: Cast output parameter from SqlGuid to Guid by Pablo

Pablo
Wed Sep 08 16:23:09 CDT 2004

Let me take a wild guess without having tried it here: the type of the Value
property is 'object', so the compiler cannot actually see the implicit
conversion operator from SqlGuid to Guid (because it doesn't know at compile
time that the type of the value will be SqlGuid).

So if you change it to:

> valueID = (System.Guid)(SqlGuid)cmd.Parameters["@valueID"].Value;

Then it should work.

This assumes that the binding of overloaded implicit conversion operators
happens at compile-time, which seems to make sense but I don't remember for
sure now.

--
Pablo Castro
Program Manager - ADO.NET Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.


"gozza" <azzog@N-O-S-P-AM.hotmail.com> wrote in message
news:%230JgV9WlEHA.592@TK2MSFTNGP11.phx.gbl...
> Hi,
>
> I had a look at MSDN and it clearly sais that SqlGuid can be casted as
Guid.
> To execute a stored procedure I have the following function (note the
s.proc
> does not return any recordsets, just sets the output value):
>
> function updateEntry(ref System.Guid valueID, ...){
>
> ...
> cmd.ExecuteNonQuery();
> //get the output value now:
> valueID = (System.Guid)cmd.Parameters["@valueID"].Value;
> ....
> }
>
> Please note that the valueID is passed by reference. The code works fine
> if I don't pass a value (i.e. set the parameter to DBNull): the valueID
gets
> set as expected. The problem occurs when I do pass a value. I get an
> Invalid Cast exception?!
>
> If I use the following sequence for getting the valueID, the code works
> fine, but as you can guess, it seems a bit too much:
>
> ....
> //get the output value now:
> object o = cmd.Parameters["@valueID"].Value;
> valueID = new Guid(o.ToString());
> ...
>
> Any ideas what I am missing in the first version?
>
> Thanks,
> Goran
>
>



Re: Cast output parameter from SqlGuid to Guid by gozza

gozza
Tue Sep 14 19:58:39 CDT 2004

Ok, this is getting a bit strange: if I pass a NULL value (DbNull) to the
stored procedure, then type of cmd.Parameters["@valueID"].Value is
System.Guid. If it's not non-NULL value, then the type of
cmd.Parameters["@valueID"].Value is SqlGuid?!?!?!

What's happening with the SQL Server provider? Why this inconsistent
behaviour?

Thanks,
Goran




"Pablo Castro [MS]" <pablocas@online.microsoft.com> wrote in message
news:eQQyOoelEHA.1936@TK2MSFTNGP12.phx.gbl...
> Let me take a wild guess without having tried it here: the type of the
> Value
> property is 'object', so the compiler cannot actually see the implicit
> conversion operator from SqlGuid to Guid (because it doesn't know at
> compile
> time that the type of the value will be SqlGuid).
>
> So if you change it to:
>
>> valueID = (System.Guid)(SqlGuid)cmd.Parameters["@valueID"].Value;
>
> Then it should work.
>
> This assumes that the binding of overloaded implicit conversion operators
> happens at compile-time, which seems to make sense but I don't remember
> for
> sure now.
>
> --
> Pablo Castro
> Program Manager - ADO.NET Team
> Microsoft Corp.
>
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
>
> "gozza" <azzog@N-O-S-P-AM.hotmail.com> wrote in message
> news:%230JgV9WlEHA.592@TK2MSFTNGP11.phx.gbl...
>> Hi,
>>
>> I had a look at MSDN and it clearly sais that SqlGuid can be casted as
> Guid.
>> To execute a stored procedure I have the following function (note the
> s.proc
>> does not return any recordsets, just sets the output value):
>>
>> function updateEntry(ref System.Guid valueID, ...){
>>
>> ...
>> cmd.ExecuteNonQuery();
>> //get the output value now:
>> valueID = (System.Guid)cmd.Parameters["@valueID"].Value;
>> ....
>> }
>>
>> Please note that the valueID is passed by reference. The code works
>> fine
>> if I don't pass a value (i.e. set the parameter to DBNull): the valueID
> gets
>> set as expected. The problem occurs when I do pass a value. I get an
>> Invalid Cast exception?!
>>
>> If I use the following sequence for getting the valueID, the code works
>> fine, but as you can guess, it seems a bit too much:
>>
>> ....
>> //get the output value now:
>> object o = cmd.Parameters["@valueID"].Value;
>> valueID = new Guid(o.ToString());
>> ...
>>
>> Any ideas what I am missing in the first version?
>>
>> Thanks,
>> Goran
>>
>>
>
>