Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
77
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

dataAdapter.Update(ds, "article");

ds.AcceptChanges();

Finally we brought the controls to normal mode by calling the ToggleControls() method and passing it the true value. We set the insertSelected variable to false, changed the cursor back to normal and updated the progress label. (The use of the insertSelected variable is discussed later in the Cancel button's event handler)

ToggleControls(true);

insertSelected = false;

this.Cursor = Cursors.Default;

lblLabel.Text = "Changes Saved";

It is important to note here that the Save button is used to save the changes in the current record. It may be selected after either the Edit Record or Insert Record buttons. In the Edit Record case, the pointer is already on current record, while in the case of the Insert Record button, as we will see shortly in the Inserting Record section, the program inserts a new empty row, moves the current record pointer to it and presents it to the user to insert the values. Hence, the job of the Save button in both cases is to save the changes in the current record from the text boxes to the data table, updating the data set and finally updating the database.

Event Handler for the Cancel Button

The Cancel button's event handler is:

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

{

if(insertSelected)

{

btnDeleteRecord_Click(null, null); insertSelected = false;

}

FillControls();

ToggleControls(true);

}

The form and controls can be brought into edit mode by pressing either the Edit Record or Insert Record button. When the Insert Record button is selected, the program inserts an empty record (row) to the table (the details of which we will see shortly) and brings the form and controls to edit mode by using the ToggleControls(false) statement. The Cancel button is used to cancel both editing of the current record and the newly inserted record. When canceling the insertion of a new record, the program needs to delete the current (newly inserted) row from the table. We have used, in our application, a Boolean variable 'insertSelected' which is set to true when the user

250

Programmers Heaven: C# School

selects the Insert Record button. This Boolean value informs the Cancel button whether the edit mode was set by the Edit Record button or by the Insert Record button. Hence the Cancel button event handler first checks whether insertSelected is true and if it is, it calls the Delete button's event handler to delete the current record and sets insertSelected back to false.

if(insertSelected)

{

btnDeleteRecord_Click(null, null);

insertSelected = false;

}

Then we fill the controls (text boxes) from the data in the current record and bring the controls back to the normal mode.

FillControls();

ToggleControls(true);

Inserting Records

To insert a record into the table, the user can select the Insert Record button. A record is inserted into the table by adding a new row to the DataTable's Rows collection. Here is the event handler for the Insert Record button.

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

{

insertSelected = true;

DataRow row = dataTable.NewRow(); dataTable.Rows.Add(row);

totalRec = dataTable.Rows.Count; currRec = totalRec-1; row["artId"] = totalRec;

txtArticleId.Text = totalRec.ToString(); txtArticleTitle.Text = ""; txtArticleTopic.Text = ""; txtAuthorId.Text = ""; txtNumOfLines.Text = "";

txtDateOfPublishing.Text = DateTime.Now.Date.ToString();

ToggleControls(false);

}

251

Programmers Heaven: C# School

First of all we set the insertSelected variable to true, so that later the Cancel button may get informed that the edit mode was set by the Insert Record button. We then created a new DataRow using the DataTable's NewRow() method, added it to the Rows collection of the data table and updated the currRec and totalRec variables.

DataRow row = dataTable.NewRow();

dataTable.Rows.Add(row);

totalRec = dataTable.Rows.Count;

currRec = totalRec-1;

Now the new row is ready to have the new values inserted into it. Here we have set the artId field to the total number of records as we don't want to allow the user to set the primary key field of the table. Of course this is just a design issue. You may want to allow your user to insert the primary key field value too.

row["artId"] = totalRec;

txtArticleId.Text = totalRec.ToString();

We then cleared all the text boxes, but filled the Date of Publishing text box with the current date in order to help the user, and finally set the edit mode by calling the ToggleControls() method.

txtArticleTitle.Text = ""; txtArticleTopic.Text = ""; txtAuthorId.Text = ""; txtNumOfLines.Text = "";

txtDateOfPublishing.Text = DateTime.Now.Date.ToString();

ToggleControls(false);

Deleting a Record

Deleting a record is again very simple. All you need to do is get a reference to the target row and call its Delete() method. Then you need to call the Update() method of the data adapter and the AcceptChanges() method of the DataSet to permanently save your changes in the data table to the physical database. The event handler for the Delete Record button is:

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

{

DialogResult res = MessageBox.Show(

"Are you sure you want to delete the current record?", "Confirm Record Deletion", MessageBoxButtons.YesNo);

if(res == DialogResult.Yes)

{

252

Programmers Heaven: C# School

DataRow row = dataTable.Rows[currRec];

row.Delete();

dataAdapter.Update(ds, "article");

ds.AcceptChanges();

lblLabel.Text = "Record Deleted";

totalRec--;

currRec = totalRec-1;

FillControls();

}

}

Since selecting the delete record button will permanently delete the current record, we need to seek confirmation from the user to check they are sure they wish to go ahead with the deletion. For this purpose we present the user with a message box with Yes and No buttons.

DialogResult res = MessageBox.Show(

"Are you sure you want to delete the current record?",

"Confirm Record Deletion", MessageBoxButtons.YesNo

);

If the user selects the Yes button in the message box, the code to delete the current record is executed.

if(res == DialogResult.Yes)

{

DataRow row = dataTable.Rows[currRec]; row.Delete();

dataAdapter.Update(ds, "article"); ds.AcceptChanges();

lblLabel.Text = "Record Deleted"; totalRec--;

currRec = totalRec-1; FillControls();

}

First we got a reference to the row representing the current record in the data table and called its Delete() method. We then saved the changes to the database and updated the totalRec and currRec variables. Finally, we filled the controls (text boxes) with the last record in the table.

This concludes our demonstration application to perform common data access tasks using ADO.Net.

253

Соседние файлы в папке books