Response.Cookies[“path"].Value = Request.Url.AbsolutePat

本文关键字:Value Request Url AbsolutePat quot Cookies path Response | 更新日期: 2023-09-27 18:10:42

请帮助我,实际上我想如果用户是登录的,那么用户可以检查
课程信息,否则会跳转到登录页面然后返回courseinfo
页面,但登录后给出错误

我的代码是aspx

<%@ Page Language="C#" MasterPageFile="~/Secret for Management/MasterPage.master"
AutoEventWireup="true" CodeFile="Course_info.aspx.cs" Inherits="Secret_Course_info"
Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
<meta http-equiv="Content-Language" content="English" />
<meta name="Author" content="MominaNisar ()" />
<meta name="MominaNisar" content="index,follow" />
<meta name="Description" content="CENTER MANAGEMENT" />
<meta name="Keywords" content="CENTER ,MANAGEMENT" />
<link rel="stylesheet" type="text/css" href="../style.css" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<li><a href="../Secret for all user/Default.aspx" accesskey="c"><span         
class="key">H</span>ome</a></li>
<li><a href="../Secret for all user/Management.aspx" accesskey="g"><span class="key">
    M</span>anagement</a></li>
<li><a href="../Secret for all user/student.aspx" accesskey="s"><span     
 class="key">S</span>tudent</a></li>
<li class="active"><span class="key">C</span>ourse</li>
<li><a href="../Secret for all user/Exam_schedule.aspx" accesskey="x">Exam<span 
 class="key">  
    S</span>chedule</a></li>
<li><a href="../Secret for all user/Notice_Board.aspx" accesskey="n"><span class="key">
    N</span>otice Board</a></li>
<li><a href="../Secret for all user/Visiter_info.aspx" accesskey="n"><span class="key">
    V</span>isitor</a></li>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Menu_left" runat="Server">
<li><a href="../Secret for all user/Course_of_Management.aspx" accesskey="n"><span
    class="key">M</span>anagement</a></li>
<li><a href="../Secret for all user/Course_of_Computer_science.aspx" accesskey="f">       
<span
    class="key">C</span>omputer Science</a></li>
<li><a href="../Secret for all user/Course_of_Other.aspx" accesskey="F"><span 
class="key">
O</span>ther</a></li>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="Leftcol610" runat="Server">
<h3>
    Courses Info
</h3>
<asp:UpdatePanel ID="up1" runat="server">
    <ContentTemplate>
        <asp:Panel ID="pn" DefaultButton="btnSubmit" runat="server">
            <table align="center">
                <tr>
                    <td colspan="3">
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td class="active">
                        &nbsp;
                    </td>
                    <td class="active">
                        Course Name :
                    </td>
                    <td>
                        <asp:TextBox ID="Course_Name" runat="server" />
       <asp:RequiredFieldValidator ID="RequiredFieldValidator1"    runat="server" 
   SetFocusOnError="True" ControlToValidate="Course_Name">*
    </asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="active">
                        &nbsp;
                    </td>
                    <td class="active">
                        Course Duration :
                    </td>
                    <td>
                        <asp:TextBox ID="Course_Duration" runat="server" />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
  SetFocusOnError="True" ControlToValidate="Course_Duration">*  
  </asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="active">
                        &nbsp;
                    </td>
                    <td class="active">
                        Course Qualification :
                    </td>
                    <td>
                        <asp:TextBox ID="Course_Qualification" runat="server" />
      <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
      SetFocusOnError="True"   ControlToValidate="Course_Qualification">*
        </asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="active">
                        &nbsp;
                    </td>
                    <td class="active">
                        Course Total Fees :
                    </td>
                    <td>
                        <asp:TextBox ID="Course_Total_Fees" runat="server" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
   SetFocusOnError="True" ControlToValidate="Course_Total_Fees">*  
   </asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="active">
                        &nbsp;
                    </td>
                    <td class="active">
                        Course Fees Method :
                    </td>
                    <td>
                        <asp:TextBox ID="Course_Fees_Method" runat="server" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
    SetFocusOnError="True" ControlToValidate="Course_Fees_Method">*  
    </asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="active" colspan="3">
       <asp:Label ID="lblerror" CssClass="popuptitle" Style="color: Red" runat="server"
                            Width="465px" Visible="False" />
                    </td>
                </tr>
                <tr>
                    <td class="active" colspan="3" align="center">
                        <asp:Button Text="SUBMIT" ID="btnSubmit" runat="server" 
                             onclick="btnSubmit_Click" Visible="False" />
                    </td>
                </tr>
            </table>
        </asp:Panel>
        </ContentTemplate>
   </asp:UpdatePanel>
   </asp:Content>


   aspx.cs

 using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Secret_Course_info : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader rea;
protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Cookies["UserName"] != null)
    {
        if (Context.User.Identity.Name.ToString() == "admin")
        {
            btnSubmit.Visible = true;
        }
        else
        {
            btnSubmit.Visible = false;
            lblerror.Visible = true;
            lblerror.Text = "::This Page Only For Administrator::";
        }
    }
    else
    {
        Response.Cookies["path"].Value = Request.Url.AbsolutePath.ToString();
        /* now after login i want that it reverts to the page but it gives the error object reference is not set to instance of the type*/
        Response.Redirect("Login.aspx");
    }
  }
   protected void btnSubmit_Click(object sender, EventArgs e)
   {
    int total;
    total = 0;
    String strcon = "Data Source=localhost;Initial   
    Catalog=Student_BioData;user=aaa;pwd=123;";
    string strsql = "select * from Courses ";
    con = new SqlConnection(strcon);
    cmd = new SqlCommand();
    try
    {
        con.Open();
        cmd.Connection = con;
        cmd.CommandText = strsql;
        rea = cmd.ExecuteReader();
        while (rea.Read()) { total++; }
    }
    catch (SqlException ex) { lblerror.Text = ex.Message.ToString(); }  
    total += 1;
    try
    {
     strsql = "Insert into Courses values('"+
       Course_Name.Text.Trim()+ "','" +
        Course_Duration.Text.Trim() + "','" +
        Course_Qualification.Text.Trim() + "','" +
        Course_Total_Fees.Text.Trim() + "','" +
        Course_Fees_Method.Text.Trim()+ "')";
    con = new SqlConnection(strcon);
    cmd = new SqlCommand();
        con.Open();
        cmd.Connection = con;
        cmd.CommandText = strsql;
        cmd.ExecuteNonQuery();
            lblerror.Visible = true;
            lblerror.Text = " Data Save In Courses info ";
      }
    catch (SqlException ex) { lblerror.Text = ex.Message.ToString(); }   
     }
   }

  stack trace

    [NullReferenceException: Object reference not set to an instance of an object.]
     Loginpage.btnLogIn_Click(Object sender, EventArgs e) in d:'Momina'installed     
    setup'Center Management'Center Management'Login.aspx.cs:53
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
      System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler
   .RaisePostBackEvent(String eventArgument) +10
      System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String   
       eventArgument) +13
         System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
        System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,
        Boolean includeStagesAfterAsyncPoint) +5563


   it gives error like
    //Response.Redirect("UpdateProfile.aspx");
    Line 52:             Response.Cookies["UserName"].Value = txtUserName.Text;
 Line 53: Response.Redirect(Request.Cookies["path"].Value.ToString());//herecomes error 
         Line 54:         }
        Line 55:         else

    login.aspx
   <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="Login.aspx.cs" Inherits="Loginpage" Title="Loginpage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
