Introduction
After i megrate a legacy application from ASP.Net 3.5 to ASP.Net 4 I was very surprised that I get a MissingFieldException when the code attempted to access an out parameter and return value of a storage procedure by using the index property of an SqlParameterCollection.
After I investigated the cause of the problem, i stated, it must be a compiler error.
The cause of the problem was that the code with an object-variable worked, which was assigned an instance of a sqlcommand-objects. the compiler now correctly recognized that 'Parameters' is a SqlParameterCollection. However, they now try to access it and retrieve parameters on the basis of the indexes in the collection or the names, you get refillable following exception. However, the same code works fine under ASP.Net 2.0 and 3.5.
The Exception
System.MissingMemberException: Overload resolution failed because no accessible 'Parameters'
accepts this number of arguments.
Example
Dim cmd = new SqlCommand(...)
cmd.Parameters.Add("@erg").Value = 1
Dim val as Int32 = cmd.Parameters("@erg").Value
Solution 1
One can circumvent this behavior by using the correct typ for the variable instead of an object variable:
Dim cmd AS new SqlCommand(...) cmd.Parameters.Add("@erg").Value = 1
Dim val as Int32 = cmd.Parameters("@erg").Value
Solution 2
Or you can cast the Parameters-Propertie to an SqlParameterCollection and it will also work.
Dim cmd = new SqlCommand(...)
cmd.Parameters.Add("@erg").Value = 1
Dim val as Int32 = DirectCast(cmd.Parameters, SqlParameterCollection)("@erg").Value
Note
Parameters.GetType will return SqlParameterCollection but do not be fooled.
Dim cmd = new SqlCommand(...)
cmd.Parameters.GetType()