locked
Help with Session RRS feed

  • Question

  • User451074631 posted

    Hello,

    first of all, I'm not a professional, but I know how to program, and I take some interest in programming.

    I'm making a site for a graduation exam, and I kinda got a problem. I use ASP .NET 3.0, and Session isn't working correctly, or I got the syntax wrong, this is how my code looks like:

    Register.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Default2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <link rel="stylesheet" type="text/css" href="StyleSheet.css" />
        <title>הרשם לליריקס</title>
        <script type="text/javascript">
                function check() {
                    if (document.getElementById("uname").value != null && document.getElementById("uname").value != "" && document.getElementById("fname").value != null && document.getElementById("fname").value != "" && document.getElementById("pass").value != null && document.getElementById("pass").value != "" && document.getElementById("mail").value != null && document.getElementById("mail").value != "")
                        document.Form1.submit();
                    else
                        window.alert("חלק מהערכים ריקים, אנא מלאם!");
                    checlmail();
                }
                function checkmail() {
                    var at = false;
                    var iat;
                    var dot = false;
                    var mail = document.getElementById("mail").value;
                    for (var i = 0; i < mail.length-1; i++) {
                        if (mail[i] == '@') {
                            at = !at;
                            iat = i;
                        }
                    }
                    if (at) {
                        for (var i = iat; i < mail.length-1; i++) {
                            if (mail[i] == '.')
                                dot = true;
                        }
                    }
                    if (!at || !dot)
                        window.alert("כתובת המייל אינה תקינה.");
                }
        </script>
    </head>
    <m>
    <img src="logo.png" align="middle" />
    <a href="Default.aspx">בית</a> &nbsp;
    <a href="Search.aspx">חיפוש שירים</a> &nbsp;
    <a href="About.aspx">אודות</a> &nbsp;
    <a href="Users.aspx">אינדקס משתמשים</a> &nbsp;
    <a href="Songs.aspx">אינדקס שירים</a> &nbsp;
    <a href="LogReg.aspx">כניסה והרשמה</a> &nbsp;
    </m>
    
    <body>
        <form id="Form1" method="post" action="Register.aspx" runat="server">
        <div>
        <table>
        <tr>
            <td>שם משתמש:</td>
            <td><input type="text" id="uname" maxlength="15" name="uname" /></td>
        </tr>
        <tr>
            <td>סיסמא:</td>
            <td><input type="password" id="pass" maxlength="18" name="pass" /></td>
        </tr>
        <tr>
            <td>אי-מייל:</td>
            <td><input type="text" id="mail" maxlength="25" name="mail" /></td>
        </tr>
        <tr>
            <td>שם פרטי</td>
            <td><input type="text" id="fname" maxlength="15" name="fname" /></td>
        </tr>
        <tr>
            <td>שם משפחה:</td>
            <td><input type="text" id="lname" maxlength="15" name="lname" /></td>
        </tr>
        <tr>
            <td>איזור בארץ:</td>
            <td>
                <input id="center" type="radio" value="מרכז" name="region" />מרכז
                <br />
                <input id="north" type="radio" value="צפון" name="region" />צפון
                <br />
                <input id="south" type="radio" value="דרום" name="region" />דרום
            </td>
        </tr>
        </table>
        <br />
        <input type="button" value="שלח!" onclick="check()" />
        </div>
        </form>
    </body>
    </html>
    
    
    
    Register.aspx.cs:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session("username") != null)
            {
                if (IsPostBack)
                {
                    string uname = Request.Form["uname"];
                    string pass = Request.Form["pass"];
                    string mail = Request.Form["mail"];
                    string fname = Request.Form["fname"];
                    string lname = Request.Form["lname"];
                    string region = Request.Form["region"];
                    string alert;
                    string dB = "Database.mdf";
                    string selectQuery = "SELECT * FROM Users WHERE Username = '" + uname + "'";
                    if (MyAdoHelper.IsExist(dB, selectQuery))
                    {
                        alert = "<script type=\"text/javascript\">window.alert(\"שם המשתמש כבר קיים במערכת!\"); window.location = \"Register.aspx\"; </script>";
                    }
                    else
                    {
                        selectQuery = "INSERT INTO Users (Username, Email, Password, FirstName, LastName, Region) VALUES ('" + uname + "','" + mail + "','" + pass + "','" + fname + "','" + lname + "','" + region + "')";
                        MyAdoHelper.DoQuery(dB, selectQuery);
                        alert = "<script type=\"text/javascript\">window.alert(\"נרשמת בהצלחה! אתה כעת מוחזר לעמוד ההתחברות\"); window.location = \"LogReg.aspx\";</script>";
                    }
                    Response.Write(alert);
                    Response.End();
                }
            }
            else
                Response.Write("<script type=\"text/javascript\">window.alert(\"הנך כבר מחובר\"); window.location = \"LogReg.aspx\";</script>");
        }
    }
    
    
    
    
    Don't mind the hebrew, it's just a message to the user.
    Well, VS2010 tells me "The name Session does not exist in the current context", but I see my friend used the same thing, and it worked correctly.
    I don't get what's my mistake here, and I'll be glad if you help me with that :)
    Friday, June 10, 2011 4:28 AM

