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
7 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; }

    C M 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; }

      C Offline
      C Offline
      Chaos Lawful
      wrote on last edited by
      #2

      You didn't delete the dlg object in the CFirstDialog::OnButtonTry(). That should be the problem. Why do you want to use the heap object rather than stack object? If you do it like this: void CFirstDialog::OnButtonTry() { CTestDialog dlg; dlg.DoModal(); } you won't be worried about the work of releasing object. I think this form is better.;) Law is meaningless without chaos. Chaos without Law is equal to destruction. Chaos and Law create our rich and colorful world.

      L 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; }

        M Offline
        M Offline
        mProject
        wrote on last edited by
        #3

        After seeing code, I think you are not destroying dialog (delete dlg). This should be your problem. CTestDialog* dlg; dlg=new CTestDialog; dlg->DoModal(); ////add line... delete dlg; :-O :-O :-O :rose: :rose: :rose: -------------------------------------------------- Say Whatever You Know. Helping other people is good for health. ========= Manish ========= ---------------------------------------------------

        L 1 Reply Last reply
        0
        • M mProject

          After seeing code, I think you are not destroying dialog (delete dlg). This should be your problem. CTestDialog* dlg; dlg=new CTestDialog; dlg->DoModal(); ////add line... delete dlg; :-O :-O :-O :rose: :rose: :rose: -------------------------------------------------- Say Whatever You Know. Helping other people is good for health. ========= Manish ========= ---------------------------------------------------

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

          The wrong still happened.

          1 Reply Last reply
          0
          • C Chaos Lawful

            You didn't delete the dlg object in the CFirstDialog::OnButtonTry(). That should be the problem. Why do you want to use the heap object rather than stack object? If you do it like this: void CFirstDialog::OnButtonTry() { CTestDialog dlg; dlg.DoModal(); } you won't be worried about the work of releasing object. I think this form is better.;) Law is meaningless without chaos. Chaos without Law is equal to destruction. Chaos and Law create our rich and colorful world.

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

            The wrong still happened,what should I do?

            C 1 Reply Last reply
            0
            • L LoveInSnowing

              The wrong still happened,what should I do?

              C Offline
              C Offline
              Chaos Lawful
              wrote on last edited by
              #6

              So could you send your piece of codes to me? I think looking into the codes will be more clear.:) Law is meaningless without chaos. Chaos without Law is equal to destruction. Chaos and Law create our rich and colorful world.

              L 1 Reply Last reply
              0
              • C Chaos Lawful

                So could you send your piece of codes to me? I think looking into the codes will be more clear.:) Law is meaningless without chaos. Chaos without Law is equal to destruction. Chaos and Law create our rich and colorful world.

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

                Thank you,under the help of others,I had solved the problem. Thanks again. A beginner of Visual C++,and need your help.

                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