Lesson 2 Managing Control Layout with Container Controls

This lesson describes how to add and configure container controls. You will learn how to add controls to a form or to a container control and to configure various kinds of container controls to create dynamic and varied layouts for controls in your form. After this lesson, you will be able to Add a control to a form or container control at design time. Add a control to a form or container at run time. Group and arrange controls with the Panel control. Group and arrange controls with the...

Controlling Connection Pooling Options

Connection pooling is enabled by default when creating ADO.NET connection objects. You can control connection pooling behavior (or disable pooling altogether) by setting connection string keywords specific to connection pooling. For example, to specifically disable connection pooling, you set Pooling False in your connection string. Table 5-7 provides a list of connection string keywords that can be used to control how a specific connection interacts with the connection pool. Not all keywords...

Lab Working with Data Adapter Objects

In this lab you will create DataAdapters. Exercise 1 Creating a DataAdapter with the Data Adapter Configuration Wizard 1. Create a Windows application and name it DataAdapterWizardExample. In this version of Visual Studio, DataAdapter objects have been removed from the Toolbox in lieu of using the Designer-generated TableAdapters, so first add the SqlDataAdapter back into the Toolbox for this example. 2. Right-click the Data section of the Toolbox and select Choose Items. 3. In the Choose...

Lab Securing a Configuration File

In this lab you will practice encrypting and decrypting a configuration file. Exercise 1 Encrypting and Decrypting a Configuration File In this lesson, you will see how to use the DpapiProtectedConfigurationProvider to encrypt and decrypt the ConnectionStrings section of the app.config file. 1. Create a new Windows Application and name it SecuringConnectionStrings. 2. Add a reference to the System.Configuration namespace. 3. Add two buttons to the form, setting the Name and Text properties to...

The Mask Property

The most important property of the MaskedTextBox is the Mask property. This property allows you to define a string that represents the required format of an input string in the MaskedTextBox. The MaskedTextProvider associated with the MaskedTextBox provides the parsing engine that parses the Mask format. The code characters used by the default MaskedTextProvider are shown in Table 2-10. Table 2-8 Elements of the Default MaskedTextProvider Table 2-8 Elements of the Default MaskedTextProvider...

Lab Bulk Copying

In this lab you will bulk copy data from one table to another. Creating Tables to Copy Data into To demonstrate how to perform a bulk copy operation, you need tables to copy data into. A quick way to create the tables is to use Server Explorer and the Visual Database Tools to do some cutting and pasting Use the following steps to create CustomerHis-tory and OrderHistory tables, which you will use to bulk copy the Customer and Order table data into. 1. In Server Explorer, expand the Tables node...

Creating a Command Object that Returns XML Data

You can also execute commands that return data formatted as XML. To execute commands that return XML data, create commands that run SQL statements that return XML or retrieve existing XML-formatted data from your database. Set the CommandText property to a SQL statement that returns XML and call the ExecuteXMLReader method of the command. Calling the ExecuteXMLReader method returns a System.Xml.XmlReader object, which is similar to a data reader except that it is specifically for accessing...

Working with BLOBs

Saving and fetching binary data presents interesting problems that are typically not encountered when querying standard rows of data. The problems arise because you will probably not want to move the entire BLOB in one piece but will likely need to break it up into smaller portions. For example, consider having to move a large binary that is several megabytes in size. Loading the entire BLOB into a variable consumes a lot of memory and can seriously affect the performance of your application....

Configuring Connection Pooling with Ole Db Connections

The OLE DB connection object OleDbConnection automatically pools connections through the use of OLE DB session pooling. You control how OLE DB connections use pooling by adding an OLE DB Services keyword to the connection string and setting its value based on the combination of services you want to enable or disable for the connection. The following connection strings explicitly enable connection pooling by setting the OLE DB Services keyword to -1. OLE DB connection string for an Office Access...

Merging Menus

