Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
168
Добавлен:
24.05.2014
Размер:
25.78 Mб
Скачать

220 Project 1 DATABASE PROGRAMMING USING VC++.NET

Now, you need to create the common function that will display the previous record, whether the user chooses the button or the menu option. To do this, open the menu IDR_MAINFRAME. Right-click Record, Previous Record and choose Add Event Handler. Add the function to the view class.

void CBankOperationView::OnRecordPrev()

{

Y

 

//The variable is set to store a number less than the current value.

m_nCurrentRecord—;

 

F

{

 

 

//Check for the BOF

 

 

M

if(m_pSet->IsBOF())

 

L

 

 

A

 

E

AfxMessageBox(“ ttempt to scroll beyond First Record”);

T

 

return;

 

 

 

}

//Move the recordset pointer to the previous record and refresh the view. m_pSet->MovePrev();

UpdateData(false);

//Code to disable and enable the controls

}

While moving the pointer backward, you need to perform one vital check — whether the pointer is pointing to the beginning of the file. You do this by using the IsBOF() function, which will return True if BOF is reached. So, if the return value is True, then display an appropriate message and cancel the move.

Next, you need to add the functionality for the > button. This button is used to move to the next record. Follow the same steps that you performed for adding the functionality to move to the first and the previous record. Specify the event handler’s name as OnBnClickedNext. Click Add and Edit to add the following code:

void CBankOperationView::OnBnClickedNext()

{

OnRecordNext();

Team-Fly®

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

221

 

 

 

EnableDisableNavigationKeys(4);

}

As before, you need to code the common function that will allow the user to navigate to the next record both from the menu and by clicking the button. Open the menu IDR_MAINFRAME. Right-click Record, Next Record and choose Add Event Handler. Add the function to the view class.

void CBankOperationView::OnRecordNext()

{

//The variable is set to store a number more than the current value. m_nCurrentRecord++;

//Check for the EOF if(m_pSet->IsEOF())

{

AfxMessageBox(“Attempt to scroll beyond Last Record”); return;

}

//Move the recordset pointer to the next record and refresh the view. m_pSet->MoveNext();

UpdateData(false);

//Code for enabling and disabling the controls

}

While moving the pointer forward, you need to perform one vital check — whether the pointer is pointing to the end of the file. You do this by using the IsEOF() function, which will return True if EOF is reached. So, if the return value is True, then display an appropriate message and cancel the move.

The last navigation function that you need to code is for the >> button. This button is used to move to the last record. Follow the same steps that you performed for adding the functionality to move to the first, previous, and next record. Specify the event handler’s name as OnBnClickedLast. Click Add and Edit to add the following code:

222 Project 1 DATABASE PROGRAMMING USING VC++.NET

void CBankOperationView::OnBnClickedLast()

{

OnRecordLast();

EnableDisableNavigationKeys(4);

}

To code the common function, open the menu, IDR_MAINFRAME, and right-click Records, Last Record. From the menu, choose Add Event Handler. Add the function to the view class.

void CBankOperationView::OnRecordLast()

{

//Checking for the availability of the recordset class. If the recordset is closed, you will open it else check for the BOF and EOF status. Based on the return value, you will move the pointer to the first record.

if(!m_pSet->IsOpen()) m_pSet->Open();

if(!m_pSet->IsBOF() && m_pSet->IsEOF ())

{

m_pSet->MoveFirst();

}

//Counting the total number of records int nTotalRecordCount=0;

while (!m_pSet->IsEOF())

{

nTotalRecordCount++; m_pSet->MoveNext();

}

//Initializing the m_CurrentRecord variable to store the total number of //records. m_nCurrentRecord = nTotalRecordCount;

//Moving to the last record and refreshing the view. m_pSet->MoveLast();

UpdateData(false);

//Code to disable and enable other controls

}

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

223

 

 

 

Your application now has the navigation feature operational. Next, you will proceed to add the code for the Add button.

The Add Option

When a customer wants to open an account, he or she has to provide details, such as first and last name, date of birth, address, and type of account. The application should provide the facility to easily accept all such required information and store it in the Account_Detail table in the back end. There is a set of steps that is commonly performed in any type of “add” operation, be it a banking application or an airlines application. Some of these steps include:

Autogeneration of the key field, the primary key in the back end.

Validations, such as ensuring that the user enters values in all mandatory fields and determining which fields can be left empty.

Updating the table with the newly entered record.

The functionality to be provided for the Add option of this Banking application is as follows:

When the user clicks on the Add button, the account number should be generated automatically and displayed in the corresponding edit control. Additionally, this control should be disabled so that the user doesn’t tamper with the value.

All other controls should be empty. The user will enter the new values into the appropriate controls. Besides this, the Add and Modify buttons are to be disabled until the current record is either saved or cancelled.

Once all values are entered, the user needs to click on the Update button. In the OnBnClicked method of the Update button, perform the required validations and add the record to the table.

