sqldttype uniqueidentifier and stored procedure

L

Loren Dummer

I am converting some VB6 code using ADO to VB.NET using ADO.NET.

I am using the sqlcommand object trying to pass a value to a stored
procedure where one of the parameters is a uniqueidentifier datatype.

I keep getting the following error message from .Net. "Invalid cast from
System.String to System.Guid."

Can anyone tell me what datatype I am suppose to use in .NET for this to
work. I have tried specifying a size also used nvarchar, varchar also with
the same error.

When using VB6 all I had to do was specify the parameter datatype as
advarchar and set the length to a number of characters and everything was
OK.

Here is a sample of my code where I am setting the parameter value.

sqlParam = new sqlParameter

With sqlParam

.ParameterName = "@SessionGUID"
.Direction = ParameterDirection.Input
.SQLDbType = SqlDbType.UniqueIdentifier
.Value = sessionguid

End With

..Parameters.Add(SqlParam)

Any help would be appreciated.

Thanks,

Loren
 
B

bruce barker

sessionguid needs to be a System.Guid or a string containing a valid guid
say:

string sessionguid = "0a560418-5d7e-451f-b4e7-aec7a36ccbe9";
 
L

Loren Dummer

I have actually tried changing the sessionguid variable to a datatype of
System.Guid and this did not work either.

What I have is a function that accepts the sessionguid as a string variable.

Here is most of the code for the function.


Private Function ValidateUser(ByVal sessionguid as String) as Boolean
With sqlCmd

.Connection = sqlCn

.CommandText = "p_RegistrantUserSession_ValidateSessionGUID"

.CommandType = CommandType.StoredProcedure

sqlParam = New SqlParameter

With sqlParam

.ParameterName = "@SessionGUID"

.Direction = ParameterDirection.Input

.SqlDbType = SqlDbType.UniqueIdentifier

.Value = sessionguid

End With

.Parameters.Add(sqlParam)

End With

Try

sqlCn.Open()

lReturnValue = sqlCmd.ExecuteNonQuery()

ValidateSessionGUID = sqlCmd.Parameters.Item("@ReturnCode").Value

Catch e As Exception

Throw New Exception(e.Message)

Finally

sqlCmd = Nothing

If (sqlCn.State = ConnectionState.Open) Then

sqlCn.Close()

End If

sqlCn = Nothing

End Try
 
M

Mike

Announcing MyGeneration (100% Free)
http://www.MyGenerationSoftware.com

MyGeneration is a .NET developer tool that generates code from
templates effectively and efficiently by combining a well known
scripting engine with a powerful database meta-data API. It's 100%
FREE: You can use MyGeneration free of charge! The only thing we ask
in return is that you provide us with feedback, report any bugs to us,
contribute to our site, and spread the word. Most code generators
don't allow the user to alias the names of tables, views, columns,
relationships, etc, however, MyGeneration supports this and more!
MyGeneration Supports, through the MyMeta API, Microsoft Sql Server
2000, Oracle, IBM DB2, MySql, and Microsoft Access. MyGeneration is
probably the only Code Generator out there that allows for a dynamic
Graphical User Interface script block which allows you to control the
UI in the template itself. Each Template has an optional User
Interface code section. MyGeneration uses the powerful and well known
Scintilla source code editing control which features syntax
highlighting, search and replace, line numbering, and many other nifty
features! It is also fully configurable through XML configuration
scripts.

Download your copy at http://www.MyGenerationSoftware.com
 

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