System.InvalidCastException was unhandled by user code

Discussion in 'ASP .Net' started by Deven, Jul 1, 2009.

  1. Deven

    Deven Guest

    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.ConnectionStrings("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
     
    Deven, Jul 1, 2009
    #1
    1. Advertising

  2. Deven

    miher Guest

    "Deven" <> az alábbiakat írta a következő
    üzenetben news:...
    > 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.ConnectionStrings("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
     
    miher, Jul 1, 2009
    #2
    1. Advertising

  3. Deven

    Deven Guest

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

    Thanks,
    Deven

    "miher" wrote:

    >
    >
    > "Deven" <> az alábbiakat írta a következő
    > üzenetben news:...
    > > 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.ConnectionStrings("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
    >
    >
     
    Deven, Jul 2, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Floela
    Replies:
    3
    Views:
    582
    Jon Skeet
    Jul 10, 2003
  2. Arjen
    Replies:
    1
    Views:
    7,784
    Carl Prothman [MVP]
    Aug 6, 2003
  3. Miguel Angel Dinelli
    Replies:
    1
    Views:
    454
    Chris Jackson
    Nov 13, 2003
  4. Roy Lawson
    Replies:
    12
    Views:
    5,229
  5. Warren Tang
    Replies:
    1
    Views:
    896
    Warren Tang
    Sep 23, 2008
Loading...

Share This Page