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. Managed C++/CLI
  4. ADO.NET : SqlDataReader : I need to assign retrieved value to C++ variable

ADO.NET : SqlDataReader : I need to assign retrieved value to C++ variable

Scheduled Pinned Locked Moved Managed C++/CLI
helpcsharpc++
8 Posts 3 Posters 4 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.
  • D Offline
    D Offline
    Doug
    wrote on last edited by
    #1

    I can get the following to work, the result displaying on the screen: Console::WriteLine(myReader->GetSqlValue(3)); What I really need to do is to assign the item retrieved to a C++ variable eg. int halfhrSQL = myReader->GetSqlValue(3); This gives the C2440 compile error: cannot convert from 'System::Object __gc *' to 'int'. No matter what cast I use, I cannot get around the error. Please help if you can. Thanks, Doug (New Zealand) Doug

    N 1 Reply Last reply
    0
    • D Doug

      I can get the following to work, the result displaying on the screen: Console::WriteLine(myReader->GetSqlValue(3)); What I really need to do is to assign the item retrieved to a C++ variable eg. int halfhrSQL = myReader->GetSqlValue(3); This gives the C2440 compile error: cannot convert from 'System::Object __gc *' to 'int'. No matter what cast I use, I cannot get around the error. Please help if you can. Thanks, Doug (New Zealand) Doug

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      Use Convert::ToInt32

      public: static int ToInt32(
      Object* value
      );

      Regards, Nish


      Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

      D 3 Replies Last reply
      0
      • N Nish Nishant

        Use Convert::ToInt32

        public: static int ToInt32(
        Object* value
        );

        Regards, Nish


        Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

        D Offline
        D Offline
        Doug
        wrote on last edited by
        #3

        Hi Nish Thanks very much for pointing me in the right direction! I now have the code working: int halfhrSQL = Convert::ToInt32(myReader->GetValue(3)); It took me a while too find out I had to use GetValue(). Not GetSqlValue(3). Not GetSqlInt32(3). Not GetData(). Not GetInt32(). The above compile but throw the following exception: "System.InvalidCastException : Specified cast is not valid at System.Convert.ToInt32(Object value)" Best regards Doug Doug

        N 1 Reply Last reply
        0
        • D Doug

          Hi Nish Thanks very much for pointing me in the right direction! I now have the code working: int halfhrSQL = Convert::ToInt32(myReader->GetValue(3)); It took me a while too find out I had to use GetValue(). Not GetSqlValue(3). Not GetSqlInt32(3). Not GetData(). Not GetInt32(). The above compile but throw the following exception: "System.InvalidCastException : Specified cast is not valid at System.Convert.ToInt32(Object value)" Best regards Doug Doug

          N Offline
          N Offline
          Nick Parker
          wrote on last edited by
          #4

          Doug if you are still having a problem you might want to read this[^] article from MSDN, especially the remarks. :) Nick Parker
          The greatest lesson in life is to know that even fools are right sometimes. - Winston Churchill


          D 2 Replies Last reply
          0
          • N Nick Parker

            Doug if you are still having a problem you might want to read this[^] article from MSDN, especially the remarks. :) Nick Parker
            The greatest lesson in life is to know that even fools are right sometimes. - Winston Churchill


            D Offline
            D Offline
            Doug
            wrote on last edited by
            #5

            Thanks everyone, problem solved!;) Doug

            1 Reply Last reply
            0
            • N Nish Nishant

              Use Convert::ToInt32

              public: static int ToInt32(
              Object* value
              );

              Regards, Nish


              Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

              D Offline
              D Offline
              Doug
              wrote on last edited by
              #6

              Thanks Nish, I have definitely got this working where the native C++ type is int and I am confident that I can now handle any numeric type. What I am battling with is where the .NET value retrieved is a String (the SQL field is text). I have managed to get the following to work: String* busNumberSQL __gc[] = new String* __gc[97]; short halfhoursSQL = 0; . . . Console::WriteLine(myReader->GetString(0)); halfhoursSQL++; busNumberSQL[halfhoursSQL] = Convert::ToString(myReader->GetValue(0)); However busNumberSQL is a managed array of type String*. Now I need assign the value of busNumberSQL[halfhoursSQL] to a CString or native C++ string type. All I seem to end up with is the decimal representation of the hexidecimal memory address. I have also tried: Convert::ToChar(myReader->GetValue(0)) to assign one character (Convert::ToString(myReader->GetValue(0)))->ToCharArray() As I continue to try different things, do you have any idea how this may be done? I will let you know if I crack it. (With the old ADO I was able to do this by doing a double cast: ---> _bstr_t --> LPCTSTR and then doing a simple assignment to a CString) Thanks and regards Doug Doug

              1 Reply Last reply
              0
              • N Nish Nishant

                Use Convert::ToInt32

                public: static int ToInt32(
                Object* value
                );

                Regards, Nish


                Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

                D Offline
                D Offline
                Doug
                wrote on last edited by
                #7

                Hi Nish Re: ADO.NET : SqlDataReader : I need to assign retrieved value to C++ variable I have found the source of the problem expressed in the thread "ADO.NET : SqlDataReader : I need to assign retrieved value to C++ variable": http://www.codeproject.com/script/comments/forums.asp?forumid=3785#xx309754xx but do not yet know why it occurs or how I can fix it. The problem arises with the /clr compiler setting. Here is the code to illustrate this: ///////////////////////////////////////////////////////// #include #include #include "try_ADONET.h" //#using //#using //#using // This is required for the ADO.NET Provider using namespace std; //using namespace System; int Main() { CString busNumberSQL[2]; CString bus = "Hello"; busNumberSQL[1] = bus; return 0; } //////////////////////////////////////////////////////// Without the /clr setting the array of CString busNumberSQL[2] is correctly contructed as is CString bus and the line busNumberSQL[1] = bus; works as expected. This can be seen by putting a breakpoint in at return 0; and seeing the variables in a Watch. Copy the expanded Watch details (Name, Value & Type) to an EXCEL sheet. .................. Next, add the compiler switch /clr and rerun the code. CString bus is constructed correctly BUT something weird happens in the construction of CString busNumberSQL[2]. Now, busNumberSQL[1] = bus; results in the address of bus as an integer being assigned to busNumberSQL[1], and look at the Watch details!! Copy the expanded Watch details (Name, Value & Type) to an EXCEL sheet Name: busNumberSQL; Value: {Length=2}; Type: ATL::CStringT > >[] ...(Note no dimension at end compared to the "no /clr" case. Next Watch line: [0] 2083454756 __int32 Next Watch line: [1] 3103464 __int32 My conclusion is that busNumberSQL[2] is not an array of CString. Of course, I need the /clr in order to use ADO.NET! It seems as though a CString works, but a CString array for some (unknown) cannot even be defined. Do you have any light for me? (I have not got into your previous post yet on Marshall) Best regards Doug. (In case you are wondering I am very much a beginner with C++ and .NET) Doug

                1 Reply Last reply
                0
                • N Nick Parker

                  Doug if you are still having a problem you might want to read this[^] article from MSDN, especially the remarks. :) Nick Parker
                  The greatest lesson in life is to know that even fools are right sometimes. - Winston Churchill


                  D Offline
                  D Offline
                  Doug
                  wrote on last edited by
                  #8

                  Hi Nick Re: ADO.NET : SqlDataReader : I need to assign retrieved value to C++ variable I have found the source of the problem expressed in the thread "ADO.NET : SqlDataReader : I need to assign retrieved value to C++ variable": http://www.codeproject.com/script/comments/forums.asp?forumid=3785#xx309754xx but do not yet know why it occurs or how I can fix it. The problem arises with the /clr compiler setting. Here is the code to illustrate this: ///////////////////////////////////////////////////////// #include #include #include "try_ADONET.h" //#using //#using //#using // This is required for the ADO.NET Provider using namespace std; //using namespace System; int Main() { CString busNumberSQL[2]; CString bus = "Hello"; busNumberSQL[1] = bus; return 0; } //////////////////////////////////////////////////////// Without the /clr setting the array of CString busNumberSQL[2] is correctly contructed as is CString bus and the line busNumberSQL[1] = bus; works as expected. This can be seen by putting a breakpoint in at return 0; and seeing the variables in a Watch. Copy the expanded Watch details (Name, Value & Type) to an EXCEL sheet. .................. Next, add the compiler switch /clr and rerun the code. CString bus is constructed correctly BUT something weird happens in the construction of CString busNumberSQL[2]. Now, busNumberSQL[1] = bus; results in the address of bus as an integer being assigned to busNumberSQL[1], and look at the Watch details!! Copy the expanded Watch details (Name, Value & Type) to an EXCEL sheet Name: busNumberSQL; Value: {Length=2}; Type: ATL::CStringT > >[] ...(Note no dimension at end compared to the "no /clr" case. Next Watch line: [0] 2083454756 __int32 Next Watch line: [1] 3103464 __int32 My conclusion is that busNumberSQL[2] is not an array of CString. Of course, I need the /clr in order to use ADO.NET! It seems as though a CString works, but a CString array for some (unknown) cannot even be defined. Do you have any light for me? (I have not got into your previous post yet on Marshall) Best regards Doug. (In case you are wondering I am very much a beginner with C++ and .NET) Doug

                  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