Slide 10.9: User interface: NextPage.aspx (cont.)
Slide 10.11: C#: Default.aspx.cs (cont.)
Home

C#: Default.aspx.cs


The file Default.aspx.cs is created because you checked the option “Place code in separate file” when create the Default.aspx web form. C# (or ASP.NET) is based on event-driven programming. The page includes the following three event handlers:
insert_Click
It goes to the “NextPage” page with the input name and email by using the GET method.

delete_Click
It deletes the author from the authors table based on the input name and goes to the “NextPage” page.

search_Click
It searches the authors table for the input name by using the following SQL command:
  SELECT  authorID, AuthorName, Email  FROM  authors
    WHERE  authorName  LIKE  %@authorName%
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( );
 }
}