Convert std:string to System::Object_gc*

I am attempting to code a simple program that passes two variables to my SQL Server.  I have created a basic DB called Persons with two columns (ID, Name).   I have setup a dataadapter and all it's friends within my program, with a int variable and string variable (ID and Name).  When I try to add the string variable into my datarow, I receive the message:

error C2664: 'void System::Data::DataRow::set_Item(int,System::Object __gc *)' : cannot convert parameter 2 from 'std::string' to 'System::Object __gc *'

I received the same error message from inserting the ID variable, but added the tostring method to it and it now works fine.  Tried that  on the string variable but no go.

Using:

Visual Studio 2008 (c++)
SQL Server 2008

Any insight would be appreciated.
Thanks in advance.

int ID = 908;
string Name = "Buddy";

	DataRow * myRow;

	SqlDataAdapter * myDataAdapter;
    myDataAdapter = new SqlDataAdapter();

	SqlConnection* conn;
	conn = new SqlConnection(S"Data Source=HELL\\KKH;Initial Catalog=TestingDB;Integrated Security=SSPI;");

	DataSet * myDataSet;
	myDataSet = new DataSet();

	conn->Open();

	SqlParameter * myParameter;
	myDataAdapter->SelectCommand = new SqlCommand (S"select * from Person", conn);

	myDataAdapter->InsertCommand = new SqlCommand("insert into Person (ID, Name) values (@auID, @Name)", conn); 

	myParameter = myDataAdapter->InsertCommand->Parameters->Add(
    new SqlParameter ("@auID", SqlDbType::Int));
    myParameter->SourceColumn = "ID";
    myParameter->SourceVersion = DataRowVersion::Current;

	myParameter = myDataAdapter->InsertCommand->Parameters->Add(
    new SqlParameter ("@Name", SqlDbType::VarChar));
    myParameter->SourceColumn = "Name";
    myParameter->SourceVersion = DataRowVersion::Current;
	
	myDataAdapter->Fill (myDataSet,"a");

	myRow = myDataSet->Tables->Item["a"]->NewRow();
	myRow->Item[0] = ID.ToString(); //WORKS FINE SOMEHOW 
	myRow->Item[1] = Name; //LINE IN QUESTION

	myDataSet->Tables->Item ["a"]->Rows->Add(myRow);

	myDataAdapter->Fill (myDataSet,"a");

	myDataAdapter->Update (myDataSet,"a");
                                            
	conn->Close();
}

Open in new window

dthansenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Does this help

myRow->Item[1] = String(Name);


or instead of
string Name = "Buddy";
use
String Name = "Buddy";



The compiler is getting confused, the 'name' is being compiled as part of the std library, not as a managed code string object.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dthansenAuthor Commented:
Hi AndyAinscow,

Thank you very much for your help.  After implementing your suggestion, I received an error along the lines of "cannot convert parameter 1 from System:String to System::String" (?? how does that work?).  After a little research I alterered the String declaration to:

std::string Name = "Buddy";
System::String* Name1 = Name.c_str();

Then I was able to use the original insertion code to the DataRow( using the new string variable):

myRow->Item[1] = Name1;

Your suggestion got me on the right track, and I thank you for that.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.