MFC VC++ Repopulating the ClistCtrl after database is updated through dialog
-
Below are my codes. I'm new to MFC VC++ and in learning process, pls excuse my coding standard. This is the Error I got: Exception thrown at 0x00007FFCFBE90CC3 (mfc140ud.dll) in Medication_Administration.exe: 0xC0000005: Access violation reading location 0x0000000000000040. If there is a handler for this exception, the program may be safely continued. 1st Dialog :
// Medication_AdministrationDlg.cpp : implementation file
//#include "stdafx.h"
#include "Medication_Administration.h"
#include "Medication_AdministrationDlg.h"
#include "afxdialogex.h"
#include "odbcinst.h"
#include "afxdb.h"
#include "MedicineDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif// CPatientDlg dialog
CPatientDlg::CPatientDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_MEDICATION_ADMINISTRATION_DIALOG, pParent)
, m_strSearch(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CPatientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PATIENT_LIST, m_ListControl);
DDX_Text(pDX, IDC_SEARCH_BOX, m_strSearch);
}BEGIN_MESSAGE_MAP(CPatientDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEARCH_BUTTON, &CPatientDlg::OnBnClickedSearchButton)
ON_BN_CLICKED(IDC_SELECT_BUTTON, &CPatientDlg::OnBnClickedSelectButton)
END_MESSAGE_MAP()// CPatientDlg message handlers
BOOL CPatientDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();// Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m\_hIcon, TRUE); // Set big icon SetIcon(m\_hIcon, FALSE); // Set small icon ShowWindow(SW\_MINIMIZE); // TODO: Add extra initialization here m\_strSearch = "Enter name or MRN to search"; UpdateData(FALSE); return TRUE; // return TRUE unless you set the focus to a control
}
void CPatientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{CDialogEx::OnSysCommand(nID, lParam);
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.void CPatientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for paintingSendMessage(WM\_ICONERASEBKGND, reinterpret\_cast(dc.GetSafeHdc()), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM\_CXICON);
-
Below are my codes. I'm new to MFC VC++ and in learning process, pls excuse my coding standard. This is the Error I got: Exception thrown at 0x00007FFCFBE90CC3 (mfc140ud.dll) in Medication_Administration.exe: 0xC0000005: Access violation reading location 0x0000000000000040. If there is a handler for this exception, the program may be safely continued. 1st Dialog :
// Medication_AdministrationDlg.cpp : implementation file
//#include "stdafx.h"
#include "Medication_Administration.h"
#include "Medication_AdministrationDlg.h"
#include "afxdialogex.h"
#include "odbcinst.h"
#include "afxdb.h"
#include "MedicineDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif// CPatientDlg dialog
CPatientDlg::CPatientDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_MEDICATION_ADMINISTRATION_DIALOG, pParent)
, m_strSearch(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CPatientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PATIENT_LIST, m_ListControl);
DDX_Text(pDX, IDC_SEARCH_BOX, m_strSearch);
}BEGIN_MESSAGE_MAP(CPatientDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEARCH_BUTTON, &CPatientDlg::OnBnClickedSearchButton)
ON_BN_CLICKED(IDC_SELECT_BUTTON, &CPatientDlg::OnBnClickedSelectButton)
END_MESSAGE_MAP()// CPatientDlg message handlers
BOOL CPatientDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();// Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m\_hIcon, TRUE); // Set big icon SetIcon(m\_hIcon, FALSE); // Set small icon ShowWindow(SW\_MINIMIZE); // TODO: Add extra initialization here m\_strSearch = "Enter name or MRN to search"; UpdateData(FALSE); return TRUE; // return TRUE unless you set the focus to a control
}
void CPatientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{CDialogEx::OnSysCommand(nID, lParam);
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.void CPatientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for paintingSendMessage(WM\_ICONERASEBKGND, reinterpret\_cast(dc.GetSafeHdc()), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM\_CXICON);
-
You need to show us where the exception was thrown. Use the debugger to run the program and check the stack windo when the exception occurs.
Thanks for replying. It when the below function is called.. I've marked the exact line.
void MedicationDlg::OnBnClickedSaveButtonMedicationDlg(){
..........
..........
MedicineObj.ResetListControl(); /* <-----when this function was called exception was thrown*/
MedicineObj.DatabaseReload();
OnOK();}
This functions is in 2nd dialog:
void MedicineDlg::ResetListControl() {
m_MedListControl.DeleteAllItems(); /* <----- This is the exact line when execute exceptions was thrown*/
int nColumnCount = m_MedListControl.GetHeaderCtrl()->GetItemCount();// Delete all of the columns. for (int i = 0; i < nColumnCount; i++) { m\_MedListControl.DeleteColumn(0); }
}
-
Thanks for replying. It when the below function is called.. I've marked the exact line.
void MedicationDlg::OnBnClickedSaveButtonMedicationDlg(){
..........
..........
MedicineObj.ResetListControl(); /* <-----when this function was called exception was thrown*/
MedicineObj.DatabaseReload();
OnOK();}
This functions is in 2nd dialog:
void MedicineDlg::ResetListControl() {
m_MedListControl.DeleteAllItems(); /* <----- This is the exact line when execute exceptions was thrown*/
int nColumnCount = m_MedListControl.GetHeaderCtrl()->GetItemCount();// Delete all of the columns. for (int i = 0; i < nColumnCount; i++) { m\_MedListControl.DeleteColumn(0); }
}
Does
m_MedListControl
have a non-null value?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Does
m_MedListControl
have a non-null value?"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Yes, what I did was in the 2nd dialog there will be a list view control already populated with the database data when a button is clicked in the 1st dialog. when I click one of the row in the 2nd dialog list view control, 3rd dialog will pop up where I'll enter some data which will be updated to the database and then I delete all the items in the 2nd dialog list view controlled and tries to repopulate again with the updated data in the database.
-
Yes, what I did was in the 2nd dialog there will be a list view control already populated with the database data when a button is clicked in the 1st dialog. when I click one of the row in the 2nd dialog list view control, 3rd dialog will pop up where I'll enter some data which will be updated to the database and then I delete all the items in the 2nd dialog list view controlled and tries to repopulate again with the updated data in the database.
-
Thanks for replying. It when the below function is called.. I've marked the exact line.
void MedicationDlg::OnBnClickedSaveButtonMedicationDlg(){
..........
..........
MedicineObj.ResetListControl(); /* <-----when this function was called exception was thrown*/
MedicineObj.DatabaseReload();
OnOK();}
This functions is in 2nd dialog:
void MedicineDlg::ResetListControl() {
m_MedListControl.DeleteAllItems(); /* <----- This is the exact line when execute exceptions was thrown*/
int nColumnCount = m_MedListControl.GetHeaderCtrl()->GetItemCount();// Delete all of the columns. for (int i = 0; i < nColumnCount; i++) { m\_MedListControl.DeleteColumn(0); }
}
Member 14575556 wrote:
void MedicationDlg::OnBnClickedSaveButtonMedicationDlg(){ .......... .......... MedicineObj.ResetListControl(); /* <-----when this function was called exception was thrown*/ MedicineObj.DatabaseReload(); OnOK(); }
What is this MedicineObj? Where and how is it defined?
-
Member 14575556 wrote:
void MedicationDlg::OnBnClickedSaveButtonMedicationDlg(){ .......... .......... MedicineObj.ResetListControl(); /* <-----when this function was called exception was thrown*/ MedicineObj.DatabaseReload(); OnOK(); }
What is this MedicineObj? Where and how is it defined?
MedicineDlg MedicineObj;
It is an object for MedicineDlg.
void MedicationDlg::OnBnClickedSaveButtonMedicationDlg()
{
// TODO: Add your control notification handler code here
CString m_MedStatusInput, m_MedCommentInput, m_MedDoseInput, m_MedRouteInput, SqlQuery;
m_MedDose_MedicationDlg.GetLBText(m_MedDose_MedicationDlg.GetCurSel(), m_MedDoseInput);
m_MedRoute_MedicationDlg.GetLBText(m_MedRoute_MedicationDlg.GetCurSel(), m_MedRouteInput);
UpdateData(TRUE);
m_MedStatusInput = m_MedStatus_MedicationDlg;
m_MedCommentInput = m_MedComments_MedicationDlg;CDatabase dbobj; dbobj.OpenEx(\_T("DSN=Demo;UID=root;PWD=root")); CRecordset recset(&dbobj); SqlQuery.Format(\_T("UPDATE dbdemo.medicine SET Route = '%s', Dose = '%s',Status = '%s',Comment = '%s' WHERE NDC ='%s'"), m\_MedRouteInput, m\_MedDoseInput, m\_MedStatusInput, m\_MedCommentInput, m\_MedDSNInput); dbobj.ExecuteSQL(SqlQuery);//till here its working MedicineDlg MedicineObj; MedicineObj.ResetListControl();// Here the code breaks MedicineObj.DatabaseReload(); OnOK();
}
-
No, that is what you think will happen. The only way to be certain is by using the debugger to check exactly what bad address is causing the exception. And no one hare can do that for you.
Yes I did that and still I couldn't fix it... Thanks anyway. I'll try harder.
-
MedicineDlg MedicineObj;
It is an object for MedicineDlg.
void MedicationDlg::OnBnClickedSaveButtonMedicationDlg()
{
// TODO: Add your control notification handler code here
CString m_MedStatusInput, m_MedCommentInput, m_MedDoseInput, m_MedRouteInput, SqlQuery;
m_MedDose_MedicationDlg.GetLBText(m_MedDose_MedicationDlg.GetCurSel(), m_MedDoseInput);
m_MedRoute_MedicationDlg.GetLBText(m_MedRoute_MedicationDlg.GetCurSel(), m_MedRouteInput);
UpdateData(TRUE);
m_MedStatusInput = m_MedStatus_MedicationDlg;
m_MedCommentInput = m_MedComments_MedicationDlg;CDatabase dbobj; dbobj.OpenEx(\_T("DSN=Demo;UID=root;PWD=root")); CRecordset recset(&dbobj); SqlQuery.Format(\_T("UPDATE dbdemo.medicine SET Route = '%s', Dose = '%s',Status = '%s',Comment = '%s' WHERE NDC ='%s'"), m\_MedRouteInput, m\_MedDoseInput, m\_MedStatusInput, m\_MedCommentInput, m\_MedDSNInput); dbobj.ExecuteSQL(SqlQuery);//till here its working MedicineDlg MedicineObj; MedicineObj.ResetListControl();// Here the code breaks MedicineObj.DatabaseReload(); OnOK();
}
Member 14575556 wrote:
MedicineDlg MedicineObj; MedicineObj.ResetListControl();// Here the code breaks
It is some "empty", i.e. not created/initialized object that has nothing to do with a really displayed dialog!
-
Member 14575556 wrote:
MedicineDlg MedicineObj; MedicineObj.ResetListControl();// Here the code breaks
It is some "empty", i.e. not created/initialized object that has nothing to do with a really displayed dialog!
I have included the header file of MedicineDlg (MedicineDlg.h)
-
Below are my codes. I'm new to MFC VC++ and in learning process, pls excuse my coding standard. This is the Error I got: Exception thrown at 0x00007FFCFBE90CC3 (mfc140ud.dll) in Medication_Administration.exe: 0xC0000005: Access violation reading location 0x0000000000000040. If there is a handler for this exception, the program may be safely continued. 1st Dialog :
// Medication_AdministrationDlg.cpp : implementation file
//#include "stdafx.h"
#include "Medication_Administration.h"
#include "Medication_AdministrationDlg.h"
#include "afxdialogex.h"
#include "odbcinst.h"
#include "afxdb.h"
#include "MedicineDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif// CPatientDlg dialog
CPatientDlg::CPatientDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_MEDICATION_ADMINISTRATION_DIALOG, pParent)
, m_strSearch(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CPatientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PATIENT_LIST, m_ListControl);
DDX_Text(pDX, IDC_SEARCH_BOX, m_strSearch);
}BEGIN_MESSAGE_MAP(CPatientDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SEARCH_BUTTON, &CPatientDlg::OnBnClickedSearchButton)
ON_BN_CLICKED(IDC_SELECT_BUTTON, &CPatientDlg::OnBnClickedSelectButton)
END_MESSAGE_MAP()// CPatientDlg message handlers
BOOL CPatientDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();// Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m\_hIcon, TRUE); // Set big icon SetIcon(m\_hIcon, FALSE); // Set small icon ShowWindow(SW\_MINIMIZE); // TODO: Add extra initialization here m\_strSearch = "Enter name or MRN to search"; UpdateData(FALSE); return TRUE; // return TRUE unless you set the focus to a control
}
void CPatientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{CDialogEx::OnSysCommand(nID, lParam);
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.void CPatientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for paintingSendMessage(WM\_ICONERASEBKGND, reinterpret\_cast(dc.GetSafeHdc()), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM\_CXICON);
Member 14575556 wrote:
MedicineDlg MedicineObj; MedicineObj.ResetListControl();// Here the code breaks MedicineObj.DatabaseReload();
This will not work as it is not the same instance of
MedicineDlg
that was created inOnBnClickedSelectButton()
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Member 14575556 wrote:
MedicineDlg MedicineObj; MedicineObj.ResetListControl();// Here the code breaks MedicineObj.DatabaseReload();
This will not work as it is not the same instance of
MedicineDlg
that was created inOnBnClickedSelectButton()
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Please give some direction how to solves this problem. Thank you.
-
Please give some direction how to solves this problem. Thank you.
You should redesign your project architecture to not directly access one dialog controls from another one.
-
I have included the header file of MedicineDlg (MedicineDlg.h)
It has nothing to do with your problem. See the answer of David below.
-
Please give some direction how to solves this problem. Thank you.
I would be inclined to change how the data was presented. What you have:
List of data
+---Details of an item in the list
+---Editable view of that itemseems a bit convoluted. I just see too many places for errors, and possibly maintainability. At a minimum, I would combine the last two items. That would go a long way toward solving your immediate problem.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles