Jump to content
  • Advertisement
Sign in to follow this  

How To Convert Managed String To Value Or Value To Managed String In Visual C++ .Net

This topic is 3498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Help me pliz.. I got a problem here.. I'm working with Visual C++ .Net 2005, when i'm using this code : txtItemPrice->Text=reader->GetDecimal(2); I got this error : Error 2 error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'System::Decimal' to 'System::String ^' d:\my documents\visual studio 2008\projects\mini_project\mini_project\frmItem.h 322 mini_project Does anybody know how to convert a value into a managed string or how to convert a managed string to a value in Visual C++ .Net.. Help Me Pliz.. T.T

Share this post

Link to post
Share on other sites
Original post by dfx_slayer


This would look like:


If you want to convert a string into an actual value:

String ^myString = "23.5";
Decimal myDecimal = Convert::ToDecimal(myString);

Share this post

Link to post
Share on other sites
thx bro..
It's worked..
but didn't work for Decimal Type.. T.T

I got a new problem here :

sql="INSERT INTO ITEM VALUES('" + txtIDItem->Text->Trim() + "','" + txtItemName->Text->Trim() + "'," + Convert::ToInt32(txtItemPrice->Text) + ",'" + txtItemDescription->Text->Trim() + "')";

when i try to execute that sql statement..
here is the error i got : "Input String was not in a correct format"

In my database :
IDItem : Text
ItemName : Text
ItemPrice : Long
ItemDescription : Text

Could someone help me pliz.. T.T

Share this post

Link to post
Share on other sites
Step 1: Write the final SQL query to some debug output where you can see what it is and confirm that it actually makes sense.
Step 2: Learn to use the actual database query stuff in the Framework, instead of doing this dangerous string mess.

By the way. C++/CLI, which you're using, is a terrible place for a newbie (or any developer, really) to be. Why aren't you using C#? It doesn't sound like you're at all ready to be tackling a language of this complexity and relative obscurity. You'd be much better off in almost ANY other programming language.

Share this post

Link to post
Share on other sites
I remember having problems with reader.GetDecimal and reader.GetDouble too.
Im not sure why they don't work, or why they are not implemented, but I do remember that I ended up with something like this:

double d = Convert.ToDouble(reader->GetValue(0))

You can try that if everything else fails.

As for adding parameters to a query, I prefer to do it this way:

command.CommandText="INSERT INTO ITEM VALUES(@ID, @ItemName, @ItemPrice, @ItemDescription)";
command.Parameters.AddWithValue("@ID", txtIDItem->Text->Trim());
command.Parameters.AddWithValue("@ItemName", txtItemName->Text->Trim());
command.Parameters.AddWithValue("@ItemPrice", Convert::ToInt32(txtItemPrice->Text));
command.Parameters.AddWithValue("@ItemDescription", txtItemDescription->Text->Trim());

ps. My syntax may be scewed, I havent compiled it

Unless you have a reason to use a string as ID I would suggest either an auto incrementing int, or a guid instead.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!