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. MFC VC++ Repopulating the ClistCtrl after database is updated through dialog

MFC VC++ Repopulating the ClistCtrl after database is updated through dialog

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpdatabasedebuggingannouncement
18 Posts 4 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.
  • M Member_14575556

    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.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #8

    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.

    M 1 Reply Last reply
    0
    • M Member_14575556

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

      }

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #9

      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?

      M 1 Reply Last reply
      0
      • V Victor Nijegorodov

        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?

        M Offline
        M Offline
        Member_14575556
        wrote on last edited by
        #10

        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();
        

        }

        V 1 Reply Last reply
        0
        • L Lost User

          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.

          M Offline
          M Offline
          Member_14575556
          wrote on last edited by
          #11

          Yes I did that and still I couldn't fix it... Thanks anyway. I'll try harder.

          1 Reply Last reply
          0
          • M Member_14575556

            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();
            

            }

            V Offline
            V Offline
            Victor Nijegorodov
            wrote on last edited by
            #12

            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!

            M 1 Reply Last reply
            0
            • V Victor Nijegorodov

              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!

              M Offline
              M Offline
              Member_14575556
              wrote on last edited by
              #13

              I have included the header file of MedicineDlg (MedicineDlg.h)

              V 1 Reply Last reply
              0
              • M Member_14575556

                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 painting

                	SendMessage(WM\_ICONERASEBKGND, reinterpret\_cast(dc.GetSafeHdc()), 0);
                
                	// Center icon in client rectangle
                	int cxIcon = GetSystemMetrics(SM\_CXICON);
                
                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #14

                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 in OnBnClickedSelectButton().

                "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

                M 1 Reply Last reply
                0
                • D David Crow

                  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 in OnBnClickedSelectButton().

                  "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

                  M Offline
                  M Offline
                  Member_14575556
                  wrote on last edited by
                  #15

                  Please give some direction how to solves this problem. Thank you.

                  V D 2 Replies Last reply
                  0
                  • M Member_14575556

                    Please give some direction how to solves this problem. Thank you.

                    V Offline
                    V Offline
                    Victor Nijegorodov
                    wrote on last edited by
                    #16

                    You should redesign your project architecture to not directly access one dialog controls from another one.

                    1 Reply Last reply
                    0
                    • M Member_14575556

                      I have included the header file of MedicineDlg (MedicineDlg.h)

                      V Offline
                      V Offline
                      Victor Nijegorodov
                      wrote on last edited by
                      #17

                      It has nothing to do with your problem. See the answer of David below.

                      1 Reply Last reply
                      0
                      • M Member_14575556

                        Please give some direction how to solves this problem. Thank you.

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #18

                        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 item

                        seems 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

                        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