Home > Articles > Data > Access

This chapter is from the book

Creating the Authors ADO.NET Application

In this lesson, you will be creating a managed C++ application that produces a Windows Form. The application will access data found within an Access database and display the results within the controls of the form. Furthermore, the application will allow you to add new records to the database.

The first step is to create the main project solution. You will be creating a managed C++ application, so click New, Project from the File menu. Select Visual C++ Projects from the list of project types and select Managed C++ Application from the list of application templates. Give your solution the name AuthorDB and click OK to create the project. Now that the project has been created, you will need to create the main form's code and the necessary event handlers for the various controls contained on the form. The user interface will consist of three edit controls, three label controls, and six buttons. The edit controls will be used to display the database contents, and the buttons will be used to navigate and change the data.

For this project, create a separate file containing the Windows Form code. Click Project, Add Class from the main menu. Select the Generic C++ Class option from the list of class templates. Click Open and give your class the name AuthorDBForm. Click Finish to create the new class. Because this just creates a non-managed class, you will have to add the __gc keyword to the class declaration. Open the AuthorDBForm.h file and add the __gc keyword before the class keyword. Also, you will need to import and declare the necessary namespaces required for Windows Forms. Listing 20.1 shows the various namespaces you will need.

Listing 20.1 Class Declaration for the AuthorDB User Interface

 1: #pragma once
 2: #include "stdafx.h"
 3: #include <tchar.h>
 4:
 5: #using <mscorlib.dll>
 6: #using <System.DLL>
 7: #using <System.Drawing.DLL>
 8: #using <System.Windows.Forms.DLL>
 9:
10: using namespace System;
11: using namespace System::Drawing;
12: using namespace System::Windows::Forms;
13:
14: __gc class AuthorDBForm : public Form
15: {
16: public:
17:   AuthorDBForm(void);
18:   ~AuthorDBForm(void);
19:
20: protected:
21:   void InitForm();
22:
23: private:
24:   Button* buttonPrevious;
25:   Button* buttonNext;
26:   Button* buttonFirst;
27:   Button* buttonLast;
28:   Button* buttonNew;
29:   Button* buttonDelete;
30:   Label*  labelID;
31:   Label*  labelName;
32:   Label*  labelYear;
33:   TextBox* textBoxID;
34:   TextBox* textBoxName;
35:   TextBox* textBoxYear;
36:
37:   void buttonNew_Click(Object* sender, EventArgs* e);
38:   void buttonDelete_Click(Object* sender, EventArgs* e);
39:   void buttonFirst_Click(Object* sender, EventArgs* e);
40:   void buttonPrevious_Click(Object* sender, EventArgs* e);
41:   void buttonNext_Click(Object* sender, EventArgs* e);
42:   void buttonLast_Click(Object* sender, EventArgs* e);
43: };

Because this is a Windows Form object, you will need to derive your class from the .NET Forms class. This can be seen on line 14 of Listing 20.1. The next step involves creating the controls for your form. As mentioned earlier, you will be creating six Button controls, three TextBox controls, and three Label controls. Create the necessary declarations for these controls in a private section of your class declaration. We will continue with the same design that has been used throughout this book for forms, so create a protected InitForm method that doesn't accept any parameters and returns void.

This form contains two groups of buttons. Four of these buttons will be used to navigate through the data. With these buttons, you will be able to move to the next or previous record and also be able to move to the first or last record within the DataTable. The other group of buttons will allow you to insert and delete records within the database. Therefore, create six event handler delegates for each of the buttons, as shown in Listing 20.2.

