I am trying to save user data to a sql table but keep getting the following error;
Unable to cast object of type 'System.Boolean' to type 'System.Data.SqlClient.SqlParameter'.
I am using VWD Express Edition with .NET 2.0 and a SQL 2000 database. The code that generates the error is as follows;
Public
Function chkUser(ByVal strUser As String) As String
dim MyConnection As SqlConnection
Dim MyCommand As SqlCommand
Dim ReturnString As String
Dim params As SqlParameter
Dim SelectCmd As String = "wm_CheckUser"
MyConnection = New SqlConnection(myConnectionString)
MyCommand = New SqlCommand(SelectCmd, MyConnection)
MyCommand.CommandType = CommandType.StoredProcedure Try
params = MyCommand.Parameters.Add("@parUser", SqlDbType.VarChar, 100).Value = strUser
MyCommand.Connection.Open()
MyCommand.ExecuteNonQuery()
ReturnString = "Record Exists!"
Catch Exp As SqlException
ReturnString = ReturnError(Exp.Number, "Users", Exp.Message)
End Try
MyCommand.Connection.Close()
Return ReturnString End FunctionBasically, I'm traying to check to see if a user id already exists in the database before saving the data the user entered. If the email address entered by the user is already in the database I want a message to be shown to the user. If the email address does not exist then the data entered by the user is saved and the form goes to step two (2) of the user registration process.
Any help with this would be greately appreciated. I can't seem to see what is wrong here. Please someone help.
Thanks,
Jaime
![]() |
0 |
![]() |
try this
Try MyCommand.Parameters.Add("@parUser", SqlDbType.VarChar, 100) MyCommand.Parameters[0].Value = strUser ...... ...If this still not work, post your stored procedure here, we can look at more.
![]() |
0 |
![]() |
I tried you suggestion and now I get the following;
The ConnectionString property has not been initialized.
Line 66: MyCommand.Parameters("@parUser").Value = strUser
Line 67:
Line 68: MyCommand.Connection.Open()
Line 69: MyCommand.ExecuteNonQuery()
Line 70: ReturnString = "Record Exists!"
![]() |
0 |
![]() |
in your code:MyConnection = New SqlConnection(myConnectionString) MyCommand = New SqlCommand(SelectCmd, MyConnection)where did you define the variable "myConnectionString", you have to verify you have a valiade connection string before this line
![]() |
0 |
![]() |
Here is the code found in my class file which I use to hold all data relevant code. The connection string is in my web.config file which is why the line under the New Public Sub sets the value of myConnectionString equal to System.Configuration.ConfigurationManager.AppSettings("SiteConnectionString").
Any ideas would be welcomed since I am pretty much stuck here.
Thanks.
Public Class DataClass
Public myConnectionString As String
Public Sub New()
MyBase.New()
'Used In Production Environment (Uncomment line below when in Production)
'myConnectionString = ConfigurationSettings.AppSettings("connectionstring")
'Used In Development Environment (Comment line below when in Production)
myConnectionString = System.Configuration.ConfigurationManager.AppSettings("SiteConnectionString")
End SubPublic
Function chkUser(ByVal strUser As String) As String
Dim MyConnection As SqlConnection
Dim MyCommand As SqlCommand
Dim ReturnString As String
'Dim params As SqlParameter
Dim SelectCmd As String = "wm_CheckUser"
MyConnection = New SqlConnection(myConnectionString)
MyCommand = New SqlCommand(SelectCmd, MyConnection)
MyCommand.CommandType = CommandType.StoredProcedure
MyCommand.Connection.Open()
Try
MyCommand.Parameters.Add("@parUser", SqlDbType.VarChar, 100)
MyCommand.Parameters("@parUser").Value = strUser
MyCommand.ExecuteNonQuery()
ReturnString = "Record Exists!"
Catch Exp As SqlException
ReturnString = ReturnError(Exp.Number, "Users", Exp.Message)
End Try
MyCommand.Connection.Close()
Return ReturnString
End FunctionEnd
Class
![]() |
0 |
![]() |
Put a break point at this line of code, run under debug mode,
MyConnection = New SqlConnection(myConnectionString)
When code stopped, examine the myConnectionString, verify it contain the right coonection string
Hope this help
![]() |
0 |
![]() |
Thank you very much for all your help. I found the problem and it was that I was using a obsolete call to application setting in the web.config file which is no longer used in the 2.0 framework. My oversight when I converted the project from 1.1 to 2.0.
Thanks again for the help.
![]() |
0 |
![]() |
I thought you were using 1.1, when I saw you use appsetting. It is great you solve the problem
![]() |
0 |
![]() |