Monday, March 26, 2012

Parameter Conversions Question from SQL Server

I have a question regarding a casting error I recieve when I explictly assign a value to a value in a stored procedure......
The value pstrLoginName is a public string defined in a code module
The stored procedure is NVARCHAR Datatypes....

Here is the Stored Procedure

CREATE procedure dbo.Appt_GetPermissions_NET
(
@.LoginName nvarchar(15),
@.Password NvarChar(15),
@.DeleteScan bit Output

)
as
select

@.DeleteScan=UserP_DeleteScan
from
Clinic_Users
where
UserName = @.LoginName
and
UserPassword = @.Password

GO

Here is my error

Specified cast is not valid
Line 194: cmdsql.CommandType = CommandType.StoredProcedure
Line 195:
Line 196: parmLogin = cmdsql.Parameters.Add("@.LoginName", SqlDbType.NVarChar, 15).Value = pstrLoginName
Line 197: parmPassword = cmdsql.Parameters.Add("@.LoginPassword", SqlDbType.NVarChar, 15).Value = pstrLoginPassword
Line 198: parmDelete = cmdsql.Parameters.Add("@.DeleteScan", SqlDbType.Bit)

my vb .net code to execute this

Public Sub ObtainPermission()

'get the needed data
Dim consql As New SqlConnection("Server=myserver;database=APPOINTMENTS;uid=webtest;pwd=webtest")
Dim cmdsql As New SqlCommand
Dim parmLogin As SqlParameter
Dim parmPassword As SqlParameter
Dim parmDelete As SqlParameter

cmdsql = New SqlCommand("Appt_GetPermissions_NET", consql)
cmdsql.CommandType = CommandType.StoredProcedure

parmLogin = cmdsql.Parameters.Add("@.LoginName", SqlDbType.NVarChar, 15).Value = pstrLoginName <--error occurs here

parmPassword = cmdsql.Parameters.Add("@.LoginPassword", SqlDbType.NVarChar, 15).Value = pstrLoginPassword <--I'm sure it will happen here then
parmDelete = cmdsql.Parameters.Add("@.DeleteScan", SqlDbType.Bit)
parmDelete.Direction = ParameterDirection.Output

consql.Open()
cmdsql.ExecuteNonQuery()

'obtain rights

pstrPermissions = cmdsql.Parameters("@.DeleteScan").Value
consql.Close()

End SubDude, your doing too much on one line!
If you are going to do all this on one line you'll have to add some paranthesis:
(parmLogin = cmdsql.Parameters.Add("@.LoginName", SqlDbType.NVarChar, 15)).Value = pstrLoginName

I'd recommend splitting the row in two lines for better readability.|||You have both this line:


Dim cmdsql As New SqlCommand

and this line:

cmdsql = New SqlCommand("Appt_GetPermissions_NET", consql)

Note that you have used "New" twice. Remove the "New" from the first line and you should have better luck.
Terri|||Ofcourse it's wrong to allocate an object twice,
but it shouldn't raise an error, since he hasn't used the variable
before allocating it the second time.
Won't it just 'leak' an object instance?sql

No comments:

Post a Comment