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

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

Line 01: using System;
The using command enables type names to be referenced without namespace qualification and the System namespace contains fundamental classes and base classes that define commonly-used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions.

Line 02: using System.Data.OleDb;
The System.Data namespace provides access to classes that represent the ADO.NET (ActiveX Data Objects) architecture, which lets you build components that efficiently manage data from multiple data sources. The System.Data.OleDb namespace is the .NET Framework Data Provider for OLE DB (Object Linking and Embedding Database). OLE DB is Microsoft’s strategic low-level application program interface (API) for access to different data sources and includes not only the SQL capabilities of the Open Database Connectivity (ODBC) but also includes access to data other than SQL data.

Line 04: public partial class _Default
The class _Default represents the code behind for your Default.aspx page. It is partial so that it can be declared in multiple class file. In this case, all the control declarations are auto generated in another partial class for _Default. Which you normally can’t see, ideally because you don’t need to.

Line 04: public partial class _Default : System.Web.UI.Page
Inherits System.Web.UI.Page, which represents an .aspx file, also known as a Web Forms page, requested from a server that hosts an ASP.NET web application. It inherits from page so you can use the features of the already implemented Page class. This means you can access query strings, complete redirects and many many more features, check out the link for a full list (there are hundreds).
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( );
 }
}