Slide 10.14: C#: NextPage.aspx.cs
Slide 11.1: ASP.NET versions
Home

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

Line 06: Page_Load event
The Page_Load event is triggered when a page loads, and ASP.NET will automatically call the subroutine Page_Load, and execute the code inside it.

Line 07: Request.QueryString( variable ) collection
Retrieves the values of the variables in the HTTP query string. The HTTP query string is specified by the values following the question mark (?). Several different processes can generate a query string. For example, the following anchor tag generates a variable named string with the value “a sample.”
   <a href="example?string=a sample">string sample</a>
Line 14: Page.IsPostBack property
Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.
NextPage.aspx.cs
using System;
using System.Data.OleDb;

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

 protected void Page_Load( object sender, EventArgs e ) {
  if ( Request.QueryString["type"] == "delete" ) {
   Result.Text = "The author, " +
     Request.QueryString["authorName"] + ", is deleted.";
  }
  else {
   Result.Text = "The author, " +
     Request.QueryString["authorName"] + ", is inserted.";
   if ( !Page.IsPostBack ) {
    OleDbConnection conn = new OleDbConnection(
      @"Provider=Microsoft.ACE.OleDb.12.0;Data Source=" +
      Server.MapPath( "Access\\bookstore.mdb" ) );
    String sql = "INSERT INTO authors( authorName, email )";
    sql += "VALUES( @authorName, @email )";
    OleDbCommand cmd = new OleDbCommand(sql, conn);
    cmd.Parameters.AddWithValue( "@authorName", Request.QueryString["authorName"] );
    cmd.Parameters.AddWithValue( "@email", Request.QueryString["email"] );
    conn.Open( );
    cmd.ExecuteNonQuery( );
    cmd.Dispose( );
    conn.Close( );
   }
  }
 }

 protected void home_Click( object sender, EventArgs e ) {
  Response.Redirect( "Default.aspx" );
 }

 protected void authorList_SelectedIndexChanged( object sender, EventArgs e ) {
  OleDbConnection conn = new OleDbConnection(
    @"Provider=Microsoft.ACE.OleDb.12.0;Data Source=" +
    Server.MapPath( "Access\\bookstore.mdb" ) );
  String sql = "SELECT authorID, authorName, email ";
  sql += "FROM authors WHERE authorName = @authorName";
  OleDbCommand cmd = new OleDbCommand( sql, conn );
  cmd.Parameters.AddWithValue( "@authorName", authorList.SelectedValue );
  conn.Open( );
  var dbread = cmd.ExecuteReader( );
  author.DataSource = dbread;
  author.DataBind( );
  dbread.Close( );
  conn.Close( );
 }
}