Menus can be merged at run time and their items incorporated into a single menu. You can merge MenuStrips, ContextMenuStrips which are covered in greater detail later in this lesson , or both. In fact, you can even merge MenuStrip controls with ToolStrip controls. Like merging tool strips, merging menu strips is accomplished by invoking the ToolStripManager.Merge method on the static ToolStripManager class, as shown here targetMenuStrip C targetMenuStrip The preceding example takes the first...

How to Create Auto Incrementing Columns in Data Table Objects

The DataTable itself assigns values to an auto-incrementing column in the table. This is typical of a table that contains information such as order information where it is desirable to have a unique OrderID, but the actual value of the OrderID column isn't important other than that it needs to be unique. DataColumn objects provide specific properties to create auto-incrementing columns where you can set the starting value, called the AutolncrementSeed, as well as the increment, or number added...

Disabling Hiding and Deleting Menu Items

At times, it makes sense to remove certain options from a menu. You might want a menu item to be disabled when conditions aren't appropriate for it to be invoked or to hide a menu item that shouldn't be displayed. In some cases, you might want to delete a menu item completely from a menu. You can disable a menu item by setting the Enabled property to False. This will cause the menu item to appear dimmed. It will still be visible to the user, but it cannot be invoked by mouse clicks or...

Adding Tool Strip Items to a Tool Strip

At design time, you can add tool strip items to a tool strip by choosing appropriate items from the drop-down menu in the Designer as shown in Figure 4-1. Figure 4-1 Adding a tool strip item at design time The item you choose from the menu will be added to the tool strip, and an instance of it will be added to your application. You can set properties for the item in the Properties window and refer to the item in code. At run time, you can dynamically add items to a tool strip by using the...

Exercise Practice with Container Controls

Open Visual Studio and create a new Windows Forms project. 2. From the Toolbox, drag a TabControl to the surface of the Form. In the Property Grid, set the Dock property to Fill. 3. In the Property Grid, choose the TabPages property to open the TabPages Collection Editor. Add tab pages until there is a total of four pages. Set the Text properties of these four TabPage controls to GroupBox, FlowLayoutPanel, TableLayoutPanel, and SplitContainer, respectively. Click OK. 4. In the form, select...

Lab Working with Data View Objects

In this lab you will use DataView objects to sort and filter the data displayed from a DataTable. Exercise 1 Working with DataView Objects This example demonstrates sorting and filtering using a DataView. 1. Create a Windows application and name it DataViewExample. 2. Add a DataGridView to the form and set its Name property to CustomersGrid. 3. Add a TextBox and set the following properties 4. Add a second TextBox and set the following properties 5. Add a button to the form and set the...

Setting the Data Table Objects Default Table Views Using a Data ViewManager

A DataViewManager is basically a collection of DataViewSetting objects that are used to set the default sorting and filtering behavior of each DataTable in a DataSet. You can use a DataViewManager as a data source for data-bound controls. For example, if you create a DataViewManager to manage several tables from the Northwind DataSet, you can use code similar to the following to bind it to the individual tables Dim dvm As New DataViewManager NorthwindDataSet1 OrdersDataGridView.DataSource C...

Synchronizing a Data Set with an Xml DataDocument

When working with XML data and DataSet objects, you will typically need to manipulate data through the DataSet classes as well as XML classes available in the .NET Framework. Keeping your DataSet and XmlDataDocument in synch allows you to process the data using whichever method of access you prefer while working on the same data source. The following code example shows how to create a new XmlDataDocument and synchronize it with the NorthwindDataSet. Dim NwDataDocument As New XmlDataDocument...

Maintaining Changes to Data Row Objects

Changes to DataRow objects that have not yet been accepted or rejected are maintained by the RowState and DataRowVersion enumerations. The RowState is used to determine the state of a row. When first populated, the rows in a DataTable have a RowState of Unchanged. Table 7-3 details the different values of the RowState enumeration. No changes have been made since the last AcceptChanges call or since the initial filling of the DataTable. This row has been added since the last AcceptChanges call....