Listing 20.2 Setting Control Properties and Wiring Events Within InitForm

 1: #include "StdAfx.h"
 2: #include "authordbform.h"
 3:
 4: AuthorDBForm::AuthorDBForm()
 5: {
 6:   InitForm();
 7: }
 8:
 9: AuthorDBForm::~AuthorDBForm()
 10: {
 11:
 12: }
 13:
 14: void AuthorDBForm::InitForm()
 15: {
 16:  textBoxID = new TextBox();
 17:  textBoxName = new TextBox();
 18:  textBoxYear = new TextBox();
 19:  buttonPrevious = new Button();
 20:  buttonNext = new Button();
 21:  buttonFirst = new Button();
 22:  buttonLast = new Button();
 23:  buttonNew = new Button();
 24:  buttonDelete = new Button();
 25:  labelID = new Label();
 26:  labelName = new Label();
 27:  labelYear = new Label();
 28:
 29:  SuspendLayout();
 30:
 31:  // textBoxID
 32:  textBoxID->Location = Point(16, 24);
 33:  textBoxID->Name = S"textBoxID";
 34:  textBoxID->Size = System::Drawing::Size(40, 20);
 35:  textBoxID->TabIndex = 0;
 36:  textBoxID->Text = S"";
 37:  textBoxID->ReadOnly = true;
 38:
 39:  // textBoxName
 40:  textBoxName->Location = Point(64, 24);
 41:  textBoxName->Name = S"textBoxName";
 42:  textBoxName->Size = System::Drawing::Size(192, 20);
 43:  textBoxName->TabIndex = 1;
 44:  textBoxName->Text = S"";
 45:
 46:  // textBoxYear
 47:  textBoxYear->Location = Point(264, 24);
 48:  textBoxYear->Name = S"textBoxYear";
 49:  textBoxYear->Size = System::Drawing::Size(104, 20);
 50:  textBoxYear->TabIndex = 2;
 51:  textBoxYear->Text = S"";
 52:
 53:  // buttonPrevious
 54:  buttonPrevious->Location = Point(48, 56);
 55:  buttonPrevious->Name = S"buttonPrevious";
 56:  buttonPrevious->Size = System::Drawing::Size(32, 23);
 57:  buttonPrevious->TabIndex = 3;
 58:  buttonPrevious->Text = S"<";
 59:  buttonPrevious->add_Click(new EventHandler(this,buttonPrevious_Click));
 60:
 61:  // buttonNext
 62:  buttonNext->Location = Point(80, 56);
 63:  buttonNext->Name = S"buttonNext";
 64:  buttonNext->Size = System::Drawing::Size(32, 23);
 65:  buttonNext->TabIndex = 4;
 66:  buttonNext->Text = S">";
 67:  buttonNext->add_Click( new EventHandler( this, buttonNext_Click ));
 68:
 69:  // buttonFirst
 70:  buttonFirst->Location = Point(16, 56);
 71:  buttonFirst->Name = S"buttonFirst";
 72:  buttonFirst->Size = System::Drawing::Size(32, 23);
 73:  buttonFirst->TabIndex = 5;
 74:  buttonFirst->Text = S"<<";
 75:  buttonFirst->add_Click( new EventHandler( this, buttonFirst_Click ));
 76:
 77:  // buttonLast
 78:  buttonLast->Location = Point(112, 56);
 79:  buttonLast->Name = S"buttonLast";
 80:  buttonLast->Size = System::Drawing::Size(32, 23);
 81:  buttonLast->TabIndex = 6;
 82:  buttonLast->Text = S">>";
 83:  buttonLast->add_Click( new EventHandler( this, buttonLast_Click ));
 84:
 85:  // buttonNew
 86:  //
 87:  buttonNew->Location = Point(232, 56);
 88:  buttonNew->Name = S"buttonNew";
 89:  buttonNew->Size = System::Drawing::Size(64, 23);
 90:  buttonNew->TabIndex = 7;
 91:  buttonNew->Text = S"New";
 92:  buttonNew->add_Click( new EventHandler( this, buttonNew_Click ));
 93:
 94:  // buttonDelete
 95:  buttonDelete->Location = Point(304, 56);
 96:  buttonDelete->Name = S"buttonDelete";
 97:  buttonDelete->Size = System::Drawing::Size(64, 23);
 98:  buttonDelete->TabIndex = 8;
 99:  buttonDelete->Text = S"Delete";
100:  buttonDelete->add_Click( new EventHandler( this, buttonDelete_Click ));
101:  //
102:  // labelID
103:  //
104:  labelID->Location = Point(16, 8);
105:  labelID->Name = S"label1";
106:  labelID->Size = System::Drawing::Size(32, 16);
107:  labelID->TabIndex = 9;
108:  labelID->Text = S"ID";
109:
110:  // labelName
111:  labelName->Location = Point(64, 8);
112:  labelName->Name = S"label2";
113:  labelName->Size = System::Drawing::Size(100, 16);
114:  labelName->TabIndex = 10;
115:  labelName->Text = S"Name";
116:
117:  // labelYear
118:  labelYear->Location = Point(264, 8);
119:  labelYear->Name = S"label3";
120:  labelYear->Size = System::Drawing::Size(100, 16);
121:  labelYear->TabIndex = 11;
122:  labelYear->Text = S"Year Born";
123:
124:  // Form1
125:  AutoScaleBaseSize = System::Drawing::Size(5, 13);
126:  ClientSize = System::Drawing::Size(384, 102);
127:  Name = "AuthorDBForm";
128:  Text = "Authors";
129:
130:  Controls->Add( labelYear);
131:  Controls->Add( labelName );
132:  Controls->Add( labelID );
133:  Controls->Add( buttonDelete );
134:  Controls->Add( buttonNew );
135:  Controls->Add( buttonLast );
136:  Controls->Add( buttonFirst );
137:  Controls->Add( buttonNext );
138:  Controls->Add( buttonPrevious );
139:  Controls->Add( textBoxYear );
140:  Controls->Add( textBoxName );
141:  Controls->Add( textBoxID );
142:
143:  ResumeLayout(false);
144: }
145:
146: void AuthorDBForm::buttonNew_Click(Object* sender, EventArgs* e)
147: {
148: }
149:
150: void AuthorDBForm::buttonDelete_Click(Object* sender, EventArgs* e)
151: {
152: }
153:
154: void AuthorDBForm::buttonFirst_Click(Object* sender, EventArgs* e)
155: {
156: }
157:
158: void AuthorDBForm::buttonPrevious_Click(Object* sender, EventArgs* e)
159: {
160: }
161:
162: void AuthorDBForm::buttonNext_Click(Object* sender, EventArgs* e)
163: {
164: }
165:
166: void AuthorDBForm::buttonLast_Click(Object* sender, EventArgs* e)
167: {
168: }

Now that the user interface variables and member functions have been declared, you can set the control properties and wire the controls to their associated delegates. This is all done within the InitForm function, as shown in Listing 20.2. The last step is to make sure your applications entry-point function, _tmain, displays your new form. In your AuthorDB.cpp file, include the header file for your form class and add the code to display your form, as shown in Listing 20.3.

Listing 20.3 Displaying Your Main Windows Form

 1: #include "stdafx.h"
 2:
 3: #using <mscorlib.dll>
 4: #include <tchar.h>
 5:
 6: using namespace System;
 7:
 8: #include "authordbform.h"
 9:
10: // This is the entry point for this application
11: int _tmain(void)
12: {
13:   Application::Run( new AuthorDBForm() );
14:   return 0;
15: }

At this point, your user interface is complete, albeit without any data to work with. Compile your application to make sure everything is working correctly. When you run your application, you should see a window similar to the one in Figure 20.2.

Figure 20.2 The user interface for the AuthorDB application.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020