Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An SqlParameter with ParameterName is not contained?

Tags:

sql

vb.net

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

This is what 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 in a setup 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 later:

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?


1 Answers

AFAIK, the "@" is not technically part of the name of the parameter... rather it's what you put into your T-SQL to denote that a parameter name is coming afterwards. So I think you'll want to refer to it like this instead (with no "@") :

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

You could also try the same thing when you initially insert the parameter name-- although since you're not getting an error there, I'd suspect the accessor call is to blame, not the inserter call.

like image 66
Justin Grant Avatar answered Jul 01 '26 03:07

Justin Grant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!