Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > System.InvalidCastException was unhandled by user code

Reply
Thread Tools

System.InvalidCastException was unhandled by user code

 
 
Deven
Guest
Posts: n/a
 
      07-01-2009
I have custom login.aspx page in which I am calling store procedure to verify
if user exist in database. For some reason, when I run application to test,
it will crash on following line with error message...

sqlLoginCheck.Parameters.Add("P")

Message="The SqlParameterCollection only accepts non-null SqlParameter type
objects, not String objects."

Below is exact code in my logincheck sub procedure.

Protected Sub LoginCheck()

Dim intResult As Integer
Dim conSqlConnection As SqlConnection = New
SqlConnection(ConfigurationManager.ConnectionStrin gs("SRConnectionString").ConnectionString)
Dim sqlLoginCheck As SqlCommand = New SqlCommand("uspCheckLogin",
conSqlConnection)

sqlLoginCheck.CommandType = Data.CommandType.StoredProcedure
sqlLoginCheck.CommandText = "uspCheckLogin"

sqlLoginCheck.Parameters.Add("@Username", Data.SqlDbType.NVarChar,
10).Value = txtUsername.Text
sqlLoginCheck.Parameters.Add("@Password", Data.SqlDbType.NVarChar,
10).Value = txtPassword.Text

Dim P As SqlParameter = New SqlParameter("@Result",
Data.SqlDbType.Int)

P.Direction = Data.ParameterDirection.Output
conSqlConnection.Open()
sqlLoginCheck.Parameters.Add("P")
sqlLoginCheck.ExecuteNonQuery()
intResult = Convert.ToInt32(sqlLoginCheck.Parameters("@Result" ).Value)
conSqlConnection.Close()
If intResult = 1 Then
Session("strUsername") = txtUsername.Text
Server.Transfer("success.aspx")
Else
Server.Transfer("Failed.aspx")
End If
End Sub

Any help in this matter would be appreciated.

Thanks,
Deven
 
Reply With Quote
 
 
 
 
miher
Guest
Posts: n/a
 
      07-01-2009



"Deven" <(E-Mail Removed)> az al√°bbiakat √*rta a k√∂vetkezŇĎ
√ľzenetben news:(E-Mail Removed)...
> I have custom login.aspx page in which I am calling store procedure to
> verify
> if user exist in database. For some reason, when I run application to
> test,
> it will crash on following line with error message...
>
> sqlLoginCheck.Parameters.Add("P")
>
> Message="The SqlParameterCollection only accepts non-null SqlParameter
> type
> objects, not String objects."
>
> Below is exact code in my logincheck sub procedure.
>
> Protected Sub LoginCheck()
>
> Dim intResult As Integer
> Dim conSqlConnection As SqlConnection = New
> SqlConnection(ConfigurationManager.ConnectionStrin gs("SRConnectionString").ConnectionString)
> Dim sqlLoginCheck As SqlCommand = New SqlCommand("uspCheckLogin",
> conSqlConnection)
>
> sqlLoginCheck.CommandType = Data.CommandType.StoredProcedure
> sqlLoginCheck.CommandText = "uspCheckLogin"
>
> sqlLoginCheck.Parameters.Add("@Username", Data.SqlDbType.NVarChar,
> 10).Value = txtUsername.Text
> sqlLoginCheck.Parameters.Add("@Password", Data.SqlDbType.NVarChar,
> 10).Value = txtPassword.Text
>
> Dim P As SqlParameter = New SqlParameter("@Result",
> Data.SqlDbType.Int)
>
> P.Direction = Data.ParameterDirection.Output
> conSqlConnection.Open()
> sqlLoginCheck.Parameters.Add("P")
> sqlLoginCheck.ExecuteNonQuery()
> intResult =
> Convert.ToInt32(sqlLoginCheck.Parameters("@Result" ).Value)
> conSqlConnection.Close()
> If intResult = 1 Then
> Session("strUsername") = txtUsername.Text
> Server.Transfer("success.aspx")
> Else
> Server.Transfer("Failed.aspx")
> End If
> End Sub
>
> Any help in this matter would be appreciated.
>
> Thanks,
> Deven