Resolving Conflicts Between a Data Set and a Database Using the Data Adapter

DataAdapter objects provide two specific properties that assist in resolving conflicts that may occur when attempting to fill a DataSet with a DataAdapter the MissingMapping-Action property and the MissingSchemaAction property. These conflicts typically occur when changes have been made to the database schema or to the DataSet schema. For example, if columns are added to a table in the database and you attempt to perform a SELECT from that table, the adapter uses these properties to decide what...

Creating Menu Strips and Tool Strip Menu Items

Toolstripmenu

You can create a MenuStrip at design time in the same way that you create any control by dragging it from the Toolbox onto the design surface. Once added to the design surface, an interface for creating tool strip menu items appears. You can type a string into the box in the menu strip to create a new tool strip menu item. After a new item has been created, additional boxes appear to the right and beneath the newly created tool strip menu item to allow you to create more items or sub-items of...

Flat Style and Flat Appearance

The FlatStyle property governs whether the button has a three-dimensional, raised appearance, or a flat appearance. You can give a button a flat appearance by setting the FlatStyle property to Flat. When the FlatStyle property is set to Flat, the FlatAppearance property determines how the button looks and behaves in the user interface. The FlatAppearance property is an instance of a structure that contains properties described in Table 2-4. Table 2-4 Properties of FlatAppearance Table 2-4...

Executing Commands Asynchronously

In addition to the execution commands listed in Figure 6-2, an additional set of commands are specifically used for asynchronous calls to a database. Executing commands asynchronously is the process of having the command execute on a separate thread from the rest of your application so users do not have to wait for the command to complete before continuing work in other parts of the application. For example, commands that are not executed asynchronously can cause your form to freeze until the...

Validating XML with Xml ValidatingReader

The XmlValidatingReader allows you to validate XML against a given schema. As the document is read, it is checked for validity against the schema that are indicated. XmlValidatingReader wraps an existing XmlReader instance. You can create an XmlValidatingReader instance, as shown here ' Assumes the existence of an XmlReader named aReader Dim aValReader As XmlValidatingReader aValReader New XmlValidatingReader aReader Assumes the existence of an XmlReader named aReader aValReader new...

Lab Returning the List of Visible SQL Servers

In this lab you will practice enumerating the SQL Servers on your network. Exercise 1 Enumerating the SQL Servers on a Network To demonstrate how to retrieve the list of visible SQL servers, let's create a small application to display the information returned from the GetDataSources method in a Data-GridView. 1. Create a new Windows application named SqlServerEnumerator. 2. Add a DataGridView to the form and name it VisibleSqlServers. The DataGridView is the control typically used for...

What Are Command Objects

To execute SQL statements and stored procedures against a database from your application , you use Command objects. Command objects contain the necessary information to execute SQL statements, stored procedures, functions, and so on against a data source return data to your application and perform database catalog operations such as creating, altering, and deleting database objects. In other words, you can use Command objects to execute any valid SQL statement. NOTE SQL Statements and Stored...

Providing a Toolbox Bitmap for Your Control

After a control has been built, it automatically appears in the Toolbox if you are using it in the same solution that contains the control, or it can be added to the Toolbox if it was created in a different project. When the control is added to the Toolbox, it appears in the Toolbox as the name of the control next to an icon. If no icon is specified, a generic icon is supplied. You can specify the icon that is displayed next to the name of your control by using the ToolboxBitmapAttribute. You...

Executing Multiple SQL Statements Using a Data Reader

In addition to returning the results from a single SQL statement, you can use a Command object and DataReader to return the results of multiple SQL statements. To execute more than one SQL statement, set the CommandText property of a Command object to multiple SQL statements separated by semicolons . After calling the ExecuteReader method, the DataReader will hold the number of result sets equal to the number of SQL statements executed. To access the data returned by the additional statements,...

Lab Practice Opening and Closing Data Connections