Finally, once the update is done, enable the Add and Modify buttons and disable the Update button.

I will next provide the code snippet for handling the Add and Update buttons. Before that, you need to create the event handler method corresponding to the Add button’s Click event. To do so, use the Event Handler Wizard. The message type trapped by default is BN_CLICKED. Change the event handler’s name to OnAdd and click on the Add and Edit button. Then, add the following code to it:

224 Project 1 DATABASE PROGRAMMING USING VC++.NET

void CBankOperationView::OnSave()

{

//Obtain the pointer to the combo box control storing the account numbers

CComboBox *cmb=(CComboBox*)GetDlgItem(IDC_ACCNO);

//Retrieve the last item in the list

long lAccountNum = cmb->GetCount();

//10000 is the seed for Account number and the lAccountNum stores the newly //generated account number

lAccountNum = 10000 + lAccountNum;

 

//Disable

the account number edit control as it

will store the auto generated

//account

number and the user shouldn’t be able

to edit it.

cmb->EnableWindow(FALSE);

m_bAddNewAccount = TRUE;

//Code to format the generated account number and set it as the current //selection in the combo box.

CString strAccNum; strAccNum.Format(“%ld”, lAccountNum); SetDlgItemText(IDC_ACCNO, strAccNum);

//Enabling and disabling controls

CButton *pAdd, *pMod, *pUpd, *pCancel, *pDel, *pReActivate, *pDep, *pTransfer;

pAdd=(CButton*)GetDlgItem(IDC_ADD); pAdd->EnableWindow(false); pMod=(CButton*)GetDlgItem(IDC_MOD); pMod->EnableWindow(false); pUpd=(CButton*)GetDlgItem(IDC_UPD); pUpd->EnableWindow(true); pCancel=(CButton*)GetDlgItem(IDC_CANCEL1); pCancel->EnableWindow(true); pDel=(CButton*)GetDlgItem(IDC_DELETE); pDel->EnableWindow(false); pReActivate=(CButton*)GetDlgItem(IDC_REACTIVATE); pReActivate->EnableWindow(false);

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

225

 

 

 

pDep=(CButton*)GetDlgItem(IDC_DEPOSIT); pDep->EnableWindow(false); pTransfer=(CButton*)GetDlgItem(IDC_TRANSFER); pTransfer->EnableWindow(false);

//Code to clear the other controls

SetDlgItemText(IDC_FNAME, “”);

SetDlgItemText(IDC_LNAME, “”);

SetDlgItemText(IDC_DOB, “”);

SetDlgItemText(IDC_PADD, “”);

SetDlgItemText(IDC_MADD, “”);

SetDlgItemText(IDC_PHONE, “”);

SetDlgItemText(IDC_EMAIL, “”);

SetDlgItemInt(IDC_REFACCNO,0,false);

SetDlgItemInt(IDC_AMOUNT,0,false);

CButton* pChk1=(CButton*)GetDlgItem(IDC_CHECK1); pChk1->SetCheck(0);

//Code for performing the required validation and for updating the database will be added to the the Update button’s Click event.

}

The following list describes the flow of control and the role of the Add button:

The user clicks on the Add button to create a new account. The control passes to the OnAdd method.

In the OnAdd method, the first task that is performed is the generation of the account number. The generated account number is set in the edit box and the control is disabled.

Once the number is generated, the Add and Modify buttons are disabled. Only Update and Cancel buttons are enabled.

Then, all other controls are cleared and the interface is made ready for user input.

After the user enters the required data, he or she has to click on the Update button. I will provide the code listing for the Update button later. Before that, let me discuss the functionality implemented in the Modify button.

226 Project 1 DATABASE PROGRAMMING USING VC++.NET

The Modify Button

When the user clicks on the Modify button, all controls, except for permanent address, mailing address, phone number, and e-mail ID, are disabled. This indicates that a user can edit only these fields. After editing the values, the user has to click on the Update button to save the changes. Again, the Update button handles the elementary validations and updates the database. You need to again trap the Click event of the Modify button. Follow the same steps as you did for creating the OnSave method. Name the method OnModify. Click on the Add and Edit button and add the following code:

void CBankOperationView::OnMod()

