GUID as parameter to stored proc causes Invalid Cast

R

Robert Storrs

I am using the Microsoft Data Access Application Block
for .NET code to interface to our SQL database. I have a
stored procedure (CREATE procedure ixe_MarkBranchStatus
@NodeState int = 0, @rootnode uniqueidentifier = null).

I have the data for rootnode store as a string
representation of a GUID in my C# program. I use the
following code to supply the parameters:
*********************************************************
storedParams =
SqlHelperParameterCache.GetSpParameterSet
(connStr,SQL_Name);
for (int i = 0; i <
storedParams.Length; i++) {
switch
(storedParams.ParameterName.ToUpper()) {

case "@NODESTATE":

storedParams.Value = newStatus;

break;

case "@ROOTNODE":

storedParams.Value = GUID;

break;
}
}
if
(SqlHelper.ExecuteNonQuery
(connStr,CommandType.StoredProcedure,SQL_Name,storedParams)
0) return true;

***********************************************************

The SQLHelper (Microsoft supplied code) bubbles up an
error: Invalid cast from System.String to System.Guid.
The line that causes the error is:

int retval = cmd.ExecuteNonQuery();

but a lot has gone on before this.

This was not a problem until I recently converted from
VS .NET, framework 1.0 to VS 2003, framework 1.1
 
W

William Ryan

Which line is raising the exception? Are you sure that you are setting the
value (ie, stepped through or confirmed that you are in fact hitting the
line which is setting the value). It's an easy thing to do.

Also, I'm not sure the specifics of what you are trying to do, but why
update the GUID? If you set the type on your server, then it will set
whenever you have an insert and then you can be sure it's unique across all
servers if you replicate for instance.
Robert Storrs said:
I am using the Microsoft Data Access Application Block
for .NET code to interface to our SQL database. I have a
stored procedure (CREATE procedure ixe_MarkBranchStatus
@NodeState int = 0, @rootnode uniqueidentifier = null).

I have the data for rootnode store as a string
representation of a GUID in my C# program. I use the
following code to supply the parameters:
*********************************************************
storedParams =
SqlHelperParameterCache.GetSpParameterSet
(connStr,SQL_Name);
for (int i = 0; i <
storedParams.Length; i++) {
switch
(storedParams.ParameterName.ToUpper()) {

case "@NODESTATE":

storedParams.Value = newStatus;

break;

case "@ROOTNODE":

storedParams.Value = GUID;

break;
}
}
if
(SqlHelper.ExecuteNonQuery
(connStr,CommandType.StoredProcedure,SQL_Name,storedParams)
0) return true;

***********************************************************

The SQLHelper (Microsoft supplied code) bubbles up an
error: Invalid cast from System.String to System.Guid.
The line that causes the error is:

int retval = cmd.ExecuteNonQuery();

but a lot has gone on before this.

This was not a problem until I recently converted from
VS .NET, framework 1.0 to VS 2003, framework 1.1
 

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