What's wrong with my code??
-
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; }
-
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; }
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)));
-
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)));
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.
-
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; }
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.
-
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.
Do you need to end a statement with a ';' too? as in,
pCommand->CommandText = _bstr_t("SELECT `tblImages`.* FROM `tblImages` WHERE `tblImages`.`visitID` = `param1`;");
-
Do you need to end a statement with a ';' too? as in,
pCommand->CommandText = _bstr_t("SELECT `tblImages`.* FROM `tblImages` WHERE `tblImages`.`visitID` = `param1`;");
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.
-
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.
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?
-
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?
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.
-
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.
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.:)
-
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.:)
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.