SqlParameter parameter with parameter Name not contained?

I have a problem with something that I have done many times, but this time it just doesn't work.

This is what I am trying to do (in Visual Studio 2003 and VB.NET)

Earlier in the code:

Private SaveCustomerInformationCommand As System.Data.SqlClient.SqlCommand

Then this is in the configuration process:

SaveCustomerInformationCommand = New System.Data.SqlClient.SqlCommand("spSaveCustomerInformation")
SaveCustomerInformationCommand.CommandType = Data.CommandType.StoredProcedure
Dim custIdParameter As System.Data.SqlClient.SqlParameter = SaveCustomerInformationCommand.CreateParameter()
custIdParameter.ParameterName = "@CustId"
custIdParameter.SqlDbType = Data.SqlDbType.Int
custIdParameter.Direction = Data.ParameterDirection.Input
SaveCustomerInformationCommand.Parameters.Add(custIdParameter)

And then:

SaveCustomerInformationCommand.Parameters.Item("@CustId").Value = myValue

And I get this:

System.IndexOutOfRangeException: An SqlParameter with ParameterName '@CustId' is not contained by this SqlParameterCollection.

Any ideas / solutions?

+3
source share
1 answer

AFAIK, "@" is technically not part of the parameter name ... rather, it is what you put in your T-SQL to indicate that after that the parameter name will appear. Therefore, I think that you will need to refer to it like this (without the "@"):

SaveCustomerInformationCommand.Parameters.Item("CustId").Value = myValue

, , , , , , .

+2

Source: https://habr.com/ru/post/1718806/


All Articles