Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

VALIDATIONS AND EXCEPTION HANDLING

Chapter 9

195

 

 

 

Summary

This chapter explained the concepts for validating code and handling exceptions. There are several ways to ensure that users specify valid data into controls, such as selecting the appropriate Windows control for accepting information, validating data in controls by using the Leave event of controls, and validating a form at the Click event of a button.

To handle exceptions in applications, you can use the try and catch statements. You can also use the Debug and Trace classes of the System.Diagnostics namespace to ensure that the values stored in variables remain within expected limits.

Finally, you can use the debugging windows and the Task List feature of Visual Studio .NET to debug your applications.

This page intentionally left blank

Chapter 10

Database

Interaction Using

ADO.NET

198 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

In most real-life projects, your application needs to interact with a database. To do so, Visual Studio .NET provides you with the ADO.NET data access model

that helps your application to communicate with data sources, such as a Microsoft

SQL Server and Oracle.

Y

 

L ADO.NET enables you to modify, Fadd, or delete data from the data source. To

ADO.NET is mainly designed to provide data access for distributed applications,

such as Web applications. In addition to accessing data from a data source,

provide data access, ADO.NET contains data providers, such as Microsoft Jet OLE DB Provider, Microsoft OLE DB Provider for Oracle, Microsoft OLE DB

E In this chapter, youTwill learn to connect the Windows forms that you have cre-

Provider for SQL Server, and so on. These data providers are used to connect to

a corresponding data source, enabling users to access and modify the data in the

data source.

M

A

 

ated for the Customer Maintenance project to the corresponding data source. In addition, you will add functionality to the data-bound controls used in these Windows forms. Finally, you will learn to create a form by using the Data Form Wizard.

Connecting Windows Forms

to a Data Source Using ADO.NET

You have seen the design of the database in Chapter 7, “Project Case Study.” In addition, you have created forms for the Customer Maintenance project in Chapter 8, “Windows Forms and Controls.” You will now learn to connect the forms that you have created to the Customer Maintenance database.

Creating Form1

Before connecting the WorkerForm, CustomerForm, and JobDetails form to the corresponding data sources, you can add functionality to the menu items that you created in Form1.

Team-Fly®

DATABASE INTERACTION USING ADO.NET

Chapter 10

199

 

 

 

 

Form1 contains menu items used to display the corresponding forms. You have already created these menu items; however, they are not functional. You can add the code to these menu items to make them functional. To do so, perform the following steps:

1.Double-click on menuItem1 to display the code window.

When the user clicks the Worker menu item, WorkerForm should be displayed. In addition, Form1 should be hidden.

2.To display WorkerForm, add the following code to the Click event of

menuItem1.

private void menuItem1_Click (object sender, System.EventArgs e)

{

WorkerForm newForm = new WorkerForm(); newForm.ShowDialog(this);

}

The previous code creates an instance of WorkerForm named newForm and then calls the ShowDialog() method to display WorkerForm.The ShowDialog() method is used to display WorkerForm as a modal dialog box. As you can see, the ShowDialog() method has a parameter this. Using the keyword this enables you to declare Form1 as the parent form of WorkerForm.

Similarly, you can write the code for the Customer menu item, the Job Details menu item, and the Reports menu item, which are used to display the CustomerForm, the JobDetails form, and the Reports form, respectively.

The Exit menu in Form1 is used to close Form1. Therefore, you need to call the Close() method of the Form class as follows:

private void menuItem5_Click(object sender, System.EventArgs e)

{

this.Close();

}

MODAL AND MODELESS FORMS

Visual Studio .NET allows you to display a form or a dialog box as modal or modeless in a MDI (multiple-document interface) application. A modal dialog box, when active, does not allow you to work with any other form in the application. Therefore, you need to close the form to work with another form in the application. You may even hide the modal form to work with another form.

However, a modeless dialog box allows you to work with another form in the application even if you do not close the modeless form.