Bug #9668 Invalid attempt to access a field before calling Read()
Submitted: 6 Apr 2005 5:11 Modified: 17 Aug 2005 12:43
Reporter: Nathan Lewis Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:1.0.4 OS:Microsoft Windows (Windows XP)
Assigned to: Assigned Account CPU Architecture:Any

[6 Apr 2005 5:11] Nathan Lewis
Description:
The following exception is thrown if I create a connection, create a command, set the command type to StoredProcedure and try to execute the command in any way, shape or form.

MySql.Data.MySqlClient.MySqlException: Exception trying to retrieve parameter in
fo for TestProc: Invalid attempt to access a field before calling Read() ---> My
Sql.Data.MySqlClient.MySqlException: Invalid attempt to access a field before ca
lling Read()
   at MySql.Data.MySqlClient.MySqlDataReader.GetFieldValue(Int32 index)
   at MySql.Data.MySqlClient.MySqlDataReader.GetString(Int32 index)
   at MySql.Data.MySqlClient.StoredProcedure.GetParameterList(String spName)
   at MySql.Data.MySqlClient.StoredProcedure.Prepare(String spName)
   --- End of inner exception stack trace ---
   at MySql.Data.MySqlClient.StoredProcedure.Prepare(String spName)
   at MySql.Data.MySqlClient.MySqlCommand.PrepareSqlBuffers(String sql)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior
)
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
   at MySqlTestApp.Class1.Main() in c:\development\other projects\mysqltestapp\c
lass1.cs:line 22

How to repeat:
Create a test database (TestDB)
Add a stored procedure (not actually necessary it seems)
create procedure TestProc ()
BEGIN
SELECT 1;
END$$

Use the following, simple code:

public static void Main()
{
	try
	{
		MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection("server=localhost;database=TestDB;User ID=root;Password=root");
		conn.Open();

		MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("TestProc",conn);
		cmd.CommandType = System.Data.CommandType.StoredProcedure;
		
		cmd.ExecuteReader();					
	}
	catch(Exception e)
	{
		System.Console.WriteLine(e);
	}

}

I've tried:
1.  Various methods of constructing the command and connection
2.  Various methods of executing the command, including ExecuteNonQuery() and using a MySqlDataAdapter with .Fill()
3.  The 1.0 and 1.1 version of the .NET Connector
4.  deleting \data and Re-installing MySql (version 5.0.3-beta-nt)

I would like to find out I'm just doing something stupid, but everything seems to point to a bug, the test code is too simple.
[7 Apr 2005 3:04] Nathan Lewis
I've confirmed this behavior on the following combinations:

1.0.4 -> 5.0.3-beta-nt (two distinct systems)
1.0.4bk -> 5.0.3-beta-nt
1.0.4 -> 5.0.3-beta-standard (linux)
1.0.4 -> 5.0.0a-alpha-max-debug (windows)

I was unable to test against 5.0.0-alpha because I wasn't able to create a stored procedure with it (mysql.proc doesn't exist by default?)

I was going to test with previous versions of the connector, but I wasn't able to find any for download.
[7 Apr 2005 14:20] Nathan Lewis
I think I've discovered the problem.  The problem is the database name is case-sensitive, but somewhere along the line MySql transformed it into lower case.  If you look at the mysql.proc table, you'll see that the db column is all lower case, even if you created the database with mixed-case.  However, my connection string was using mixed case, which made the retrieval of the parameter list fail.  

The same error message occurs when the procedure cannot be found - I think this should return a more informative error message anyway.

If db names in MySql are not case sensitive, then the query that retrieves the parameters for a stored procedure also needs to be case-insensitive.
[18 Apr 2005 15:35] StuFF mc
I have the exact same problem. Coming from a World where Stored Procedures are up and running since 10 years (Sql Server), I have to admit I sometime think about suicide with MySql 5. I'm running the connector 1.0.4 and MySql 5.0.3; I anyway don't see any case-sensitivity issue; Here the mysql.proc did not change anything, but still, I get this message. It's also a pitty that the Parameters Test Ficture do not do anything with SP, so we cannot even test it :( I'm also pretty impressed by the number of people reacting to such a bug... 3 with me :) If any of you all (2) guys find it, let the world know :) :)
[18 Apr 2005 15:37] StuFF mc
Sorry we are only 2 ;) It seems to me that MySql is very much used to have inlines queries like "select * from all" in the code, but when it comes to 3 tier apps... :)
[19 Apr 2005 3:26] Nathan Lewis
This is in fact the exception / error message you will receive whenever you try to call a stored procedure it can't find.  If you dig through the .NET connector, you'll see it basically does a select on mysql.proc to get the parameters for the stored procedure.  This is the first place it will fail if you have either the name of the stored procedure wrong and/or the name of the database wrong.  They have to match exactly what is in mysql.proc.
[25 Apr 2005 3:02] Michael Dawson
This is a very annoying bug! Are there any known workarounds?
[25 Apr 2005 9:42] Caroline Larue
Hello, I have the same error when I try to read a record which is recording by another user at the same time... This is a different cause, but the same error.

