tags:

views:

278

answers:

1

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?

+2  A: 

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.

Justin Grant