locked
Error when using a stored proc RRS feed

  • Question

  • User801447716 posted

    Hi,

    I am trying to use a sp with a try / using, I can get this to work on manual insert / update but not with a sp:

    the error line is the 'Using connection as New SqlConnection(DBConn)

     

            Dim DBConn As New SqlConnection()
            DBConn.ConnectionString = ConfigurationManager.ConnectionStrings("contoDB").ConnectionString
    
            Dim command As New SqlCommand("dbo.NewApplication", DBConn)
    
            Using connection As New SqlConnection(DBConn)
    
                command.CommandType = CommandType.StoredProcedure
    
                command.Parameters.AddWithValue("@ContactNumber", SqlDbType.NVarChar).Value = txtContactNumber.Text
    ....
    
    
                Try
                    connection.Open()
                    command.ExecuteNonQuery()
                    connection.Close()

    Any help is appreciated.

    Monday, May 23, 2016 7:28 AM

Answers

  • User753101303 posted

    Hi,

    Always tell which error you have. For now you create a new connection that you use for the command and then you are creating another connection again (using the first so more likely you have an error message telling that the argument type is not expected).

    Instead it would rather something suc h as :

    Dim DBConn As string
            DBConn = ConfigurationManager.ConnectionStrings("contoDB").ConnectionString
            Using connection As New SqlConnection(DBConn)
               Using command As New SqlCommand("dbo.NewApplication", connection)
                   ' etc...
    

    So when you have the message you likely currently see it means that the type of the argument that you provide is not correct (ie providing a SqlConnection when a string is expected for example).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 23, 2016 8:08 AM