I use VB.net with the connector MySql  v. 1.0.4.20163
[25 Apr 2005 12:53] Caroline Larue
Hello, I understand my problem only now, excuse me, my reader try to load a record which is not existed, indeed my request "where" is not right... so my error is absolutely normal. The record does not exist.
[17 May 2005 15:25] Christian Roy
Confirm the bug here database, tables, stored procedure name and parameters is in lowercase, cannot find the reason it does not work since it is almost the same code as calling our postgre function :

C#

public void ExecuteStoredProcedure(string ProcName, params CArg[] Args){
try
{
	MySqlConnection MyConn = new MySqlConnection(ConnectionString());
	MyConn.Open();

	MySqlCommand cmd = new MySqlCommand(ProcName, MyConn);
	cmd.CommandType = CommandType.StoredProcedure;

	//Compter les arguments et ajouter comme paramètre.
	foreach(CArg arg in Args)
	{
		cmd.Parameters.Add("?" + ((CArg)arg).Name, ((CArg)arg).Value);
		cmd.Parameters["?" + ((CArg)arg).Name].Direction =
                ParameterDirection.Input;
	}
				
	cmd.ExecuteNonQuery();

}
catch(Exception ex)
{
	Console.WriteLine(ex.ToString());
}
}

Stored Procedure :

CREATE PROCEDURE `inview01`.`add_cat`(p_cat_fr varchar(100), p_cat_en varchar(100))
INSERT INTO adm_call_cat (cat_fr, cat_en) values (p_cat_fr, p_cat_en)

Connection String for C# is the following :
"Data Source= localhost;Database=inview01;User ID=root;Password=root"

Exception from code :

ex	{"Exception trying to retrieve parameter info for add_cat: Invalid attempt to access a field before calling Read()" }	System.Exception
+	[MySql.Data.MySqlClient.MySqlException]	{MySql.Data.MySqlClient.MySqlException}	MySql.Data.MySqlClient.MySqlException
	System.Object	{MySql.Data.MySqlClient.MySqlException}	System.Object
	_className	null	string
	_COMPlusExceptionCode	-532459699	int
	_exceptionMethod	<valeur non définie>	System.Reflection.MethodBase
	_exceptionMethodString	null	string
	_helpURL	null	string
	_HResult	-2146232832	int
+	_innerException	{"Invalid attempt to access a field before calling Read()" }	System.Exception
	_message	"Exception trying to retrieve parameter info for add_cat: Invalid attempt to access a field before calling Read()"	string
	_remoteStackIndex	0	int
	_remoteStackTraceString	null	string
	_source	null	string
+	_stackTrace	{System.Array}	System.Object
	_stackTraceString	null	string
	_xcode	-532459699	int
	_xptrs	0	int
	HelpLink	null	string
	HResult	-2146232832	int
+	InnerException	{"Invalid attempt to access a field before calling Read()" }	System.Exception
	Message	"Exception trying to retrieve parameter info for add_cat: Invalid attempt to access a field before calling Read()"	string
	Source	"MySql.Data"	string
	StackTrace	"   at MySql.Data.MySqlClient.StoredProcedure.Prepare(String spName)\r\n   at MySql.Data.MySqlClient.MySqlCommand.PrepareSqlBuffers(String sql)\r\n   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()\r\n   at InViewSharedLib.DataBase.MySql.CMySql.ExecuteStoredProcedure(String ProcName, CArg[] Args) in c:\\visualstudio.net\\req\\inview2.0\\inview2.0\\inviewsharedlib\\database\\mysql\\cmysql.cs:line 99"	string
+	TargetSite	{System.Reflection.RuntimeMethodInfo}	System.Reflection.MethodBase
[18 May 2005 10:45] Vasily Kishkin
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. Because of this, we hope you add your comments
to the original bug instead.

Thank you for your interest in MySQL.
[18 May 2005 14:08] Nathan Lewis
Where is the other, newer, bug report for this bug?  I did several searches and did not find anything.  I still cannot find it.
[19 May 2005 16:30] Nathan Lewis
I have gone through all the bugs, both open and closed for the .NET connector and I can't find the other bug report that talks about this bug.  Vasily Kishkin, can you tell me why you marked this as duplicate?
[13 Jul 2005 2:30] Naveen Kohli
I have started using .Net connector 1.0.4 today and I am also getting this anoying message. I can't move any further with testing of SPs in MySQL 5.0.7 because I can't execute even the simplest ones...
What is ETA on this bug?
[17 Aug 2005 7:13] Aleksandar Micevski
Hey guys... 
1.It is a case sensitive, and you need all parameters, and the name of the stored procedure to be the same as in the stored procedure. I've had a problem because of BLABLA_ID and BLABLA_Id!!!! Just take your time and check the code... 
2. Try to drop that stored procedure and then to created again. Of course you will save the main body of the procedure. This also helped me with other stored procedure:)))). 

Regards
[17 Aug 2005 12:43] Reggie Burnett
This is the same issue as bug #11542
[13 Nov 2006 8:15] amit verma
well I'm having the excat name in the same case but still getting the problem ..
[24 Feb 2008 13:20] ram krishna
Invalid attempt to access a field before calling Read()

Attachment: WebForm2.aspx.cs (text/plain), 3.01 KiB.

[25 Feb 2008 15:28] Reggie Burnett
ram krishna

did the fixed version not work in your case?  I saw nothing wrong with your code so there might be something in your sp code that is showing a bug.  Can you try a simpler sproc and see if you can isolate it that way?