Share via

Converting VBA to VB.NET, getting compile error BC30311

Giacomo Raucci 366 Reputation points
2023-07-20T18:52:29.7233333+00:00

First of all, thanks to Jachen and Andy for their extreme help in my attempt to convert VBA app to VB.NET. The only error left (with this Winform) is Error BC30311 - Value of Type 'Parameter' cannot be converted to 'Integer'. I am attempting to call an SQL stored procedure using ADO. Below are the steps that I captured and the compiler error is on the last statement: returnValue = cmd.parameters("ReturnValue"). I would appreciate any help on this statement that compiles with VBA, but does not compile with Visual Studio's VB.NET. Thanks in advance, Giacomo

        storedProcName = "BackupHOHDatabase"
        cmd = New ADODB.Command
        With cmd
            .ActiveConnection = conn
            .CommandType = CommandTypeEnum.adCmdStoredProc
            .CommandText = storedProcName
            .NamedParameters = True
        End With

        param1 = cmd.CreateParameter("ReturnValue", DataTypeEnum.adInteger, ParameterDirectionEnum.adParamReturnValue)
        cmd.Parameters.Append(param1)
        '
        '  If TodaysDB = False, Then Previous Year's database is to be backuped up
        '  If TodaysDB = True, Then Current Year's database is to be backed up
        '
        If TodaysDB = False Then      ' The 1st parameter, the Previous Year's Database is to be backed up
            param2 = cmd.CreateParameter("@Databasename", DataTypeEnum.adVarChar, ParameterDirectionEnum.adParamInput, lengthOfDBName, Database)
            cmd.Parameters.Append(param2)
        Else
            param2 = cmd.CreateParameter("@Suffix", DataTypeEnum.adVarChar, ParameterDirectionEnum.adParamInput, lengthOfSuffix, Suffix)
            cmd.Parameters.Append(param2)
        End If
        param3 = cmd.CreateParameter("@TodaysDB", DataTypeEnum.adBoolean, ParameterDirectionEnum.adParamInput, , TodaysDB)
        cmd.Parameters.Append(param3)
        '
        ' Now execute Stored Procedure to Backup the HOHdatabase.
        '
        cmd.Execute()

        returnValue = cmd.Parameters("ReturnValue")
Developer technologies | VB
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other

A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
2023-07-21T01:24:31.3033333+00:00

Hi @Giacomo Raucci ,

In vb.net, you can use the following code to cast it to Integer.

returnValue = CInt(cmd.Parameters("ReturnValue").Value)

Best Regards.

Jiachen Li


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,086 Reputation points
    2023-07-20T19:21:08.32+00:00

    its cmd.Parameters("ReturnValue").Value, which is an object and must be cast to the correct datatype. in c# its:

    returnValue = (int) cmd.Parameters("ReturnValue").Value;

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.