<meta http-equiv="Content-Language" content="English" />
<meta name="Author" content="Momina Nisar " />
<meta name="Momina Nisar " content="index,follow" />
<meta name="Description" content="STUDENT MANAGEMENT SYSTEM" />
<meta name="Keywords" content="STUDENT,MANAGEMENT" />
<link rel="stylesheet" type="text/css" href="style.css" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<li><a href="Default.aspx" accesskey="h"><span class="key">H</span>ome</a></li>
<li><a href="Management.aspx" accesskey="g"><span class="key">M</span>anagement</a>     
</li>
<li><a href="student.aspx" accesskey="s"><span class="key">S</span>tudent</a></li>
<li><a href="Course_Infor.aspx" accesskey="c"><span class="key">C</span>ourse</a></li>
<li><a href="Exam_schedule.aspx" accesskey="x">Exam<span class="key"> 
S</span>chedule</a></li>
<li><a href="Notice_Board.aspx" accesskey="n"><span class="key">N</span>otice Board</a>
</li>
<li><a href="Visiter_info.aspx" accesskey="n"><span class="key">V</span>isitor</a></li>
 </asp:Content>
 <asp:Content ID="Content3" ContentPlaceHolderID="Menu_left" runat="Server">
<li><a href="Login.aspx" accesskey="l"><span class="key">L</span>ogin</a></li>
<li><a href="Contas_us.aspx" accesskey="u"><span class="key">C</span>ontact Us</a></li>
<li><a href="Feedback.aspx" accesskey="f">Feed<span class="key">B</span>ack</a></li>
</asp:Content>
 <asp:Content ID="Content4" ContentPlaceHolderID="Leftcol610" runat="Server">
