Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 dfx_slayer   Members   -  Reputation: 122

Like
0Likes
Like

Posted 19 May 2009 - 02:48 AM

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

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9629

Like
0Likes
Like

Posted 19 May 2009 - 02:50 AM

Every managed type supports the ToString() member function.

#3 rpg_code_master   Members   -  Reputation: 270

Like
0Likes
Like

Posted 19 May 2009 - 05:05 AM

Quote:
Original post by dfx_slayer

txtItemPrice->Text=reader->GetDecimal(2);



This would look like:



txtItemPrice->Text=reader->GetDecimal(2)->ToString();





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



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




#4 dfx_slayer   Members   -  Reputation: 122

Like
0Likes
Like

Posted 19 May 2009 - 04:43 PM

thx bro..
It's worked..
txtItemPrice->Text=Convert::ToString(reader->GetInt32(2));
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

#5 Promit   Moderators   -  Reputation: 7344

Like
0Likes
Like

Posted 19 May 2009 - 04:56 PM

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.

#6 pulpfist   Members   -  Reputation: 528

Like
0Likes
Like

Posted 20 May 2009 - 05:26 AM

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

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS