// Set up the Update record button updaterecord = new Button(); updaterecord->Text = “Update Current Record”; updaterecord->Size = Drawing::Size(100, 30); updaterecord->TabIndex = 0;
After the call to CreateControls, the constructor of the BankForm class calls the method FetchData. The FetchData method retrieves the data from the database and calls the FillFields method, which updates the controls with the data from the dataset, retrieved by FetchData. The FetchData method is called whenever current data is to be retrieved from the database.
Here is the complete listing for the FetchData method:
void FetchData()
{
// user id and password
String *userId = “sa”;
String *password = “”;
// Connect to the SQL Database and issue a SELECT command all in one statement
String *query = S”SELECT * FROM Account_Detail”;
// build connect string with the userid and password
SqlConnection* sqlconn = new SqlConnection(connectString); sqlconn->Open();
// set the adapter with the main command myAdapter = new SqlDataAdapter(query,sqlconn);
// set the various commands to be operated on the database
SqlCommandBuilder* DataAdapterCommands = new
SqlCommandBuilder(myAdapter);
// Create the Dataset ds = new DataSet();
IMPLEMENTING ADO.NET IN A MANAGED C++ APP. Chapter 14 493
//Fill the dataset using the fill method of the adapter
//also include the table that is used to populate it
myAdapter->Fill(ds, “Account_Detail”);
//call the functions to fill the fields with the records
//passing 0 is for the first row
FillFields(currentrec);
// Close the connection sqlconn->Close();
}
Next, you will look at the FillFields method that is called by the FetchData method to update the controls with the data from the database. The method takes a row number as a parameter and fills the controls on the form with the data from that row in the dataset.
void FillFields(int i)
{
DataTable *dtab = new DataTable();
DataRow *dr;
// set an alias for the data table collections dtab = ds->get_Tables()->get_Item(0);
//set an alias for the data row collections based on the value of i dr = dtab->get_Rows()->get_Item(i);
if(dr != NULL)
{
// fill each field on the form with the current dataset row fname->set_Text(dr->get_Item(“First_name”)->ToString());
The form has buttons and menu options that allow navigation between records. There are options for moving to the first record, the previous record, the next record, and the last record. The following event handler methods provide the functionality. A variable currentrec is used to maintain the current record number.
The OnFirstRecord method simply calls the FillFields method with a parameter 0:
void OnFirstRecord(Object *sender, EventArgs *e)
{
//call the functions to fill the fields with the records
//passing 0 is for the first row
FillFields(0); currentrec = 0;
}
void OnPrevRecord(Object *sender, EventArgs *e)
{
//call the functions to fill the fields with the records
//pass current record -> ‘1’ is for the previous row
The BankForm class also has buttons for inserting a new record and for updating an existing record. The OnInsertRecord and OnUpdateRecord methods handle the events of these buttons.
The OnInsertRecord method calls the ValidateForm method to ensure that none of the required fields is blank. If ValidateForm returns false, it sets the text of the button to “submit” and returns. Otherwise, the method does the following:
1.Creates a new DataRow object.
2.Sets the fields of the DataRow object with data from the form controls.
3.Adds the DataRow object to the DataSet.
Following is a listing of the OnInsertRecord method:
void OnInsertRecord(Object *sender, EventArgs *e)
{
if (flag==false)
{
ClearForm();
flag=true;
insertrecord->Text=”Submit”;
return;
496 Project 3 ADO.NET IN MANAGED C++
}
if (ValidateForm()==true)
{
flag=false;
insertrecord->Text=”Insert New Record”; DataRow *dr;
// create a new datarow object
dr = ds->get_Tables()->get_Item(0)->NewRow();
// fill each Datarow field with the values on the form
// default values picked up for update requirements dr->set_Item(“Type_account”,typeac->Text); dr->set_Item(“Valid_acc”,va->Text);
// add the row to the data set ds->get_Tables()->get_Item(0)->get_Rows()->Add(dr);
//use the update method of the adapter to update the
//database from the contents of the dataset.
//this can also be called later at any point
myAdapter->Update(ds,”Account_Detail”);
IMPLEMENTING ADO.NET IN A MANAGED C++ APP. Chapter 14 497
// the following function is to refresh the form with the latest data
FetchData();
MessageBox::Show(“Record added successfully.”);
}
}
The OnUpdateRecord method updates the current record that is being displayed on the form, into the database, whenever clicked on:
void OnUpdateRecord(Object *sender, EventArgs *e)
{
//when the current record is displayed
//the user changes any field and clicks on the update button
//this function will ensure that it is updated in the database
if (ValidateForm()==true)
{
DataTable *dtab = new DataTable();
DataRow *dr;
// set an alias for the data table collections dtab = ds->get_Tables()->get_Item(0);
//set an alias for the data row collections for the current row dr = dtab->get_Rows()->get_Item(currentrec);
// fill each Datarow members with the latest values dr->set_Item(“First_name”, fname->Text->ToString()); dr->set_Item(“Last_name”,lname->Text->ToString()); dr->set_Item(“date_birth”,dob->Text->ToString()); dr->set_Item(“Permanent_Addr”,paddr->Text->ToString()); dr->set_Item(“Mailing_Addr”,maddr->Text->ToString()); dr->set_Item(“Phone_Number”,phno->Text->ToString()); dr->set_Item(“eMailID”,emailid->Text->ToString()); dr->set_Item(“Ref_Acc_no”,refacno->Text); dr->set_Item(“Balance”,amount->Text);
// random account number generation dr->set_Item(“Account_number”,amount->Text);
498 Project 3 ADO.NET IN MANAGED C++
// default values picked up for update requirements dr->set_Item(“Type_account”,typeac->Text); dr->set_Item(“Valid_acc”,va->Text);
//use the update method of the adapter to update the
//database from the contents of the dataset.
//this can also be called later at any point
myAdapter->Update(ds,”Account_Detail”);
// the following function is to refresh the form with the latest data
FetchData();
MessageBox::Show(“Record updated successfully.”);
}
}
The class also uses a couple of support methods, ClearForm and ValidateForm, to clear all the controls and to validate the contents of the controls before an insert or update operation. The ClearForm method simply sets the Text property of all the text boxes to a blank string:
void ClearForm()
{
fname->set_Text(“”);
lname->set_Text(“”);
dob->set_Text(“”);
paddr->set_Text(“”);
maddr->set_Text(“”);
phno->set_Text(“”);
emailid->set_Text(“”);
refacno->set_Text(“”);
amount->set_Text(“”);
}
The ValidateForm method checks to see if any of the required fields on the form are left blank before a insert or update operation, and calls the SetError method of an ErrorProvider object with the name of the control causing the error and the error message. The ErrorProvider object has been declared in the BankForm class and initialized in the constructor. The ErrorProvider class provides a user inter-
IMPLEMENTING ADO.NET IN A MANAGED C++ APP. Chapter 14 499
face element that can be used to show a flashing error icon next to a control, to indicate that the control has an error associated with it. This has been initialized in the constructor of the BlankForm class, as shown here: