Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. AccessViolationException in OdbcDataReader

AccessViolationException in OdbcDataReader

Scheduled Pinned Locked Moved C#
mysql
16 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Frozzeg wrote:

    for (int i = 0; i <= integer+1; i++)

    It should be for (int i = 0; i<integer; i++) DbDataReader columns store collection is ZERO based.

    Life is a stage and we are all actors!

    F Offline
    F Offline
    Frozzeg
    wrote on last edited by
    #4

    it did not help

    C 1 Reply Last reply
    0
    • F Frozzeg

      public Hashtable FetchArray(ref OdbcDataReader reader)
      {
      int counter = 0;
      int integer = reader.FieldCount;
      Hashtable hashtable = new Hashtable();
      if (reader.Read())
      {
      for (int i = 0; i <= integer+1; i++)
      {
      hashtable.Add(reader.GetName(i), reader.GetValue(i)); // Exception here
      }
      counter++;
      }
      return hashtable;
      }

      Im using MySQL Connector/ODBC 5.1

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #5

      Frozzeg wrote:

      for (int i = 0; i <= integer+1; i++)

      How very odd. Why would you want to count to integer+1, that will be 2 more than the number of fields.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      1 Reply Last reply
      0
      • F Frozzeg

        it did not help

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #6

        It has to, because that fixes your ( very obvious ) bug. How about you learn to use the debugger, then examine the code to work out exactly what is wrong ? You can then ask here and you'll get a real answer, because you will ask an intelligent question

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        F 1 Reply Last reply
        0
        • C Christian Graus

          It has to, because that fixes your ( very obvious ) bug. How about you learn to use the debugger, then examine the code to work out exactly what is wrong ? You can then ask here and you'll get a real answer, because you will ask an intelligent question

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          F Offline
          F Offline
          Frozzeg
          wrote on last edited by
          #7

          In the first message, I pointed to a code string with an error

          S 1 Reply Last reply
          0
          • F Frozzeg

            In the first message, I pointed to a code string with an error

            S Offline
            S Offline
            Saksida Bojan
            wrote on last edited by
            #8

            Yes that is true, you get expection when accsess an index that doesn't exsist. If you have 3 indexes.

            for (int i=0; i<=index+1;i++)

            first part: i<=index. when you count 3, loop will do extra loop because of '<=' will do extra loop till i gets to 3. but it is zero based index and if i gets to 3, there are 4 indexes already, while reader have only 3. The second part is +1. Lose it. if you wan't to keep <= then use instead index+1 use index-1 edit: Also all exceptions comes with description, probably yours came with "Index out of boundary". When you wan't to know what is wrong,sometimes it is not enough the place and witch expection. All exceptions comes with a message

            Frozzeg wrote:

            In the first message, I pointed to a code string with an error

            You only pointed where AccessViolationException happens edit 2: Copy paste from MSDN Remarks An access violation occurs in unmanaged or unsafe code when the code attempts to read or write to memory that has not been allocated, or to which it does not have access. This usually occurs because a pointer has a bad value. Not all reads or writes through bad pointers lead to access violations, so an access violation usually indicates that several reads or writes have occurred through bad pointers, and that memory might be corrupted. Thus, access violations almost always indicate serious programming errors. In the .NET Framework version 2.0, an AccessViolationException clearly identifies these serious errors. In programs consisting entirely of verifiable managed code, all references are either valid or null, and access violations are impossible. An AccessViolationException occurs only when verifiable managed code interacts with unmanaged code or with unsafe managed code. Version Information This exception is new in the .NET Framework version 2.0. In earlier versions of the .NET Framework, an access violation in unmanaged code or unsafe managed code is represented by a NullReferenceException in managed code. A NullReferenceException is also thrown when a null reference is dereferenced in verifiable managed code, an occurrence that does not involve data corruption, and there is no way to distinguish between the two situations in versions 1.0 or 1.1.

            modified on Monday, August 17, 2009 5:09 AM

            1 Reply Last reply
            0
            • F Frozzeg

              public Hashtable FetchArray(ref OdbcDataReader reader)
              {
              int counter = 0;
              int integer = reader.FieldCount;
              Hashtable hashtable = new Hashtable();
              if (reader.Read())
              {
              for (int i = 0; i <= integer+1; i++)
              {
              hashtable.Add(reader.GetName(i), reader.GetValue(i)); // Exception here
              }
              counter++;
              }
              return hashtable;
              }

              Im using MySQL Connector/ODBC 5.1

              F Offline
              F Offline
              Frozzeg
              wrote on last edited by
              #9

              It was a stupid mistake in "for", I mistakenly used instead of - +, thx but i still have AccessViolationException P.S. sry for my English

              S 1 Reply Last reply
              0
              • F Frozzeg

                It was a stupid mistake in "for", I mistakenly used instead of - +, thx but i still have AccessViolationException P.S. sry for my English

                S Offline
                S Offline
                Saksida Bojan
                wrote on last edited by
                #10

                And what kind of accsess vaiolation occorus: please post detail. Including its message

                F 1 Reply Last reply
                0
                • S Saksida Bojan

                  And what kind of accsess vaiolation occorus: please post detail. Including its message

                  F Offline
                  F Offline
                  Frozzeg
                  wrote on last edited by
                  #11

                  System.AccessViolationException was unhandled Message="Attempting to read or write protected memory. This often indicates that other memory is damaged." Source="System.Data" StackTrace: в System.Data.Common.UnsafeNativeMethods.SQLGetData(OdbcStatementHandle StatementHandle, UInt16 ColumnNumber, SQL_C TargetType, CNativeBuffer TargetValue, IntPtr BufferLength, IntPtr& StrLen_or_Ind) в System.Data.Odbc.OdbcStatementHandle.GetData(Int32 index, SQL_C sqlctype, CNativeBuffer buffer, Int32 cb, IntPtr& cbActual) в System.Data.Odbc.OdbcDataReader.GetData(Int32 i, SQL_C sqlctype, Int32 cb, Int32& cbActualOut) в System.Data.Odbc.OdbcDataReader.internalGetString(Int32 i) в System.Data.Odbc.OdbcDataReader.GetValue(Int32 i, TypeMap typemap) в System.Data.Odbc.OdbcDataReader.GetValue(Int32 i) в FMySQLClient.MySQL.FetchArray(OdbcDataReader& reader) в C:\FMySQLClient\FMySQLClient\MySQL.cs:line 199 в FMySQLClient.Form1.button1_Click(Object sender, EventArgs e) в C:\FMySQLClient\FMySQLClient\Form1.cs:line 30 в System.Windows.Forms.Control.OnClick(EventArgs e) в System.Windows.Forms.Button.OnClick(EventArgs e) в System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) в System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) в System.Windows.Forms.Control.WndProc(Message& m) в System.Windows.Forms.ButtonBase.WndProc(Message& m) в System.Windows.Forms.Button.WndProc(Message& m) в System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) в System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) в System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) в System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) в System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) в System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) в System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) в System.Windows.Forms.Application.Run(Form mainForm) в FMySQLClient.Program.Main() в C:\FMySQLClient\FMySQLClient\Program.cs:line 18 в System.AppDomain._nExecuteAssemb

                  S 1 Reply Last reply
                  0
                  • F Frozzeg

                    System.AccessViolationException was unhandled Message="Attempting to read or write protected memory. This often indicates that other memory is damaged." Source="System.Data" StackTrace: в System.Data.Common.UnsafeNativeMethods.SQLGetData(OdbcStatementHandle StatementHandle, UInt16 ColumnNumber, SQL_C TargetType, CNativeBuffer TargetValue, IntPtr BufferLength, IntPtr& StrLen_or_Ind) в System.Data.Odbc.OdbcStatementHandle.GetData(Int32 index, SQL_C sqlctype, CNativeBuffer buffer, Int32 cb, IntPtr& cbActual) в System.Data.Odbc.OdbcDataReader.GetData(Int32 i, SQL_C sqlctype, Int32 cb, Int32& cbActualOut) в System.Data.Odbc.OdbcDataReader.internalGetString(Int32 i) в System.Data.Odbc.OdbcDataReader.GetValue(Int32 i, TypeMap typemap) в System.Data.Odbc.OdbcDataReader.GetValue(Int32 i) в FMySQLClient.MySQL.FetchArray(OdbcDataReader& reader) в C:\FMySQLClient\FMySQLClient\MySQL.cs:line 199 в FMySQLClient.Form1.button1_Click(Object sender, EventArgs e) в C:\FMySQLClient\FMySQLClient\Form1.cs:line 30 в System.Windows.Forms.Control.OnClick(EventArgs e) в System.Windows.Forms.Button.OnClick(EventArgs e) в System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) в System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) в System.Windows.Forms.Control.WndProc(Message& m) в System.Windows.Forms.ButtonBase.WndProc(Message& m) в System.Windows.Forms.Button.WndProc(Message& m) в System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) в System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) в System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) в System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) в System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) в System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) в System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) в System.Windows.Forms.Application.Run(Form mainForm) в FMySQLClient.Program.Main() в C:\FMySQLClient\FMySQLClient\Program.cs:line 18 в System.AppDomain._nExecuteAssemb

                    S Offline
                    S Offline
                    Saksida Bojan
                    wrote on last edited by
                    #12

                    Does the loop run at least once? I suspect it is connector problem. You coud try using MySql Connector/Net(I Used it, because other connectors were painfull to get them working). It is a form of external dll that you can reference to it. And you are not required to install it as it is required with ODBC connector. Try asking in their forum, you coud stumble upon a bug.

                    F 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Haven't tried it, but...

                      Frozzeg wrote:

                      int integer = reader.FieldCount; ... for (int i = 0; i <= integer+1; i++)

                      Isn't that reading FieldCount + 1 entries?

                      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #13

                      I don't think so. It attempts to read FieldCount+2 entries. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                      OriginalGriffO 1 Reply Last reply
                      0
                      • S Saksida Bojan

                        Does the loop run at least once? I suspect it is connector problem. You coud try using MySql Connector/Net(I Used it, because other connectors were painfull to get them working). It is a form of external dll that you can reference to it. And you are not required to install it as it is required with ODBC connector. Try asking in their forum, you coud stumble upon a bug.

                        F Offline
                        F Offline
                        Frozzeg
                        wrote on last edited by
                        #14

                        "Does the loop run at least once?" - no Ok, thx

                        1 Reply Last reply
                        0
                        • L Luc Pattyn

                          I don't think so. It attempts to read FieldCount+2 entries. :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                          OriginalGriffO Offline
                          OriginalGriffO Offline
                          OriginalGriff
                          wrote on last edited by
                          #15

                          My bad. Missed the "=" in "<=" - I blame Monday morning disease! :laugh:

                          No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                          1 Reply Last reply
                          0
                          • F Frozzeg

                            public Hashtable FetchArray(ref OdbcDataReader reader)
                            {
                            int counter = 0;
                            int integer = reader.FieldCount;
                            Hashtable hashtable = new Hashtable();
                            if (reader.Read())
                            {
                            for (int i = 0; i <= integer+1; i++)
                            {
                            hashtable.Add(reader.GetName(i), reader.GetValue(i)); // Exception here
                            }
                            counter++;
                            }
                            return hashtable;
                            }

                            Im using MySQL Connector/ODBC 5.1

                            S Offline
                            S Offline
                            Singan
                            wrote on last edited by
                            #16

                            I say that you are to use. for (int i = 0; i <= integer-1; i++) or for (int i = 0; i < integer; i++) If reader.FieldCount = 4 get the i parts 0,1,2,3

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups