title: Presentation, Business and Data Layer
body: <p>I just started programming in C# and was reading about dividing your application / website into the three different layers was the best practice but I am having a hard time understanding exactly how. Im working on a pet project to lean more about C# but I dont want to start on any bad habits. Can you look at what I have and see if I am doing this right? Offer some hints suggestions as to how to break everything down to the different layers? </p>
  
  <p><strong>Presentation Layer</strong></p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
    &lt;title&gt;Project: Ruth&lt;/title&gt;
    &lt;link href="CSS/StyleSheet.css" rel="stylesheet" type="text/css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
      &lt;div class="Body"&gt;
        &lt;div class="Header"&gt;
          &lt;div class="Nav"&gt;
            &lt;img src="images/Header_Main.gif" alt="" width="217" height="101" /&gt;
            &lt;div class="Menu"&gt;
              &lt;a href="Default.aspx"&gt;
                &lt;img src="images/Header_Home-Off.gif" alt="" /&gt;&lt;/a&gt;
              &lt;a href="Default.aspx"&gt;
                &lt;img src="images/Header_About-Off.gif" alt="" /&gt;&lt;/a&gt;
              &lt;a href="Register.aspx"&gt;
                &lt;img src="images/Header_Register-Off.gif" alt="" /&gt;&lt;/a&gt;
              &lt;a href="Default.aspx"&gt;
                &lt;img src="images/Header_Credits-Off.gif" alt="" /&gt;&lt;/a&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;div class="Content"&gt;
          &lt;div class="CurrentlyListening"&gt;
            &lt;asp:Label ID="lblCurrentListen" runat="server" Text="(Nothing Now)" CssClass="Txt"&gt;&lt;/asp:Label&gt;
          &lt;/div&gt;
          &lt;asp:GridView ID="gvLibrary" runat="server" AutoGenerateColumns="False" DataKeyNames="lib_id" DataSourceID="sdsLibrary" EmptyDataText="There are no data records to display." Width="760" GridLines="None"&gt;
            &lt;RowStyle CssClass="RowStyle" /&gt;
            &lt;AlternatingRowStyle CssClass="AltRowStyle" /&gt;
            &lt;HeaderStyle CssClass="HeaderStyle" /&gt;
            &lt;Columns&gt;
              &lt;asp:BoundField DataField="artist_name" HeaderText="Artist" SortExpression="artist_name" HeaderStyle-Width="200" /&gt;
              &lt;asp:BoundField DataField="album_title" HeaderText="Album" SortExpression="album_title" HeaderStyle-Width="200" /&gt;
              &lt;asp:BoundField DataField="song_title" HeaderText="Track" SortExpression="song_title" HeaderStyle-Width="200" /&gt;
              &lt;asp:TemplateField HeaderText="DL"&gt;
                &lt;ItemTemplate&gt;
                  &lt;a href="http://####/Proj_Ruth/Data/&lt;%# Eval("file_path") %&gt;" class="lnk"&gt;Link&lt;/a&gt;
                &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
            &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
          &lt;asp:SqlDataSource ID="sdsLibrary" runat="server" ConnectionString="&lt;%$ ConnectionStrings:MusicLibraryConnectionString %&gt;" DeleteCommand="DELETE FROM [Library] WHERE [lib_id] = @lib_id" InsertCommand="INSERT INTO [Library] ([artist_name], [album_title], [song_title], [file_path]) VALUES (@artist_name, @album_title, @song_title, @file_path)" ProviderName="&lt;%$ ConnectionStrings:MusicLibraryConnectionString.ProviderName %&gt;" SelectCommand="SELECT [lib_id], [artist_name], [album_title], [song_title], [file_path] FROM [Library] ORDER BY [artist_name], [album_title]" UpdateCommand="UPDATE [Library] SET [artist_name] = @artist_name, [album_title] = @album_title, [song_title] = @song_title, [file_path] = @file_path WHERE [lib_id] = @lib_id"&gt;
            &lt;DeleteParameters&gt;
              &lt;asp:Parameter Name="lib_id" Type="Int32" /&gt;
            &lt;/DeleteParameters&gt;
            &lt;InsertParameters&gt;
              &lt;asp:Parameter Name="artist_name" Type="String" /&gt;
              &lt;asp:Parameter Name="album_title" Type="String" /&gt;
              &lt;asp:Parameter Name="song_title" Type="String" /&gt;
              &lt;asp:Parameter Name="file_path" Type="String" /&gt;
            &lt;/InsertParameters&gt;
            &lt;UpdateParameters&gt;
              &lt;asp:Parameter Name="artist_name" Type="String" /&gt;
              &lt;asp:Parameter Name="album_title" Type="String" /&gt;
              &lt;asp:Parameter Name="song_title" Type="String" /&gt;
              &lt;asp:Parameter Name="file_path" Type="String" /&gt;
              &lt;asp:Parameter Name="lib_id" Type="Int32" /&gt;
            &lt;/UpdateParameters&gt;
          &lt;/asp:SqlDataSource&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Business Layer</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  public class User
  {
    DA da = new DA();
  
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public string Password { get; set; }
    public string AccessCode { get; set; }
  
    public User(string firstName, string lastName, string emailAddress, string password, string accessCode)
    {
      FirstName = firstName;
      LastName = lastName;
      EmailAddress = emailAddress;
      Password = password;
      AccessCode = accessCode;
    }
  
    public void CreateUser(User newUser)
    {
      if (da.IsValidAccessCode(newUser.AccessCode))
      {
        da.CreateUser(newUser);
      }
    }
  }
  </code></pre>
  
  <p><strong>Data Access Layer (DAL)</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Data;
  using System.Data.SqlTypes;
  using System.Data.SqlClient;
  using System.Configuration;
  
  public class DA
  {
    public DA()
    {
    }
  
    public bool IsValidAccessCode(string accessCode)
    {
      bool isValid = false;
      int count = 0;
  
      using (SqlConnection sqlCnn = new SqlConnection(ConfigurationManager.ConnectionStrings["MusicLibraryConnectionString"].ConnectionString))
      {
        sqlCnn.Open();
        using (SqlCommand sqlCmd = new SqlCommand(String.Format("SELECT COUNT(*) FROM [AccessCodes] WHERE [accessCode_accessCode] = '{0}';", accessCode), sqlCnn))
        {
          count = (int)sqlCmd.ExecuteScalar();
          if (count == 1)
          {
            isValid = true;
          }
        }
      }
      return isValid;
    }
  
    public void CreateUser(User newUser)
    {
      using (SqlConnection sqlCnn = new SqlConnection(ConfigurationManager.ConnectionStrings["MusicLibraryConnectionString"].ConnectionString))
      {
        sqlCnn.Open();
        using (SqlCommand sqlCmd = new SqlCommand(String.Format("INSERT INTO [Users] (user_firstName, user_lastName, user_emailAddress, user_password, user_accessCode) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}');", newUser.FirstName, newUser.LastName, newUser.EmailAddress, newUser.Password, newUser.AccessCode), sqlCnn))
        {
          sqlCmd.ExecuteNonQuery();
        }
      }
      DeleteAccessCode(newUser.AccessCode);
    }
  
    public void DeleteAccessCode(string accessCode)
    {
      using (SqlConnection sqlCnn = new SqlConnection(ConfigurationManager.ConnectionStrings["MusicLibraryConnectionString"].ConnectionString))
      {
        sqlCnn.Open();
        using (SqlCommand sqlCmd = new SqlCommand(String.Format("DELETE FROM [AccessCodes] WHERE [accessCode_accessCode] = '{0}';", accessCode), sqlCnn))
        {
          sqlCmd.ExecuteNonQuery();
        }
      }
    }
  }
  </code></pre>