ADO.NET Part 2

22
ADO.NET Part 2

description

ADO.NET Part 2. Overview. Introduction to the DataGridView Control. It’s a two-dimensional grid containing rows and columns Its use in unbound mode was mentioned earlier in the course Now, we will use it in bound mode. The DataGridView Control (Capabilities). - PowerPoint PPT Presentation

Transcript of ADO.NET Part 2

Page 1: ADO.NET Part 2

ADO.NET Part 2

Page 2: ADO.NET Part 2

Slide 2

Overview

Page 3: ADO.NET Part 2

Slide 3

Introduction to the DataGridView Control It’s a two-dimensional grid containing

rows and columns Its use in unbound mode was mentioned

earlier in the course Now, we will use it in bound mode

Page 4: ADO.NET Part 2

Slide 4

The DataGridView Control (Capabilities) Displays rows from a single table

(DataTable) or view Allows editing (add, change, delete) A rich set of events for error handling A rich set of properties and objects for

formatting

Page 5: ADO.NET Part 2

Slide 5

The DataGridView Control (Binding) Set the DataSource and DataMember

properties These can be

A DataSet (DataSource) and DataTable (DataMember)

A BindingSource (DataSource – the DataMember is not set)

Page 6: ADO.NET Part 2

Slide 6

The DataGridView Control (Formatting) Much can be done through the user

interface

Page 7: ADO.NET Part 2

Slide 7

The DataGridView Control(Columns Editor)

Page 8: ADO.NET Part 2

Slide 8

DataGridView (Row Events) In all, there are about 100 different

events Only selected events are of interest to us RowEnter fires as a row gets input focus RowLeave fires as a row loses focus RowValidating fires after RowLeave and

provides the chance for validation This event can be cancelled

Page 9: ADO.NET Part 2

Slide 9

DataGridView (Row Events 2) Event arguments are complex types DataGridViewCellEventArgs

RowIndex contains the 0-based index of the row

ColIndex contains the 0-based index of the column

DataGridViewCellCancelEventArgs Set Cancel to true to cancel the

subsequent events

Page 10: ADO.NET Part 2

Slide 10

DataGridView (Row Events 3) See tblLocationDataGridView event

handlers in IS389ADOPart 2

Page 11: ADO.NET Part 2

Slide 11

DataGridView (Cell Events 1) CellEnter fires when a cell gets focus CellBeginEdit fires when editing

begins CellEndEdit fires when editing ends CellLeave fires after end edit when cell

loses focus

Page 12: ADO.NET Part 2

Slide 12

DataError Event Remember that in this context, the DataGridView is bound to a data source

Trying to store type invalid values in the underlying DataTables will throw exceptions

Handle this event to handle those exceptions

Page 13: ADO.NET Part 2

Slide 13

Using the OleDbCommand Object (ExecuteScalar) Remember the ExecuteScalar method

returns a single value Use to get a max ID value

Page 14: ADO.NET Part 2

Slide 14

ExecuteScalar (Example) Get Max of field fldBoreHoleID in

tblBoreHoleDataSystem.Data.OleDb.OleDbCommand oCmd = new

System.Data.OleDb.OleDbCommand();

oCmd.CommandType = CommandType.Text;

oCmd.CommandText = "SELECT MAX(fldBoreHoleID) FROM tblBoreholeData";

oCmd.Connection = tblBoreholeDataTableAdapter.Connection;

oCmd.Connection.Open();

int i;

i = (int) oCmd.ExecuteScalar();

oCmd.Connection.Close();

Page 15: ADO.NET Part 2

Slide 15

Using the OleDbCommand Object (ExecuteNonQuery) Use to Execute INSERT, UPDATE, or DELETE

commands that do not return rows Example 1:System.Data.OleDb.OleDbCommand oCmd = new

System.Data.OleDb.OleDbCommand();

oCmd.CommandType = CommandType.Text;

oCmd.CommandText = "INSERT INTO tblCasIndex (fldCASIndexNumber, fldChemicalName, fldCASRegNo, fldCarcinogenic) VALUES (123,'abc','def',false)";

oCmd.Connection = tblBoreholeDataTableAdapter.Connection;

oCmd.Connection.Open();

oCmd.ExecuteNonQuery();

oCmd.Connection.Close();

Page 16: ADO.NET Part 2

Slide 16

Using the OleDbCommand Object with Parameters Unlike the previous example, we

generally don’t want to hardcode the data values

This is where parameters come in Add one parameter for each dynamic

field

Page 17: ADO.NET Part 2

Slide 17

Creating a Parameter An OleDbParameter has a

Name Data type Size Mapping field

Example:System.Data.OleDb.OleDbParameter prop = new System.Data.OleDb.OleDbParameter("fldCASIndexNumber",System.Data.OleDb.OleDbType.Integer,0,"fldCASIndexNumber");

Page 18: ADO.NET Part 2

Slide 18

Adding the Parameter Each parameter must be added to the Parameters collection of the OleDbCommand

ExampleoCmd.Parameters.Add(prop);

Page 19: ADO.NET Part 2

Slide 19

Filling in the Parameter Values Set the Value property of a particular

parameteroCmd.Parameters["fldCASIndexNumber"].Value = 234;

oCmd.Parameters["fldChemicalName"].Value = "XXX";

oCmd.Parameters["fldCASRegNo"].Value = "YYY";

oCmd.Parameters["fldCarcinogenic"].Value = true;

Page 20: ADO.NET Part 2

Slide 20

A Parameterized insert statement The INSERT statement looks like this

oCmd.CommandText = "INSERT INTO tblCasIndex (fldCASIndexNumber, fldChemicalName, fldCASRegNo, fldCarcinogenic) VALUES (?,?,?,?)";

Page 21: ADO.NET Part 2

Slide 21

Using the OleDbDataReader Place an SQL SELECT statement in the OleDbcommand

Call ExecuteReader to create the forward-only reader

Call Read to read each record Then call the various Get method to get

the value of each field

Page 22: ADO.NET Part 2

Slide 22

Programmatically Selecting Records See Example