Hi,
What the line < sqlLoginCheck.Parameters.Add("P") > does is trying to add
the string "P" as an sqlparameter, however what You would like to do is to
add the variable named P to the parameters collection using the line:
sqlLoginCheck.Parameters.Add(P)
(notice that there are no quotes around P)

-Zsolt

 
Reply With Quote
 
 
 
 
Deven
Guest
Posts: n/a
 
      07-02-2009

Thank you for your quick response. It resolved my problem.

Thanks,
Deven

"miher" wrote:

>
>
> "Deven" <(E-Mail Removed)> az al√°bbiakat √*rta a k√∂vetkezŇĎ
> √ľzenetben news:(E-Mail Removed)...
> > I have custom login.aspx page in which I am calling store procedure to
> > verify
> > if user exist in database. For some reason, when I run application to
> > test,
> > it will crash on following line with error message...
> >
> > sqlLoginCheck.Parameters.Add("P")
> >
> > Message="The SqlParameterCollection only accepts non-null SqlParameter
> > type
> > objects, not String objects."
> >
> > Below is exact code in my logincheck sub procedure.
> >
> > Protected Sub LoginCheck()
> >
> > Dim intResult As Integer
> > Dim conSqlConnection As SqlConnection = New
> > SqlConnection(ConfigurationManager.ConnectionStrin gs("SRConnectionString").ConnectionString)
> > Dim sqlLoginCheck As SqlCommand = New SqlCommand("uspCheckLogin",
> > conSqlConnection)
> >
> > sqlLoginCheck.CommandType = Data.CommandType.StoredProcedure
> > sqlLoginCheck.CommandText = "uspCheckLogin"
> >
> > sqlLoginCheck.Parameters.Add("@Username", Data.SqlDbType.NVarChar,
> > 10).Value = txtUsername.Text
> > sqlLoginCheck.Parameters.Add("@Password", Data.SqlDbType.NVarChar,
> > 10).Value = txtPassword.Text
> >
> > Dim P As SqlParameter = New SqlParameter("@Result",
> > Data.SqlDbType.Int)
> >
> > P.Direction = Data.ParameterDirection.Output
> > conSqlConnection.Open()
> > sqlLoginCheck.Parameters.Add("P")
> > sqlLoginCheck.ExecuteNonQuery()
> > intResult =
> > Convert.ToInt32(sqlLoginCheck.Parameters("@Result" ).Value)
> > conSqlConnection.Close()
> > If intResult = 1 Then
> > Session("strUsername") = txtUsername.Text
> > Server.Transfer("success.aspx")
> > Else
> > Server.Transfer("Failed.aspx")
> > End If
> > End Sub
> >
> > Any help in this matter would be appreciated.
> >
> > Thanks,
> > Deven

>
> Hi,
> What the line < sqlLoginCheck.Parameters.Add("P") > does is trying to add
> the string "P" as an sqlparameter, however what You would like to do is to
> add the variable named P to the parameters collection using the line:
> sqlLoginCheck.Parameters.Add(P)
> (notice that there are no quotes around P)
>
> -Zsolt
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
an unhandled win32 exception was unhandled occurred in inetinfo.exe Warren Tang ASP .Net 1 09-23-2008 03:01 PM
Error "Http exception was unhandled by user code. Request Not Availablein this context" Blasting Cap ASP .Net 0 05-17-2006 12:40 PM
Passing by reference / "Unhandled exception: User breakpoint" Andy C++ 4 10-07-2005 08:19 PM
Unhandled Exception (does not look like it's happening within code =?Utf-8?B?Q3JhaWc4MzE=?= ASP .Net 4 09-14-2005 04:29 PM
problems in vc++:Applying setting user breakpoint, unhandled exception ,linking error,runtime error manish C++ 1 04-02-2004 12:33 PM



Advertisments