{

//Enable the Update and Cancel buttons

CButton *b1, *pCancel; b1=(CButton*)GetDlgItem(IDC_UPD); b1->EnableWindow(true); pCancel=(CButton*)GetDlgItem(IDC_CANCEL1); pCancel->EnableWindow(true);

//Disabling the fields that are not editable

CEdit *pFName,*pLName,*pDOB,*pRefAcc,*pAmount;

pFName=(CEdit*)GetDlgItem(IDC_FNAME); pFName->EnableWindow(false);

pLName=(CEdit*)GetDlgItem(IDC_LNAME); pLName->EnableWindow(false);

pDOB=(CEdit*)GetDlgItem(IDC_DOB); pDOB->EnableWindow(false);

pRefAcc=(CEdit*)GetDlgItem(IDC_REFACCNO); pRefAcc->EnableWindow(false);

pAmount=(CEdit*)GetDlgItem(IDC_AMOUNT); pAmount->EnableWindow(false);

CComboBox *pcmbAcc;

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

227

 

 

 

pcmbAcc=(CComboBox*)GetDlgItem(IDC_ACCNO); pcmbAcc->EnableWindow(false);

CButton *pAdd, *pMod, *pDel, *pReActivate, *pDep, *pTransfer; pAdd=(CButton*)GetDlgItem(IDC_SAVE); pAdd->EnableWindow(false); pMod=(CButton*)GetDlgItem(IDC_MOD); pMod->EnableWindow(false); pDel=(CButton*)GetDlgItem(IDC_DELETE); pDel->EnableWindow(false); pReActivate=(CButton*)GetDlgItem(IDC_REACTIVATE); pReActivate->EnableWindow(false); pDep=(CButton*)GetDlgItem(IDC_DEPOSIT); pDep->EnableWindow(false); pTransfer=(CButton*)GetDlgItem(IDC_TRANSFER); pTransfer->EnableWindow(false);

}

The Update Button

The Update button encompasses the code for updating the database with either the new record or with the edited values. In addition, all validations are done before updating the database. To accomplish these tasks, you will have to add a message handler for trapping the Click event of the Update button. Follow the same steps as you did for creating the OnSave method. Name the method OnUpdate. Click on the Add and Edit button and add the following code to it:

void CBankOperationView::OnUpd()

{

//Check whether the recordset is open. If not open the recordset; otherwise, move the recordset pointer to the first record.if(!m_pRef->IsOpen())

m_pRef->Open(); if(m_pRef->IsBOF() && m_pRef->IsEOF ())

AfxMessageBox(“No Records in the Database...”); m_pRef->MoveFirst();

//Retrieve the account number into a variable and check for its existence // in the database.

long lAcctNumToEdit = m_pSet->m_Account_Number;

228 Project 1 DATABASE PROGRAMMING USING VC++.NET

if(!m_pSet->IsOpen()) m_pSet->Open();

if(m_pSet->IsBOF() && m_pSet->IsEOF ()) AfxMessageBox(“No Records in the Database...”);

m_pSet->MoveFirst();

//Check for the referral account number bool flag=false;

CString strRefAccNum;

GetDlgItemText(IDC_REFACCNO, strRefAccNum);

CEdit *pRefAcc = (CEdit*)GetDlgItem(IDC_REFACCNO); if (atoi(strRefAccNum) != 0 )

{

while(!m_pRef->IsEOF())

{

if(atoi(strRefAccNum) == m_pRef->m_Account_Number)

{

if(m_pRef->m_Valid_Acc==”CA”)

{

AfxMessageBox(“UnOperational Account - Give Another

Number”);

flag=false;

pRefAcc->SetFocus();

return;

}

flag=true;

break;

}

m_pRef->MoveNext();

}

if(m_pRef->IsEOF() && flag==false)

{

AfxMessageBox(“Invalid Account Number: Please enter valid account number”);

pRefAcc->SetFocus();

return;

}

}

IMPLEMENTING DATA ACCESS TECHNOLOGIES

Chapter 8

229

//Retrieving the amount

 

 

 

 

CEdit* pAmount=(CEdit*)GetDlgItem(IDC_AMOUNT);

 

 

CString strAmt;

 

 

pAmount->GetWindowText(strAmt);

 

 

long lAmt = atoi(strAmt);

 

 

strAmt.Format(“%ld”, lAmt);

 

 

pAmount->SetWindowText(strAmt);

 

 

//Retrieving the fname and lname and validating whether fields are empty

CString strFName, strLName, strDOB, strPADD;

GetDlgItemText(IDC_FNAME, strFName);

if (strFName.Trim().IsEmpty())

{

AfxMessageBox(“Please enter your first name”); ((CEdit*)GetDlgItem(IDC_FNAME))->SetFocus();

return;

}

GetDlgItemText(IDC_LNAME, strLName); if (strLName.Trim().IsEmpty())

{

AfxMessageBox(“Please enter your last name”); ((CEdit*)GetDlgItem(IDC_LNAME))->SetFocus(); return;

}

//Check for the date of birth entered – To do this, you will use the VerifyDate //method. I will provide the code for this function later.

GetDlgItemText(IDC_DOB, strDOB); if (!VerifyDate(strDOB))

{

AfxMessageBox(“Please enter/correct your DOB”); ((CEdit*)GetDlgItem(IDC_DOB))->SetFocus(); return;

}

//retrieving the address and checking for not being empty

GetDlgItemText(IDC_PADD, strPADD); if (strPADD.Trim().IsEmpty())