title: login with admin and user using asp.net c#
body: <p>help me please. I'm a newbie. Found this solution here too. The authentication worked but the redirection part doesn't. It always redirects to Default.Aspx tho the admins should be redirected to Add.aspx Please help :'( Thanks in advance!</p>
  
  <p>Here's my code.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Configuration;
  using System.Data.SqlClient;
  using System.Data;
  
  namespace CRUD
  {
  public partial class Login1 : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  
      protected void Button1_Click(object sender, EventArgs e)
      {
          string conn = "";
          conn = ConfigurationManager.ConnectionStrings["employee1ConnectionString"].ToString();
          SqlConnection objsqlconn = new SqlConnection(conn);
          objsqlconn.Open();
  
          SqlCommand cmd = new SqlCommand("select * from userdata where username=@username and password=@password", objsqlconn);
          cmd.Parameters.AddWithValue("@username", TextBox1.Text);
          cmd.Parameters.AddWithValue("@password", TextBox1.Text);
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataTable dt = new DataTable();
  
          da.Fill(dt);
          if (dt.Rows.Count &gt; 0)
          {
              if (dt.Rows[0]["permission"].ToString() == "admin")
                  Response.Redirect("Add.aspx");
              else
                  Response.Redirect("Default.aspx");
  
          }
          else
          {
              Label1.Text = "Invalid username or password. Please try again.";
          }
  
      }
  }
  }
  </code></pre>
  
  <p>and...</p>
  
  <pre><code> &lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login1.aspx.cs" Inherits="CRUD.Login1"       %&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;&lt;/title&gt;
  &lt;style type="text/css"&gt;
      .style1
      {
          text-align: center;
      }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
  
  &lt;/div&gt;
  &lt;p class="style1"&gt;
      LOG IN&lt;/p&gt;
  &lt;p class="style1"&gt;
      &amp;nbsp;&lt;/p&gt;
  &lt;p class="style1"&gt;
      Username:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
      &lt;asp:TextBox ID="TextBox1" runat="server" Width="167px"&gt;&lt;/asp:TextBox&gt;
  &lt;/p&gt;
  &lt;p class="style1"&gt;
      Password:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
      &lt;asp:TextBox ID="TextBox2" runat="server" Width="167px"&gt;&lt;/asp:TextBox&gt;
  &lt;/p&gt;
  &lt;p class="style1"&gt;
  </code></pre>
  
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          
      </p>
      
          
      </p>
      
          Not a member yet? Click 
          here
          .</p>
        
       
       </p>
  
  <p>Table structure:
  db: userdata
  -username
  -password
  -permission</p>