Thread: Problem in when I run insert query

  1. #1
    Registered User
    Join Date
    Jan 2007

    Problem in when I run insert query

    Hi !

    I am getting run time exception when I run insert query.

    Actually I am calling some database related function from my main CPP file.

    The code is as follow:
    void ABC::Insert_Database(char* filepath,int UserId,char *EmailService)
    			 int c,i=0;
    			 CDatabase *pdatabase=new CDatabase();
    			 char size[300];
    			 FILE* LogFile=NULL;
    			 int userid=3;
    			 string mailservice="yahoo";
    			 string emaildata="sasaa";
    			 string parsed="";
    			 string logpath="c:\dad";
    			 string sRecDate = pdatabase->GetDateTime();
    			 string logfileext="html";
    			string date="03/05/2006 03:34:44";
    			 char query[400];
    				sprintf(query, "insert into tblEmailService (uID , EmailService, logfileExtension,\
    							 EmailData, RecDate,  EmailLogPath, EmailDate) values ( %d,  '%s', '%s',  '%s',format(#%s#,\"mm/dd/yyyy hh:mm:ss\"), '%s', '%s')",\
    							 userid, mailservice.c_str(),logfileext.c_str(),\
    							 emaildata.c_str (), date.c_str (), logpath.c_str (), emaildate.c_str ());
    		 catch(_com_error &e)
    			 std::ofstream f1("C:\\error.txt",ios::app);
    //Database function related CPP file
    //connection  related function
    BOOL CDatabaseSingleton::Connect()
    		string DBPath =C:\\Database\\abc.mdb";
    		string Cnn = "Provider=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=";
    		Cnn+=";USER ID=admin;PASSWORD=;Jet OleDB:Database Password =...;";	
    				m_bIsDBConnected = true;
    				m_pCommandPtr->ActiveConnection = m_pDBConnect;
    		catch (_com_error &e)
    			m_bIsDBConnected = false;
    			_bstr_t err = e.Description  ();
    			string sError (err);
    			CActivityLoggerSingleton *pLogger = CActivityLoggerSingleton::CreateActivityLoggerInstance ();
    			pLogger->WriteLog ("CDatabaseSingleton::Connect ", sError.c_str ());
    			//string str = HandleError (e);
    	return m_bIsDBConnected;
    ///insert function
    BOOL CDatabase::Insert(CString sInsert)
    	BOOL bResult = FALSE;
    	CDatabase *Dis=new CDatabase();
    	_bstr_t bstrQuery (sInsert.GetBuffer (sInsert.GetLength ()));
    		variant_t vNull;
    		vNull.vt = VT_NULL;
    		vNull.scode = DISP_E_PARAMNOTFOUND;
    		m_pCommandPtr->CommandType = adCmdText;
    		m_pCommandPtr->CommandText = bstrQuery;
    		_RecordsetPtr pRecSet = m_pCommandPtr->Execute(&vNull, &vNull, adExecuteNoRecords);
    		bResult = true;
    	catch( _com_error &e )
    		_bstr_t err = e.Description  ();
    		string sError (err);
    		CActivityLoggerSingleton *pLogger = CActivityLoggerSingleton::CreateActivityLoggerInstance ();
    		pLogger->WriteLog ("CDatabaseSingleton::Insert ", sError.c_str ());
    		string sSQL (bstrQuery);
    		pLogger->WriteLog ("CDatabaseSingleton::Insert ",sSQL.c_str () );
    		bResult = false;
    This code successfully insert record but give unhandled exception.

    Please tell where I am wrong.


  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Hannover, Germany
    The first things that I noticed is that you use 4 different styles/libraries in your project. ANSI C, C++/STL, MFC and COM. The MFC could handle all the needs I see in your program. Casting and switching between those four leads to a lot of unneccessary casting and minor mistakes. None of them may be your problem right now, but each of them will come back to nag you later. To make your life way easier, decide on one style and one library. MFC for example would be fine.

    _bstr_t bstrQuery (sInsert.GetBuffer (sInsert.GetLength ()));

    string logpath="c:\dad";


    Those three lines don't do what you think they are doing. The result is close enough to what you expect to probably not be your problem now, but I cannot pinpoint your problem so my advise would be to clear up your code. Decide for one library and use it completly and clear up your formatting. "delete" everything you have "new"ed.

    For finding out where it crashes, use a debugger. As MFC is involved, I guess you might be using Visual Studio. Run your program in debug mode and follow the instructions in the messagebox to find the line of code the exception is generated.

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I have problem!!!
    By Dragon227Slayer in forum C# Programming
    Replies: 9
    Last Post: 01-21-2004, 03:49 AM
  2. overload insert operator problem
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 04:34 PM
  3. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Edit Control problem
    By Malek in forum Windows Programming
    Replies: 3
    Last Post: 06-16-2002, 01:12 AM