title: Using LINQ to create a simple login
body: <p>I'm an C# ASP.NET beginner, so please excuse anything that's... not quite right!</p>
  
  <p>In short, I want to create a really basic login system: one which runs through a database and uses sessions so that only logged in users can access certain pages. I know how to do most of that, but I'm stuck with querying data with LINQ on the login page.</p>
  
  <p>On the login page, I have a DropDownList to select a username, a Textbox to type in a password and a button to login (I also have a literal for errors). The DropDownList is databound to a datatable called DT_Test. DT_Test contains three columns: UsernameID (int), Username (nchar(30)) and Password (nchar(30)). UsernameID is the primary key. </p>
  
  <p>I want to make the button's click event query data from the database with the DropDownList and Textbox, in order to check if the username and password match. But I don't know how to do this... </p>
  
  <p>Current Code (not a lot!):</p>
  
  <p>Front End:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login_Test.aspx.cs" Inherits="Login_Login_Test" %&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;Login Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="LoginTest" runat="server"&gt;
      &lt;div&gt;
  
          &lt;asp:DropDownList ID="DDL_Username" runat="server" Height="20px" 
              DataTextField="txt"&gt;
          &lt;/asp:DropDownList&gt;
          &lt;br /&gt;
          &lt;asp:TextBox ID="TB_Password" runat="server" TextMode="Password"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
          &lt;asp:Button ID="B_Login" runat="server" onclick="B_Login_Click" Text="Login" /&gt;
          &lt;br /&gt;
          &lt;asp:Literal ID="LI_Result" runat="server"&gt;&lt;/asp:Literal&gt;
  
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Back End:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public partial class Login_Login_Test : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
              Binder();
              }
      }
  
      private void Binder()
      {
          using (DataClassesDataContext db = new DataClassesDataContext())
          {
              DDL_Username.DataSource = from x in db.DT_Honeys select new { x.UsernameID, txt = x.Username };
              DDL_Username.DataBind();
          }
      }
      protected void B_Login_Click(object sender, EventArgs e)
      {
          if (TB_Password.Text != "")
          {
              using (DataClassesDataContext db = new DataClassesDataContext())
              {
  
              }
          }
  
      }
  }
  </code></pre>
  
  <p>I have spent hours searching and trying different code, but none of it seems to fit in for this context. </p>
  
  <p>Anyway, help and tips appreciated, thank you very much!</p>
  
  <ul>
  <li>I am aware of security risks etc. but this is not a live website or anything, it is simply for testing purposes as a beginner. *</li>
  </ul>
  
  <p>Updated code:</p>
  
  <p>Back End:</p>
  
  <pre><code>    using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public partial class Login_Page_Test : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
              Tester();
          }
      }
  
      private void Tester()
      {
          using (DataClassesDataContext db = new DataClassesDataContext())
          {
              DDL_Username.DataSource = from x in db.DT_Honeys select new { id = x.UsernameID, txt = x.Username };
              DDL_Username.DataValueField = "id";
              DDL_Username.DataTextField = "txt";
              DDL_Username.DataBind();
          }
      }
      protected void B_Login_Click(object sender, EventArgs e)
      {
          if (TB_Password.Text != "")
          {
              using (DataClassesDataContext db = new DataClassesDataContext())
              {
                  DT_Honey blah = new DT_Honey();
                  blah = db.DT_Honeys.SingleOrDefault(x =&gt; x.UsernameID == int.Parse(DDL_Username.SelectedValue.ToString()));
  
                  if (blah != null)
                  {
                      if (TB_Password.Text.ToString().Trim() == blah.Password.ToString())
                      {
                          LI_Result.Text = "Credentials correct.";
                      }
                      else
                      {
                          LI_Result.Text = "Error: credentials are incorrect.";
                  }
                  }
                  else
                  {
                      LI_Result.Text = "Error: null value.";
                  }
  
              }
      }
  
      }
  }
  </code></pre>
  
  <p>Front End:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login_Page_Test.aspx.cs" Inherits="Login_Page_Test" %&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;Logi Page Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="LoginPageTest" runat="server"&gt;
      &lt;div&gt;
  
      &lt;/div&gt;
      &lt;asp:DropDownList ID="DDL_Username" runat="server"&gt;
      &lt;/asp:DropDownList&gt;
      &lt;br /&gt;
      &lt;asp:TextBox ID="TB_Password" runat="server"&gt;&lt;/asp:TextBox&gt;
      &lt;br /&gt;
      &lt;asp:Button ID="B_Login" runat="server" onclick="B_Login_Click" Text="Login" /&gt;
      &lt;br /&gt;
      &lt;asp:Literal ID="LI_Result" runat="server"&gt;&lt;/asp:Literal&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <ul>
  <li>I have checked the database aspects, and it is all fine in terms of primary key, columns, actual data, and dataclasses.</li>
  </ul>
  
  <hr>
  
  <p>Same with textbox:</p>
  
  <p>Back End:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public partial class Login_Login_Page_2 : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
      protected void B_Login_Click(object sender, EventArgs e)
      {
          if (TB_Username.Text.ToString().Trim() != "" &amp;&amp; TB_Password.Text.ToString().Trim() != "")
          {
              using (DataClassesDataContext db = new DataClassesDataContext())
              {
                  DT_Honey Login = new DT_Honey();
                  Login = db.DT_Honeys.SingleOrDefault(y =&gt; y.UsernameID == int.Parse(TB_Username.Text.ToString().Trim()));
  
                  if (Login != null)
                  {
                      if (TB_Password.Text.Trim() == Login.Password.ToString().Trim())
                      {
                          LI_Result.Text = "Yeah! The credentials you entered were correct!";
                      }
                  }
  
                  else
                  {
                      LI_Result.Text = "Oops! There was an error with the credentials you entered; please try again.";
                  }
              }
          }
  
          else
          {
              LI_Result.Text = "Wow! Please fill out &lt;b&gt;both&lt;/b&gt; the Username and Password text fields to login; thank you.";
          }
      }
  }
  </code></pre>
  
  <p>Front End:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login_Page_2.aspx.cs" Inherits="Login_Login_Page_2" %&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;Login Page 2&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="LoginPage2" runat="server"&gt;
      &lt;div&gt;
  
          &lt;asp:TextBox ID="TB_Username" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
          &lt;asp:TextBox ID="TB_Password" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;br /&gt;
          &lt;asp:Button ID="B_Login" runat="server" onclick="B_Login_Click" Text="Login" /&gt;
          &lt;br /&gt;
          &lt;asp:Literal ID="LI_Result" runat="server"&gt;&lt;/asp:Literal&gt;
  
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>