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