<fieldset>
 <legend>Login</legend> 
<div class='container'>
<asp:Label ID="Name" runat="server" Text="UserName:" CssClass="lbl"/>
<br/>
 <asp:TextBox ID="txtUserName" runat="server" Height="22px"/>
 <asp:RequiredFieldValidator ID="RV1" runat="server" 
                        ControlToValidate="txtUserName" 
                        ErrorMessage="Please Enter User Name" 
                        SetFocusOnError="True">*
 </asp:RequiredFieldValidator><br />
 </div>
 <div class='container'>
 <asp:Label ID="lblPwd" runat="server" Text="Password:" CssClass="lbl"/>
 <br/>
 <asp:TextBox ID="txtPwd" runat="server" TextMode="Password" 
                     CssClass="pwd" Height="22px"/>
 <asp:RequiredFieldValidator ID="RV2" runat="server" 
                        ControlToValidate="txtPwd" 
                        ErrorMessage="Your Password" 
                        SetFocusOnError="True">*
 </asp:RequiredFieldValidator><br />
 </div>
  <div class='container'>
   <asp:Button ID="btnLogIn" runat="server" Text="Sign In" 
                      onclick="btnLogIn_Click"/>
  </div>
 <div class='container'>
 <asp:HyperLink ID="HyperLink1" runat="server" 
 NavigateUrl="~/ForgotPassword.aspx">Forgot Password ?</asp:HyperLink>
 <br/>
 </div>
 <div class='short_explanation'>New User ? 
 <asp:HyperLink ID="HyperLink2" runat="server" 
  NavigateUrl="~/Createuser.aspx">SignUp !</asp:HyperLink></div>                       
  <asp:ValidationSummary ID="ValidationSummary1" 
                   runat="server" CssClass="error"/>
  <br /><br />
   <asp:Label ID="lblMsg" runat="server" Text="" CssClass="lbl"/>
   </fieldset>
   </asp:Content>
    login.aspx.cs
  using System;
  using System.Collections;
 using System.Configuration;
  using System.Data;
  using System.Linq;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.HtmlControls;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Xml.Linq;
  using System.Data.SqlClient;
  using System.Net;
  using System.Net.Mail;
   public partial class Loginpage : System.Web.UI.Page
    {
   protected void Page_Load(object sender, EventArgs e)
    {
    //Login1.Focus();

      }
    protected void btnLogIn_Click(object sender, EventArgs e)
    {
    String strCon = "Data Source=localhost;Initial     
     Catalog=Student_BioData;user=aaa;pwd=123;";
    string strSelect = "SELECT COUNT(*) FROM integrate WHERE UserName = @UserName AND 
    Password = @Password";
    SqlConnection con = new SqlConnection(strCon);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = strSelect;
    SqlParameter username = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
    username.Value = txtUserName.Text.Trim().ToString();
    cmd.Parameters.Add(username);
    SqlParameter password = new SqlParameter("@Password", SqlDbType.VarChar, 50);
    password.Value = txtPwd.Text.Trim().ToString();
    cmd.Parameters.Add(password);
    con.Open();
    int result = (Int32)cmd.ExecuteScalar();
    con.Close();
    if (result >= 1)
    {
        Session["UserName"] = txtUserName.Text.ToString().Trim();
        //Response.Redirect("UpdateProfile.aspx");
        Response.Cookies["UserName"].Value = txtUserName.Text;
        Response.Redirect(Request.Cookies["path"].Value.ToString());
    }
    else
        lblMsg.Text = "Incorrect Username or Password";
     }
    }

请帮助我,实际上我想如果用户是登录的,那么用户可以检查
课程信息,否则会跳转到登录页面然后返回courseinfo
页面,但是登录后显示错误

Response.Cookies[“path"].Value = Request.Url.AbsolutePat

检查null引用

Response.Cookies["path"].Value = Request.Url != null ? Request.Url.AbsolutePath : string.Empty;

Response.Cookies["path"].Value = Request.UrlReferrer != null ? Request.UrlReferrer.AbsolutePath : string.Empty;