Slide 10.12: C#: Default.aspx.cs (cont.)
Slide 10.14: C#: NextPage.aspx.cs
Home

C#: Default.aspx.cs (Cont.)

Line 16: cmd.Parameters.AddWithValue(
    "@authorName", authorName.Text );
It adds a value to the end of the OleDbParameterCollection.

Line 17: conn.Open( );
Opens a database connection with the property settings specified by the ConnectionString.

Line 19: cmd.ExecuteNonQuery( );
Executes an SQL statement against the Connection and returns the number of rows affected.

Line 20: cmd.Dispose( );
Releases all resources used by the Component.

Line 21: conn.Close( );
Closes the connection to the data source.

Line 35: OleDbDataReader dbread = cmd.ExecuteReader( );
Sends the CommandText to the Connection, and builds an OleDbDataReader using one of the CommandBehavior values. Sends the CommandText to the Connection and builds an OleDbDataReader, which provides a way of reading a forward-only stream of data rows from a data source.

Line 36: Repeater.DataSource = dbread;
Gets or sets the data source that provides data for populating the list.

Line 37: Repeater.DataBind( );
Use the DataBind method to bind the data source specified by the DataSource property to the Repeater control. When you bind a data source to the Repeater control, the information in the data source is displayed in the control.
Default.aspx.cs
using System;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page {

 protected void insert_Click( object sender, EventArgs e ) {
  Response.Redirect( "NextPage.aspx?authorName=" +
    authorName.Text + "&email=" + email.Text + "&type=insert" );
 }

 protected void delete_Click( object sender, EventArgs e ) {
  OleDbConnection conn = new OleDbConnection(
    @"Provider=Microsoft.ACE.OleDb.12.0;Data Source=" +
    Server.MapPath( "Access\\bookstore.mdb" ) );
  String sql = "DELETE FROM authors WHERE authorName=@authorName";
  OleDbCommand cmd = new OleDbCommand( sql, conn );
  cmd.Parameters.AddWithValue( "@authorName", authorName.Text );
  conn.Open( );
  cmd.ExecuteNonQuery( );
  cmd.Dispose( );
  conn.Close( );
  Response.Redirect( "NextPage.aspx?authorName=" +
    authorName.Text + "&type=delete" );
 }

 protected void search_Click( object sender, EventArgs e ) {
  string connString = "Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + 
    Server.MapPath( "Access\\bookstore.mdb" );
  OleDbConnection conn = new OleDbConnection( connString );
  String sql = "SELECT authorID, authorName, email FROM authors " +
               "WHERE authorName LIKE @authorName";
  OleDbCommand cmd = new OleDbCommand( sql, conn );
  cmd.Parameters.AddWithValue( "@authorName", "%" + authorName.Text + "%" );
  conn.Open( );
  OleDbDataReader dbread = cmd.ExecuteReader( );
  searchResult.DataSource = dbread;
  searchResult.DataBind( );
  dbread.Close( );
  conn.Close( );
  cmd.Dispose( );
 }
}