Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. What's wrong with my code??

What's wrong with my code??

Scheduled Pinned Locked Moved C / C++ / MFC
databasegraphicsquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LoveInSnowing
    wrote on last edited by
    #1

    I had written a dialog based application. It contained a button named "Try" When I clicked the "Try" button,opened an other dialog named "Test" which contained a button named "Open". When I first clicked "Open" button and clicked the "OK" button in Open File Dialog,everything is ok,and then close the "Test" Dialog. But When I click "Try" button again,it showed me wrong. Who can please tell me what's wrong with my code?? ****************************************************************** void CFirstDlg::OnButtonTry() { // TODO: Add your control notification handler code here CTestDialog* dlg; dlg=new CTestDialog; dlg->DoModal(); } ****************************************************************** BOOL CTestDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CString sCon; _ConnectionPtr m_pCon; _RecordsetPtr m_pRs; sCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=main.mdb"; CoInitialize (NULL); HRESULT hr=S_OK; try { hr=m_pCon.CreateInstance(__uuidof(Connection)); if(FAILED(m_pCon->Open(_bstr_t(sCon),"","",adModeUnknown))) AfxMessageBox("Can not open the database!"); } catch(_com_error e) { CString errormessage; errormessage.Format("Failed!\r\nError Message:%s",e.ErrorMessage()); AfxMessageBox(errormessage); } m_pRs.CreateInstance("ADODB.Recordset"); if(FAILED(m_pRs->Open("SELECT * FROM employeer",_variant_t((IDispatch*)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText))) AfxMessageBox("Can not open the record set!"); if((m_pRs->State & adStateOpen) == adStateOpen) m_pRs->Close(); if ( (m_pCon->State & adStateOpen) == adStateOpen) m_pCon->Close(); CoUninitialize(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } *********************************************************************** void CTestDialog::OnButtonOpen() { // TODO: Add your control notification handler code here static char BASED_CODE szFilter[]="Bitmap Files (*.bmp)|*.bmp||"; CFileDialog *fd; fd=new CFileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFilter,this); if(IDOK!=fd->DoModal()) return; else AfxMessageBox(fd->GetFileName()); delete fd; }

    B C 2 Replies Last reply
    0
    • L LoveInSnowing

      I had written a dialog based application. It contained a button named "Try" When I clicked the "Try" button,opened an other dialog named "Test" which contained a button named "Open". When I first clicked "Open" button and clicked the "OK" button in Open File Dialog,everything is ok,and then close the "Test" Dialog. But When I click "Try" button again,it showed me wrong. Who can please tell me what's wrong with my code?? ****************************************************************** void CFirstDlg::OnButtonTry() { // TODO: Add your control notification handler code here CTestDialog* dlg; dlg=new CTestDialog; dlg->DoModal(); } ****************************************************************** BOOL CTestDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CString sCon; _ConnectionPtr m_pCon; _RecordsetPtr m_pRs; sCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=main.mdb"; CoInitialize (NULL); HRESULT hr=S_OK; try { hr=m_pCon.CreateInstance(__uuidof(Connection)); if(FAILED(m_pCon->Open(_bstr_t(sCon),"","",adModeUnknown))) AfxMessageBox("Can not open the database!"); } catch(_com_error e) { CString errormessage; errormessage.Format("Failed!\r\nError Message:%s",e.ErrorMessage()); AfxMessageBox(errormessage); } m_pRs.CreateInstance("ADODB.Recordset"); if(FAILED(m_pRs->Open("SELECT * FROM employeer",_variant_t((IDispatch*)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText))) AfxMessageBox("Can not open the record set!"); if((m_pRs->State & adStateOpen) == adStateOpen) m_pRs->Close(); if ( (m_pCon->State & adStateOpen) == adStateOpen) m_pCon->Close(); CoUninitialize(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } *********************************************************************** void CTestDialog::OnButtonOpen() { // TODO: Add your control notification handler code here static char BASED_CODE szFilter[]="Bitmap Files (*.bmp)|*.bmp||"; CFileDialog *fd; fd=new CFileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFilter,this); if(IDOK!=fd->DoModal()) return; else AfxMessageBox(fd->GetFileName()); delete fd; }

      B Offline
      B Offline
      Brendan Tregear
      wrote on last edited by
      #2

      at a quick glance, you're not deleting the memory you allocate for CTestDialog. Why not use: void CFirstDlg::OnButtonTry() { CTestDialog dlg; dlg.DoModal(); } Also you're not freeing memory in your OnButtonOpen() if the dialog doesn't return IDOK. What is the error message, is it a com exception or unhandled exception? I'd also consider setting all com pointers to null before calling CreateInstance. eg. pConnection = NULL; TESTHR(pConnection.CreateInstance(__uuidof(Connection)));

      L 1 Reply Last reply
      0
      • B Brendan Tregear

        at a quick glance, you're not deleting the memory you allocate for CTestDialog. Why not use: void CFirstDlg::OnButtonTry() { CTestDialog dlg; dlg.DoModal(); } Also you're not freeing memory in your OnButtonOpen() if the dialog doesn't return IDOK. What is the error message, is it a com exception or unhandled exception? I'd also consider setting all com pointers to null before calling CreateInstance. eg. pConnection = NULL; TESTHR(pConnection.CreateInstance(__uuidof(Connection)));

        L Offline
        L Offline
        LoveInSnowing
        wrote on last edited by
        #3

        Because I am a beginner of VC++ from China,and the error message was showed in Chinese,I only know the error number is 0x80004005,I don't know what is means?Please Help me.

        1 Reply Last reply
        0
        • L LoveInSnowing

          I had written a dialog based application. It contained a button named "Try" When I clicked the "Try" button,opened an other dialog named "Test" which contained a button named "Open". When I first clicked "Open" button and clicked the "OK" button in Open File Dialog,everything is ok,and then close the "Test" Dialog. But When I click "Try" button again,it showed me wrong. Who can please tell me what's wrong with my code?? ****************************************************************** void CFirstDlg::OnButtonTry() { // TODO: Add your control notification handler code here CTestDialog* dlg; dlg=new CTestDialog; dlg->DoModal(); } ****************************************************************** BOOL CTestDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CString sCon; _ConnectionPtr m_pCon; _RecordsetPtr m_pRs; sCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=main.mdb"; CoInitialize (NULL); HRESULT hr=S_OK; try { hr=m_pCon.CreateInstance(__uuidof(Connection)); if(FAILED(m_pCon->Open(_bstr_t(sCon),"","",adModeUnknown))) AfxMessageBox("Can not open the database!"); } catch(_com_error e) { CString errormessage; errormessage.Format("Failed!\r\nError Message:%s",e.ErrorMessage()); AfxMessageBox(errormessage); } m_pRs.CreateInstance("ADODB.Recordset"); if(FAILED(m_pRs->Open("SELECT * FROM employeer",_variant_t((IDispatch*)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText))) AfxMessageBox("Can not open the record set!"); if((m_pRs->State & adStateOpen) == adStateOpen) m_pRs->Close(); if ( (m_pCon->State & adStateOpen) == adStateOpen) m_pCon->Close(); CoUninitialize(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } *********************************************************************** void CTestDialog::OnButtonOpen() { // TODO: Add your control notification handler code here static char BASED_CODE szFilter[]="Bitmap Files (*.bmp)|*.bmp||"; CFileDialog *fd; fd=new CFileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFilter,this); if(IDOK!=fd->DoModal()) return; else AfxMessageBox(fd->GetFileName()); delete fd; }

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          I've been playing with ADO a bit lately and I've found that if you pass in a bad string, it crashes, and crashes HARD.

          if(FAILED(m_pRs->Open("SELECT * FROM employeer",_variant_t((IDispatch*)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText)))

          Is 'employer' spelled wrong in the database as well ? If not, a typo here would cause a crash every time, and not a graceful or easily traceable one. I'd also suggest it is poor form to name local variables m_, that indicates a member variable ( one declared in your header file and therefore visible to the entire class ). Christian As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet. Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.

          B L 2 Replies Last reply
          0
          • C Christian Graus

            I've been playing with ADO a bit lately and I've found that if you pass in a bad string, it crashes, and crashes HARD.

            if(FAILED(m_pRs->Open("SELECT * FROM employeer",_variant_t((IDispatch*)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText)))

            Is 'employer' spelled wrong in the database as well ? If not, a typo here would cause a crash every time, and not a graceful or easily traceable one. I'd also suggest it is poor form to name local variables m_, that indicates a member variable ( one declared in your header file and therefore visible to the entire class ). Christian As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet. Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.

            B Offline
            B Offline
            Brendan Tregear
            wrote on last edited by
            #5

            Do you need to end a statement with a ';' too? as in, pCommand->CommandText = _bstr_t("SELECT `tblImages`.* FROM `tblImages` WHERE `tblImages`.`visitID` = `param1`;");

            C 1 Reply Last reply
            0
            • B Brendan Tregear

              Do you need to end a statement with a ';' too? as in, pCommand->CommandText = _bstr_t("SELECT `tblImages`.* FROM `tblImages` WHERE `tblImages`.`visitID` = `param1`;");

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              No, I don't believe so ( the code is at home so I cannot check, but I don't think SQL wants a ; ). Christian As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet. Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.

              1 Reply Last reply
              0
              • C Christian Graus

                I've been playing with ADO a bit lately and I've found that if you pass in a bad string, it crashes, and crashes HARD.

                if(FAILED(m_pRs->Open("SELECT * FROM employeer",_variant_t((IDispatch*)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText)))

                Is 'employer' spelled wrong in the database as well ? If not, a typo here would cause a crash every time, and not a graceful or easily traceable one. I'd also suggest it is poor form to name local variables m_, that indicates a member variable ( one declared in your header file and therefore visible to the entire class ). Christian As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet. Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.

                L Offline
                L Offline
                LoveInSnowing
                wrote on last edited by
                #7

                No,I means when I click the "Try" button again,the wrong message appeared,while when I first clicked the "Try" button,everything is OK. I tried to change my code with an other way such as: CFirstDialog::OnButtonTry() { CTestDialog dlg; dlg.DoModal(); } It's still wrong. Because I use Chinese,so the error message was showed in Chinese,I only know the error number is 0x80004005,what it means?

                C 1 Reply Last reply
                0
                • L LoveInSnowing

                  No,I means when I click the "Try" button again,the wrong message appeared,while when I first clicked the "Try" button,everything is OK. I tried to change my code with an other way such as: CFirstDialog::OnButtonTry() { CTestDialog dlg; dlg.DoModal(); } It's still wrong. Because I use Chinese,so the error message was showed in Chinese,I only know the error number is 0x80004005,what it means?

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  The following code will translate an error number for you

                  LPVOID lpMsgBuf;
                  FormatMessage( 
                  	FORMAT\_MESSAGE\_ALLOCATE\_BUFFER | 
                  	FORMAT\_MESSAGE\_FROM\_SYSTEM | 
                  	FORMAT\_MESSAGE\_IGNORE\_INSERTS,
                  	NULL,
                  	0x80004005,  // the error number
                  	0, // Default language
                  	(LPTSTR) &lpMsgBuf,
                  	0,
                  	NULL 
                  );
                  // Process any inserts in lpMsgBuf.
                  // ...
                  // Display the string.
                  MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB\_OK | MB\_ICONINFORMATION );
                  // Free the buffer.
                  LocalFree( lpMsgBuf );
                  

                  In this case it is 'unspecified error', which is what ADO always seems to give ( but it could well come from elsewhere all the same ). Changing your code as you have done is obviously a step forward, no need for a pointer, plus an ugly memory leak. You should do the same for your FileDialog, or at least delete the thing if people press cancel. Are you trying to run in F5 ( debug ) mode ? Are you pressing cancel and letting the debugger show you where it is crashing ? If the above code is crashing ( just a domodal() call, then it's dying in the dialog code, and I would continue to maintain in your ADO code. You should set a break point and step through the OnInitDialog as a first step. Are you closing your connection to the database ? It may well be crashing the second time because your first connection is still open. Also your database path is not absolute, if something else in the dialog is changing the current directory, that will also mean it cannot find the file and will crash. Christian As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet. Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.

                  L 1 Reply Last reply
                  0
                  • C Christian Graus

                    The following code will translate an error number for you

                    LPVOID lpMsgBuf;
                    FormatMessage( 
                    	FORMAT\_MESSAGE\_ALLOCATE\_BUFFER | 
                    	FORMAT\_MESSAGE\_FROM\_SYSTEM | 
                    	FORMAT\_MESSAGE\_IGNORE\_INSERTS,
                    	NULL,
                    	0x80004005,  // the error number
                    	0, // Default language
                    	(LPTSTR) &lpMsgBuf,
                    	0,
                    	NULL 
                    );
                    // Process any inserts in lpMsgBuf.
                    // ...
                    // Display the string.
                    MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB\_OK | MB\_ICONINFORMATION );
                    // Free the buffer.
                    LocalFree( lpMsgBuf );
                    

                    In this case it is 'unspecified error', which is what ADO always seems to give ( but it could well come from elsewhere all the same ). Changing your code as you have done is obviously a step forward, no need for a pointer, plus an ugly memory leak. You should do the same for your FileDialog, or at least delete the thing if people press cancel. Are you trying to run in F5 ( debug ) mode ? Are you pressing cancel and letting the debugger show you where it is crashing ? If the above code is crashing ( just a domodal() call, then it's dying in the dialog code, and I would continue to maintain in your ADO code. You should set a break point and step through the OnInitDialog as a first step. Are you closing your connection to the database ? It may well be crashing the second time because your first connection is still open. Also your database path is not absolute, if something else in the dialog is changing the current directory, that will also mean it cannot find the file and will crash. Christian As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet. Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.

                    L Offline
                    L Offline
                    LoveInSnowing
                    wrote on last edited by
                    #9

                    Thanks,you're right. When I open the "Open/Save File Dialog",it changed the current directory. It cannot find the database,so the wrong message appeared. Thanks again.:)

                    C 1 Reply Last reply
                    0
                    • L LoveInSnowing

                      Thanks,you're right. When I open the "Open/Save File Dialog",it changed the current directory. It cannot find the database,so the wrong message appeared. Thanks again.:)

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      Glad to help ;) ADO was a nightmare for me in the first little while because it crashes so easily and my books are filled with examples which simply do not work. Christian As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet. Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups