locked
Inserting data from textbox to MYSQL in VC++

    Question

  • hello,

         I have two textBoxes 1 and 2 . I also have a SQl Table. I have succesfully connected to MYSQL.

        I want to insert the entered textbox data to the sql database,onclick of button in VC++

        I tried the following:

    std::string SOIT="INSERT into udetails(Operator_Name,Batch) VALUES ('" + textBox1->Text + "','" + textBox2->Text + "')";
    query_state=mysql_query(connection,SOIT.c_str()); 

    This Dosent Work it gives the following errors:

    1>------ Build started: Project: WeighingMactus, Configuration: Debug Win32 ------
    1>  tabweighcode.cpp
    1>c:\users\ichange\documents\visual studio 2010\projects\weighingmactus\weighingmactus\tabweighcode.h(558): warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
    1>c:\users\ichange\documents\visual studio 2010\projects\weighingmactus\weighingmactus\tabweighcode.h(684): error C3699: '^' : cannot use this indirection on type 'std::string'
    1>          compiler replacing '^' with '*' to continue parsing
    1>c:\users\ichange\documents\visual studio 2010\projects\weighingmactus\weighingmactus\tabweighcode.h(684): error C2440: 'initializing' : cannot convert from 'System::String ^' to 'std::string *'
    1>          No user-defined-conversion operator available, or
    1>          Cannot convert a managed type to an unmanaged type
    1>c:\users\ichange\documents\visual studio 2010\projects\weighingmactus\weighingmactus\tabweighcode.h(687): error C2228: left of '.c_str' must have class/struct/union
    1>          type is 'std::string *'
    1>          did you intend to use '->' instead?



    • Edited by Harshi_l Friday, December 21, 2012 8:51 AM
    Friday, December 21, 2012 8:43 AM

Answers

  • hello,
     
         I have two textBoxes 1 and 2 . I also have a SQl Table. I have succesfully connected to MYSQL.
     
        I want to insert the entered textbox data to the sql database,onclick of button in VC++
     
        I tried the following:
     
    std::string SOIT="INSERT into udetails(Operator_Name,Batch) VALUES ('" + textBox1->Text + "','" + textBox2->Text + "')";
    query_state=mysql_query(connection,SOIT.c_str());
    There is no implicit conversion from System::String to std::string or const char*. You must do an explicit conversion, for example
     
    String^ sTemp = L"INSERT..." + textBox1->Text + L"','" + textBox2->Text + L"')";
     
    const char* SOIT = (const char*)(void*)Marshal::StringToHGlobalAnsi(sTemp);
    query_state=mysql_query(connection, SOIT);
    Marshal::FreeHGlobal(str);
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Harshi_l Monday, December 24, 2012 5:36 AM
    Friday, December 21, 2012 12:00 PM
  •  String^ stemp=L"Insert into newuser (name,uname,p1,p2) values('" + textBox1->Text + L"','" + textBox2->Text +L "','" + textBox3->Text + L"','" + textBox4->Text +L "')";
     
    the following error occurs: can u resolve please
     
    1>c:\users\ichange\documents\visual studio 2010\projects\advance login form\advance login form\createaccount1.h(250): error C2065: 'L' : undeclared identifier
    ...
    There was a mistake in the code I posted, but it does not explain to me the errors you are seeing. The following complete code works for me with /clr or /clr:pure
     
    #include <iostream>
     
    using namespace System;
    using namespace System::Runtime::InteropServices;
     
    int main()
    {
        String^ text1 = L"David";
        String^ text2 = L"Wilkinson";
        String^ sTemp = L"INSERT...('" + text1 + L"','" + text2 + L"')";
        IntPtr p = Marshal::StringToHGlobalAnsi(sTemp);
        const char* SOIT = (const char*)(void*)p;
        //query_state=mysql_query(connection, SOIT);
        std::cout << SOIT << std::endl;
        Marshal::FreeHGlobal(p);
        return 0;
    }
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Harshi_l Wednesday, December 26, 2012 4:12 AM
    Monday, December 24, 2012 10:51 AM

All replies

  • hello,
     
         I have two textBoxes 1 and 2 . I also have a SQl Table. I have succesfully connected to MYSQL.
     
        I want to insert the entered textbox data to the sql database,onclick of button in VC++
     
        I tried the following:
     
    std::string SOIT="INSERT into udetails(Operator_Name,Batch) VALUES ('" + textBox1->Text + "','" + textBox2->Text + "')";
    query_state=mysql_query(connection,SOIT.c_str());
    There is no implicit conversion from System::String to std::string or const char*. You must do an explicit conversion, for example
     
    String^ sTemp = L"INSERT..." + textBox1->Text + L"','" + textBox2->Text + L"')";
     
    const char* SOIT = (const char*)(void*)Marshal::StringToHGlobalAnsi(sTemp);
    query_state=mysql_query(connection, SOIT);
    Marshal::FreeHGlobal(str);
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Harshi_l Monday, December 24, 2012 5:36 AM
    Friday, December 21, 2012 12:00 PM
  •  String^ stemp=L"Insert into newuser (name,uname,p1,p2) values('" + textBox1->Text + L"','" + textBox2->Text +L "','" + textBox3->Text + L"','" + textBox4->Text +L "')";

    the following error occurs: can u resolve please

    1>c:\users\ichange\documents\visual studio 2010\projects\advance login form\advance login form\createaccount1.h(250): error C2065: 'L' : undeclared identifier
    1>c:\users\ichange\documents\visual studio 2010\projects\advance login form\advance login form\createaccount1.h(250): error C2143: syntax error : missing ';' before 'string'
    1>c:\users\ichange\documents\visual studio 2010\projects\advance login form\advance login form\createaccount1.h(250): error C2065: 'L' : undeclared identifier
    1>c:\users\ichange\documents\visual studio 2010\projects\advance login form\advance login form\createaccount1.h(250): error C2143: syntax error : missing ';' before 'string'
    1>c:\users\ichange\documents\visual studio 2010\projects\advance login form\advance login form\createaccount1.h(258): error C2065: 'stemp' : undeclared identifier

    Monday, December 24, 2012 7:53 AM
  •  String^ stemp=L"Insert into newuser (name,uname,p1,p2) values('" + textBox1->Text + L"','" + textBox2->Text +L "','" + textBox3->Text + L"','" + textBox4->Text +L "')";
     
    the following error occurs: can u resolve please
     
    1>c:\users\ichange\documents\visual studio 2010\projects\advance login form\advance login form\createaccount1.h(250): error C2065: 'L' : undeclared identifier
    ...
    There was a mistake in the code I posted, but it does not explain to me the errors you are seeing. The following complete code works for me with /clr or /clr:pure
     
    #include <iostream>
     
    using namespace System;
    using namespace System::Runtime::InteropServices;
     
    int main()
    {
        String^ text1 = L"David";
        String^ text2 = L"Wilkinson";
        String^ sTemp = L"INSERT...('" + text1 + L"','" + text2 + L"')";
        IntPtr p = Marshal::StringToHGlobalAnsi(sTemp);
        const char* SOIT = (const char*)(void*)p;
        //query_state=mysql_query(connection, SOIT);
        std::cout << SOIT << std::endl;
        Marshal::FreeHGlobal(p);
        return 0;
    }
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Harshi_l Wednesday, December 26, 2012 4:12 AM
    Monday, December 24, 2012 10:51 AM