Slide 10.13: C#: Default.aspx.cs (cont.)
Slide 10.15: C#: NextPage.aspx.cs (cont.)
Home

C#: NextPage.aspx.cs


The NextPage.aspx.cs includes the following three event handlers:
Home_Click
It goes back to the page Default.aspx.
Page_Load
When the page is loaded, it performs the following three tasks:

  • Prints an appropriate message, insertion or deletion.
  • Inserts the author if an insertion was submitted from the page Default.aspx with the following SQL command:
       INSERT INTO  authors( authorName, email )
         VALUES( @authorName, @email )
  • Populates the dropdownlist with the SqlDataSource authors.

SelectedIndexChanged
It displays the author’s data when he/she is selected by the dropdown list with the following SQL command:
   SELECT  authorID, authorName, email
     FROM  authors  WHERE  authorName = @authorName
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( );
 }
}