I am getting following error, while passing a C++ string variable to SQL insert command:

error C2679: binary '+' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

Below is the code:

int main()
{
    int empid = 12;
    int phone = 23456;
    std::string fname = "Pavan";

    SqlDataAdapter^ custDA = gcnew SqlDataAdapter("SELECT Emp_ID,FirstName,LastName,Phone,Service FROM Employee", 
         "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Employee");
    SqlConnection^ custConn = custDA->SelectCommand->Connection;

    //custDA->MissingSchemaAction = MissingSchemaAction::AddWithKey;

    try
    {
        custConn->Open();
        custDA->InsertCommand = gcnew SqlCommand("insert into Employee (Emp_ID,FirstName,LastName,Phone,Service) values("+empid_+",'"+fname+"','Gupta',"+phone_+",'gdjdjsjsaa')",custConn);

        custDA->InsertCommand->ExecuteNonQuery();
        printf("Row(s) Inserted !! ");
     }
     catch (Exception^ ex)
     {
         printf("%s",ex->ToString());
     }


  custConn->Close();
}

What are empid_ and phone_? Are they strings? If not they need to be.

empid and phone are integer but fname is string. empid and phone are getting parsed without any error but only when I provide string variable in my SQL insert statement, then I am getting this error

They need to be strings in order to combine them with a string. The compiler is complaining that there is no + operator that takes an int and a string

But int is getting parsed correctly inside SQL insert statement. The issue is with String parameter only. As per my knowledge, I am using the correct syntax to pass a string and int variable to SQL insert statement. If I am wrong, then pls let me know the correct syntax.

You need to make everything a string. You can not have "some text" + int + string + "some text". Also according to the MSDN SqlCommand() takes a String not a string. I dont deal with String's so im not sure if there is a implicit conversion or not.

Many thanks Nathan. I tried using String^ fname also but it also gave same error. Please let me know the correct syntax to use it. I may be using it wrong.

I have already told you. You need to convert the int's into strings. there is no built in support for integer and string concatenation. If you can use a string then you should be able to do the following. It requires includeding <sstream>

custConn->Open();
stringstream ss;
ss << "insert into Employee (Emp_ID,FirstName,LastName,Phone,Service) values(" << empid << ",'" << fname << "','Gupta'," << phone <<",'gdjdjsjsaa')"
custDA->InsertCommand = gcnew SqlCommand(ss.str(),custConn);

custDA->InsertCommand->ExecuteNonQuery();
printf("Row(s) Inserted !! ");

Thanks Nathan for providing me the complete code. I replace my section of code with this, but it started giving following error:

/*error C2297: '<<' : illegal, right operand has type 'const char [71]'
    1>select.cpp(51): error C2297: '<<' : illegal, right operand has type 'const char [3]'

    1>          d:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(951) : see declaration of 'std::operator <<'
    1>select.cpp(51): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,signed char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'int'
    1>          d:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\ostream(851) : see declaration of 'std::operator <<'
    1>select.cpp(51): error C2784: 'std::basic_ostream<char,_Traits> &std::operator <<(std::basic_ostream<char,_Traits> &,char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'int'

    1>select.cpp(52): error C2039: 'str' : is not a member of 'System::Int32'
    1>          c:\program files (x86)\reference assemblies\microsoft\framework\.netframework\v4.0\mscorlib.dll : see declaration of 'System::Int32'*/

I tried again using first solution of yours, using String instead of string and with few modifications it worked fine and my code is running as expected now..Thanks to you.. :)

Now next challenge for me is to convert string values to String, bcoz the values I am entering into table are string and to parse them into SQL insert, I need to convert them into String values. Need your help in this..

Thanks in advance.

You can use the following to convet from System::String^ to std:string

#include <msclr/marshal_cppstd.h>

System::String^ xyz="Hi boys"; 

std::string converted_xyz=msclr::interop::marshal_as< std::string >( xyz);

Thanks alot Nathan. It worked. Thanks a lot for all your help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.