In this lab you will practice working with connection objects by opening and closing the connections and displaying connection information back to the user. Exercise 1 Opening and Closing Data Connections To demonstrate working with connection objects, perform the following steps 1. Create a new Windows application and name it DataConnections. 2. Because Windows applications are not created with a reference to the System .Data.OracleClient namespace, from the Project menu select the Add...

Lesson Review

The following questions are intended to reinforce key information presented in this lesson. The questions are also available on the companion CD if you prefer to review them in electronic form. Answers to these questions and explanations of why each choice is right or wrong are located in the Answers section at the end of the book. 1. Which of the following lines of code should be used to print an additional page in the method handling the PrintPages event 2. Which of the following lines of...

Perform Batch Operations Using Data Adapter Objects

DataAdapter objects provide the opportunity to execute commands in batches through the DataAdapter.UpdateBatchSize property. Execute commands in batches when you are updating large numbers of records and want to reduce round trips to the database. Perform Batch Updates with a DataAdapter Set the UpdateBatchSize property of the DataAdapter to an integer representing the number of statements to execute as a batch. For example, to send batches of five statements at a time, you would set...

Configuring Data GridView Columns

There are six built-in types of columns you can use in a DataGridView, as outlined in Table 8-1. When adding columns to a DataGridView, select the type of column based on the data you plan to display in it. Use this column type to display text and numeric values. A data-bound DataGridView automatically generates this type of column when binding to strings and numeric values. Use this column to display Boolean values. A DataGridView automatically generates this type of column when binding to...

Tool StripProgress Bar

The ToolStripProgressBar is a control that is designed to provide feedback to the user when progress is made on a time-consuming task, and it functions very similarly to the standard ProgressBar control. The Minimum and Maximum properties set the minimum and maximum values for the ToolStripProgressBar, and the Value property determines the current setting. The visual appearance is set by the Style property, and when Style is set to Blocks or Continuous, the Value property is reflected in the...

The Web Browser Control

The WebBrowser control provides all of the functionality required to load and display HTML pages and other file types, as well as the functionality needed to navigate to locations on the World Wide Web. You can configure the WebBrowser to expose online help for your application, to load and print documents, or to display files in a variety of formats. Table 3-17 shows important properties of the WebBrowser control. Table 3-17 Important Properties of the WebBrowser Control Table 3-17 Important...

Overview of the Menu Strip Control

The MenuStrip control is essentially a ToolStrip control that is optimized for the display of ToolStripMenuItems. The MenuStrip control derives from ToolStrip and can host all of the tool strip items described in the previous lesson. Its primary function, however, is to host ToolStripMenuItems. ToolStripMenuItems are the controls that provide the visual representation for items on a menu. They can appear as text, an image, or both, and can execute code found in their ToolStripMenuItem.Click...

Data Adapter Commands

DataAdapter objects are typically configured by specifying a single SELECT statement, which is used by the DataAdapter to fetch data and populate a DataTable in a DataSet. It's important to understand how the DataAdapter uses that SELECT statement as the basis for automatically generating the Insert, Update, and Delete statements that are needed to save changes made in the DataSet back to the database when calling the Update method of the DataAdapter. The DataAdapter generates these additional...

Readlnner Xml and Read OuterXml

You can use the ReadInnerXml and ReadOuterXml methods to return portions of the XML file as strings. The ReadInnerXml method returns the contents of the current XML node, including child nodes but excluding the opening and closing nodes themselves. The ReadOuterXml method returns the contents of the current XML node, including child nodes as well as the opening and closing nodes. For example, consider the following XML fragment If XmlReader is positioned on lt text1 gt , ReadInnerXml returns...

Lesson 1 Adding and Configuring Windows Forms

Basic Setting Form

This lesson describes how to create and configure Windows Forms. You will learn how to create forms and refer to them in code, alter the visual properties of the form, and control the behavior of the form at run time. After this lesson, you will be able to Add a Windows Form to a project at design time. Add a new Windows Form at run time. Resize a window at design time or run time. Identify and set the properties that determine a form's appearance and behavior at run time. Refer to the default...