CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2011
    Posts
    27

    Error while executing the insert query.

    Its an console window application.I am getting the error (unknown String)while running the below code.Kindly check the error and let me know where i have to change.
    ----------------------------------------------------------------------------------------------------------

    Code:
    #include "stdafx.h"
    #include "objbase.h"
    #include "icrsint.h"
    #include <ole2.h>
    
     #import "C:\Program Files\Common Files\System\ado\msado15.dll" \
     no_namespace rename("EOF", "EndOfFile")
     void Insert();
    
     int main(int argc, char* argv[])
     {
    	    //HRESULT CoInitialize(NULL);
    	    if ( FAILED(::CoInitialize(NULL)) )
                return -1;
                Insert();
    			::CoUninitialize();
    		    getchar();
                return 0;             
     }
    
      void Insert()
    
    {
        /*The following variables will be initialized with necessary values and  appended to the strSQL values*/
         _bstr_t strName;
         _bstr_t strAge;
         _bstr_t strDOB;
           _bstr_t strSalary;
    
           _ConnectionPtr pConn = NULL;
    
         // Define string variables for ADO connection
         _bstr_t strCon("Provider=SQLOLEDB;Data Source=DEEPA\\SQLEXPRESS;Initial Catalog=keerth;User ID=sa;Password=tovya;Connect Timeout=30;");
    
         HRESULT hr = S_OK;
    	 //printf("connection string with all correct attributes");
    
         //Initialize the COM Library
         //CoInitialize(NULL);
    
         try
         {   
             //Create the Connection pointer
             hr = pConn.CreateInstance((__uuidof(Connection)));
    		// printf("\n connection and create instance.");
    
             if(FAILED(hr))
             {
                 printf("Error instantiating Connection object\n");
                 goto cleanup;
             }
    
             //Open the SQL Server connection
    		
             hr = pConn->Open(strCon,"sa","tovya",0);
    		 
             if(FAILED(hr))
             {
                  printf("Error Opening Database object using ADO _ConnectionPtr \n");
                  goto cleanup;
             }
    
             /*Initialize the values */
            strName = "'C++ADOinsert',";
            strAge = "23,";
    		strDOB = "'13/04/1988 12:0:0',";
            strSalary = "1660)";
    
            /* Append the values to the Insert Statement */
    		 //printf("\n connection with openString");
    		 _bstr_t strSQL("Insert into table1(NAMES,AGE,DOB,SALARY) Values(");
    
            strSQL += strName + strAge + strDOB + strSalary ;
    
            printf("&#37;s\n",(LPCSTR)strSQL);
    
            //Execute the insert statement
           pConn->Execute(strSQL,NULL,adExecuteNoRecords);
    
           printf("Data Added Successfully\n",(LPCSTR)strSQL);
    
          //Close the database
          pConn->Close();
    
         }
         catch(_com_error &ce)
         {
            printf("Error:%s\n",ce.ErrorInfo());
    		pConn->Close();
    
         }
        cleanup:
        CoUninitialize();
    
    }
    Last edited by ovidiucucu; December 22nd, 2011 at 04:47 AM. Reason: Added [CODE] tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Error while executing the insert query.

    Quote Originally Posted by keerth516 View Post
    Its an console window application.I am getting the error (unknown String)while running the below code.
    Please, provide the exact error message and the line where the error was detected.

    Quote Originally Posted by keerth516 View Post
    ... Kindly check the error and let me know where i have to change.
    Sorry, but it is almost impossible since you didn't use Code tags.
    Please, read Announcement: Before you post....
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2009
    Posts
    598

    Re: Error while executing the insert query.

    I guess the problem is with the date (DOB column), because the conversion, or lack of conversion, is a common problem with that kind of fields.
    Could you try without the date?

    Otherwise, enclose your lines of code between [code] and [/code] in your message. This will give a nice frame an indentations like
    Code:
     and

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Error while executing the insert query.

    [ shooting in the dark ] Make a UNICODE build, i.e. add UNICODE and _UNICODE to project preprocessor definitions.
    Although, I don't know that can solve your problem, just have a try.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Error while executing the insert query.

    Hi Ovidiu!
    I don"t think UNICODE build can help here. I use ADO in ANSI builds since years (if not a decade) without any problem.
    And, BTW, thank you for inserting Code tags into post of keerth516.

    Dear keerth516,
    the first thing you should do is forget that goto exists!
    There are (or were) some languages where there was impossible to program without goto, but neither C nor C++ belog to those languages. Besides you "cleanup" code does not make any sense because the best place for CoUninitialize is in the main() function *the same module that is used for CoInitialize)

    Second, did you debug you code? did you see what exactly your string strSQL contains while being passed in Execute method?
    Victor Nijegorodov

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Error while executing the insert query.

    Hi Victor,
    Indeed, should be not any problem ANSI vs. UNICODE build, as long as _bstr_t has botn char* and wchar_t* operators as well as constructors taking char* or wchar_t*, as well as...

    I was just "shooting in the dark", like said before.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    May 2011
    Posts
    27

    Re: Error while executing the insert query.

    Hi every one...thanks a lot for ur replies...

    It was an date format issues..thats why was getting such error....now it insert the values into database.

  8. #8
    Join Date
    May 2011
    Posts
    27

    Re: Error while executing the insert query.

    I have one more doubt reagrding insertion of values displayed in the console window.

    I have one application which is in C lanaguage.Am using microsoft visual studio VC++ to debug this application.It will gives the finaly ouput display in console window.I want to insert this displayed values into sqlserver2005.

    eg:Below line of code is the values displaying in console window.I want to insert the value of amout and area variable into sqlserver database.

    Could u please suggest me any idea to implement this.If u provide any example it would be helpful.

    printf("comp: &#37;s ammount %f area %u\n",(analysis->peak + i)->comp,(analysis->peak + i)->amount , (analysis->peak + i)->area);

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured