Bug #43736 "IndexOutOfRangeException" - Compact Framework Connection on Win Mobile Emulator
Submitted: 18 Mar 2009 21:08 Modified: 5 Aug 2009 13:49
Reporter: Alex Latchford Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S4 (Feature request)
Version:6.0.0 OS:Any
Assigned to: Vladislav Vaintroub CPU Architecture:Any
Tags: Connector .Net Windows Mobile Emulator

[18 Mar 2009 21:08] Alex Latchford
Description:
When I attempt to connect to a Database using the Compact Framework version of the connector I am given an "IndexOutOfRangeException" when trying to open the connection..

MySqlConnection connection = new MySqlConnection("SERVER=ip_address;DATABASE=cocktail;UID=user;PASSWORD=pass;");

MySqlCommand command = connection.CreateCommand();
command.CommandText = "select * from cocktails";

connection.Open();
connection.Close();

When I step through the code it never gets to the close line.. I am using Windows Mobile 6.1 SDK and the supplied Professional 6 Emulator with it..

When I attempt to use a non-windows mobile project type it works so sorry if this is a time waster..

How to repeat:
Create a C# Smart Device Project..

Create a button on the form..

Put the follow code and the event when clicked..

MySqlConnection connection = new MySqlConnection("SERVER=ip_address;DATABASE=cocktail;UID=user;PASSWORD=pass;");

MySqlCommand command = connection.CreateCommand();
command.CommandText = "select * from cocktails";

connection.Open();
connection.Close();

Suggested fix:
Sorry, No ideas here.
[16 Apr 2009 0:37] nicolas cieri
Hi man, i have the same problem please give a solution.

My problem is when i put "cn.open"

My connectionstring is good. i can't solve this problem

Please help
[16 Apr 2009 6:09] Tonci Grgin
Hi Alex and thanks for your report.

It is expected for alpha version to have problems but what effectively prevented me from checking this one is bug reported by me Bug#44141.
[16 Apr 2009 14:53] Tonci Grgin
Damn (sorry), I have both VS2005 and VS2008 Pro on my box and have downloaded Windows Mobile 6 SDK to my box (as VS2008 only offers WM 5.0 project) but it won't install claiming I need something called "Windows mobile device center" which I can not find on my MSDN account... It also requires .NET CF 2.0SP1 while I work on 3.5... Finally, my workstation, W2K8 SE, is not in supported OS list.

Now, what's that "Windows mobile device center" and where do I get it? What are the other, not mentioned, requirements for this bug report?

After that I'll have to see if I'll pursue this bug further as Microsoft sure made it almost impossible...
[16 Apr 2009 21:22] Alex Latchford
Tonci,

Sorry for the lack of details, there is a list of requirements on the download page, but it doesn't list 2008 requirements.. Apparently the device center requires Windows Media Player to be installed in order for you to install it..

http://www.microsoft.com/windowsmobile/en-us/help/synchronize/device-center-download.mspx

http://csharponphone.blogspot.com/2008/03/installing-windows-mobile-device-center.html

Thanks, Alex.
[17 Apr 2009 8:09] Tonci Grgin
Alex, I have already tried the emulator and failed miserably (something about manifest) :) Thus I passed this report to c/NET team for further checking. It might take them a while but it is not forgotten.

Thanks for additional info provided.
[8 May 2009 21:47] Nicholas Piasecki
I also encountered this error and worked around it by editing the source distribution as follows:

On line 34 of MySqlPacket.cs, change the following line:

private MemoryStream buffer = new MemoryStream();

to

private MemoryStream buffer = new MemoryStream(5);

The problem is that the default constructor for MySqlPacket calls Clear() which sets the Position of the internal MemoryStream to 5. When the InternalBindParameters() method of Statement enters the if located on line 153 of that file, it attempts to do a WriteByte(0). While previous versions of the Compact Framework may have auto-expanded the internal buffer of the MemoryStream in this state, on NETCF v35 this generates an IndexOutOfRangeException because it seems to only auto-expand when Position == length, not when length >= Capacity.

The big boy (Desktop) version of the .NET Framework v35 tests that Position >= length and so does not have this problem.

So, it's a discrepancy in behavior between the full-blow .NET Framework v35 and the .NET CF v35. While this really should be the NETCF's team's problem, they're probably not going to fix it anytime soon, so MySQL Connector should work around it since it's a simple enough change. After making this simple change, I am able to connect an execute a statement without any ill-perceived effects. Good luck!
[8 May 2009 22:05] Nicholas Piasecki
Created Microsoft Connect feedback issue, see the workaround there for sample code:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=445678
[21 May 2009 8:57] Tonci Grgin
Hi Nicholas and thanks for your efforts.

From what I see this is a bug/undocumented problem in compact framework, right? So, what now? My guess would be wait for Microsoft to fix this...
[21 May 2009 12:30] Nicholas Piasecki
It is a discrepancy between the two frameworks and I've reported it on Microsoft Connect, but we don't want to be in the position of mandating that Connector/NET on the CF requires NETCF 4.0 (which isn't going to be out for at *least* a year) or a particular service pack level that contains the fix (no service packs for NETCF have been announced). The NETCF team usually lags behind the core framework releases.

Considering the fix for Connector/NET is a change to one line of code, I think the burden is on Connector/NET to work around the discrepancy. A purist would say it's Microsoft's problem; a pragmatist would say it Simply Needs To Work.

Just my two cents on the matter.
[21 May 2009 13:02] Tonci Grgin
I am going to agree with Nicholas here and verify the report as S4 (feature request) since God knows when will Microsoft fix this... However, triage remains "Serious" as this is blocking people working on CF.
[5 Aug 2009 0:16] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/80104

740 Vladislav Vaintroub	2009-08-05
      - workaround for .NET compact framework problems when writing behind the end of MemoryStream (contributed by Nicholas Piasecki) (bug#43736)
[5 Aug 2009 13:49] Tony Bedford
An entry was added to the 6.0.5 and 6.1.1 changelogs:

When attempting to connect to MySQL using the Compact Framework version of MySQL Connector/NET, an IndexOutOfRangeException exception was generated on trying to open the connection.