Session

Design







Registeration.aspx Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Registration : System.Web.UI.Page
{
  
    protected void Register_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        con.ConnectionString = @"Data Source=DESKTOP-5H5O8UP\SQLEXPRESS1;Initial Catalog=registrationform;Integrated Security=True";
        con.Open();
        cmd.Connection=con;
        cmd.CommandText = "insert into register values('"+txtname.Text+"','"+txtemail.Text+"','"+txtcontact.Text+"',"+txtage.Text+",'"+txtpass.Text+"','"+txtconfirmpass.Text+"')";
        cmd.ExecuteNonQuery();
        Response.Write("Values inserted");
        Response.Redirect("Login.aspx");
    }
}
            

Login.aspx Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
   
    protected void Login_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        con.ConnectionString = @"Data Source=DESKTOP-5H5O8UP\SQLEXPRESS1;Initial Catalog=registrationform;Integrated Security=True";
        con.Open();
        cmd.Connection = con;
        cmd.CommandText = "select * from register where email='" + txtemail.Text + "' and pass='" + txtpass.Text + "'";
        SqlDataReader dr = cmd.ExecuteReader();
        if(dr.Read())
        {
            Session["reg"] = "admin";
            Response.Redirect("viewCode.aspx");
        }
        else
        {
            Response.Write("inavalid email and password.");
        }
    }
    
}
            

ViewCode.aspx


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class viewCode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if(Session["reg"].ToString()=="admin")
            {
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }
        catch(Exception rr)
        {
            Response.Redirect("Login.aspx");
        }
    }
}
                                    

Add this code in your Web.config file after system.web


         <appsettings>
             <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
         </appsettings>
            

Output

Output Example

Output Example

Output Example