Answers

  • User1288683547 posted

    I think already a thread started with same question...

    anyhow change code like

    Session("username") to Session["username"]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 10, 2011 6:57 AM

All replies

  • User1288683547 posted

    I think already a thread started with same question...

    anyhow change code like

    Session("username") to Session["username"]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 10, 2011 6:57 AM
  • User-1465674731 posted

    are you able to get the values in session in debug mode? try this..

    Friday, June 10, 2011 7:07 AM
  • User451074631 posted

    Thank you, that helped a lot!

    Friday, June 10, 2011 8:54 AM
  • User1363212110 posted

    you have refer Session with square braces like Session["sessionName"]

    Where are you assigning the username to Session? 

    Friday, June 10, 2011 8:58 AM
  • User451074631 posted

    In the login screen, right after the MyAdoHelper ifs.

    The new code works perfectly:

    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;
    
    public partial class Default2 : System.Web.UI.Page
    {
        public static int WhereIs(string a, char b)
        {
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] == b)
                    return i;
            }
            return 0;
        }
        public string str;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] != null)
                str = "שלום " + Session["username"] + " &nbsp; אם ברצוך להתנתק, לחץ <a href=\"Logout.aspx\"> כאן </a> <br />";
            if (IsPostBack)
            {
                string uname = Request.Form["uname"];
                string pass = Request.Form["pass"];
                string dB = "Database.mdf";
                string uquery = "SELECT * FROM Users";
                string pquery = "SELECT * FROM Users WHERE Password = '" + pass + "'";
                string res;
                if (MyAdoHelper.IsExist(dB, uquery))
                {
                    if (MyAdoHelper.IsExist(dB, pquery))
                    {
                        res = MyAdoHelper.printDataTable(dB, "SELECT FirstName FROM Users WHERE Username = '" + uname + "'");
                        res = res.Remove(0, 26);
                        res = res.Remove(WhereIs(res, '<'));
                        Response.Write("<script type=\"text/javascript\">window.alert(\"שלום" + res + "\"); window.location = \"Default.aspx\"; </script>");
                        Session.Add("username", uname);
    
                    }
                    else
                    {
                        res = ("<script type=\"text/javascript\">window.alert(\"סיסמא שגויה!!\"); window.location = \"LogReg.aspx\"; </script>");
                        Response.Write(res);
                    }
                }
                else
                {
                    res = ("<script type=\"text/javascript\">window.alert(\"שם משתמש שגוי!\"); window.location = \"LogReg.aspx\"; </script>");
                    Response.Write(res);
                }
            }
        }
    }
    Friday, June 10, 2011 9:47 AM