Cast output parameter from SqlGuid to Guid

G

gozza

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
 
P

Pablo Castro [MS]

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 said:
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
 
G

gozza

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 said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top