Find the error(s) in the following code. This method should modify the Age field of strUserName.
objUpdateAge[ "Age" ].Value = intAge;
objUpdateAge.Parameters[ "Original_NAME" ].Value =
strUserName;
objUpdateAge.ExecuteNonQuery();
objOleDbConnection.Close();
objUpdateAge[ "Age" ].Value = intAge;
The UPDATE statement on line 1 is malformed. Also, no connection is made to the database before the UPDATE is attempted. For this example, a sample database named Sample.mdb has been provided in the bin\debug directory. Follow the instructions previously presented in this tutorial to establish a connection to the database. The complete incorrect code reads:
// UpdateAge.cs (Incorrect)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace UpdateAge
{
///
/// Summary description for FrmUpdateAge.
///
public class FrmUpdateAge : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblAgeOut;
private System.Windows.Forms.Label lblNameOut;
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.Label Label1;
private System.Data.OleDb.OleDbConnection objOleDbConnection;
private System.Data.OleDb.OleDbCommand objUpdateAge;
private System.Data.OleDb.OleDbCommand objSelectAge;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public FrmUpdateAge()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent
// call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
// Windows Form Designer generated code
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run( new FrmUpdateAge() );
}
// updates an age in the database "sample.mdb" located in
// the "bin\debug" directory
private void FrmUpdateAge_Load(
object sender, System.EventArgs e )
{
int intAge = 27;
string strUserName = "Bill";
objUpdateAge[ "Age" ].Value = intAge;
objUpdateAge.Parameters[ "Original_NAME" ].Value =
strUserName;
objUpdateAge.ExecuteNonQuery();
objOleDbConnection.Close();
// display the updated data
objOleDbConnection.Open();
OleDbDataReader objReader =
objSelectAge.ExecuteReader();
objReader.Read();
lblAgeOut.Text = Convert.ToString( objReader[ "Age" ] );
objReader.Close();
objOleDbConnection.Close();
} // end method FrmUpdateAge_Load
} // end class FrmUpdateAge
}
The complete corrected code should read:
// UpdateAge.cs (Correct)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace UpdateAge
{
///
/// Summary description for FrmUpdateAge.
///
public class FrmUpdateAge : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblAgeOut;
private System.Windows.Forms.Label lblNameOut;
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.Label Label1;
private System.Data.OleDb.OleDbConnection objOleDbConnection;
private System.Data.OleDb.OleDbCommand objUpdateAge;
private System.Data.OleDb.OleDbCommand objSelectAge;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public FrmUpdateAge()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent
// call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
// Windows Form Designer generated code
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run( new FrmUpdateAge() );
}
// updates an age in the database "sample.mdb" located in
// the "bin\debug" directory
private void FrmUpdateAge_Load(
object sender, System.EventArgs e )
{
int intAge = 27;
string strUserName = "Bill";
objUpdateAge.Parameters[ "Age" ].Value = intAge;
objUpdateAge.Parameters[ "Original_NAME" ].Value =
strUserName;
objOleDbConnection.Open();
objUpdateAge.ExecuteNonQuery();
objOleDbConnection.Close();
// display the updated data
objOleDbConnection.Open();
OleDbDataReader objReader =
objSelectAge.ExecuteReader();
objReader.Read();
lblAgeOut.Text =
Convert.ToString( objReader[ "Age" ] );
objReader.Close();
objOleDbConnection.Close();
} // end method FrmUpdateAge_Load
} // end class FrmUpdateAge
}
You might also like to view...
Suppose the following sequence of elements are pushed onto a stack named myStack in the following order: 50, 26, 32, 18, 26, 51. What is the output of the following code?
What will be an ideal response? ``` for (int count = 1; count <=3; count++) System.out.println(myStack.pop() ); ```
A(n) ____________________ file is a series of characters or bytes for which PHP attaches no special meaning.
Fill in the blank(s) with the appropriate word(s).