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
01using System;
02using System.Data.OleDb;
03 
04public partial class NextPage : System.Web.UI.Page {
05 
06 protected void Page_Load( object sender, EventArgs e ) {
07  if ( Request.QueryString["type"] == "delete" ) {
08   Result.Text = "The author, " +
09     Request.QueryString["authorName"] + ", is deleted.";
10  }
11  else {
12   Result.Text = "The author, " +
13     Request.QueryString["authorName"] + ", is inserted.";
14   if ( !Page.IsPostBack ) {
15    OleDbConnection conn = new OleDbConnection(
16      @"Provider=Microsoft.ACE.OleDb.12.0;Data Source=" +
17      Server.MapPath( "Access\\bookstore.mdb" ) );
18    String sql = "INSERT INTO authors( authorName, email )";
19    sql += "VALUES( @authorName, @email )";
20    OleDbCommand cmd = new OleDbCommand(sql, conn);
21    cmd.Parameters.AddWithValue( "@authorName", Request.QueryString["authorName"] );
22    cmd.Parameters.AddWithValue( "@email", Request.QueryString["email"] );
23    conn.Open( );
24    cmd.ExecuteNonQuery( );
25    cmd.Dispose( );
26    conn.Close( );
27   }
28  }
29 }
30 
31 protected void home_Click( object sender, EventArgs e ) {
32  Response.Redirect( "Default.aspx" );
33 }
34 
35 protected void authorList_SelectedIndexChanged( object sender, EventArgs e ) {
36  OleDbConnection conn = new OleDbConnection(
37    @"Provider=Microsoft.ACE.OleDb.12.0;Data Source=" +
38    Server.MapPath( "Access\\bookstore.mdb" ) );
39  String sql = "SELECT authorID, authorName, email ";
40  sql += "FROM authors WHERE authorName = @authorName";
41  OleDbCommand cmd = new OleDbCommand( sql, conn );
42  cmd.Parameters.AddWithValue( "@authorName", authorList.SelectedValue );
43  conn.Open( );
44  var dbread = cmd.ExecuteReader( );
45  author.DataSource = dbread;
46  author.DataBind( );
47  dbread.Close( );
48  conn.Close( );
49 }
50}