Click here to Skip to main content
15,884,644 members
Articles / Web Development / ASP.NET
Tip/Trick

Possible solutions for an .Net 4 compiler bug that raise an MissingMemberException by acessing the index property of an SqlParameterCollection from an SqlCommand

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
14 May 2014CPOL 10K   1   1
In legacy applications you can get a MissingMemberException bei acessing a SqlParameterCollection if you work with object-variables there are reference a SqlCommand-Instance.

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

VB.NET
Dim cmd = new SqlCommand(...)
cmd.Parameters.Add("@erg").Value = 1
Dim val as Int32 = cmd.Parameters("@erg").Value '<--MissingMemberException 

Solution 1

One can circumvent this behavior by using the correct typ for the variable instead of an object variable:

VB.NET
Dim cmd AS new SqlCommand(...) '<--use the correct typ
cmd.Parameters.Add("@erg").Value = 1
Dim val as Int32 = cmd.Parameters("@erg").Value '<--Works fine

Solution 2

Or you can cast the Parameters-Propertie to an SqlParameterCollection and it will also work.

VB.NET
Dim cmd = new SqlCommand(...)
cmd.Parameters.Add("@erg").Value = 1
Dim val as Int32 = DirectCast(cmd.Parameters, SqlParameterCollection)("@erg").Value '<--Works fine too 

Note

Parameters.GetType will return SqlParameterCollection but do not be fooled.

VB.NET
Dim cmd = new SqlCommand(...)
cmd.Parameters.GetType() '<-- Return SqlParameterCollection

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThank you Pin
Shridhar Gowda26-Jul-15 22:59
Shridhar Gowda26-Jul-15 22:59 
Thank you, It helped me. Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.