How to use session ??
-
I'm making a login page with ASP.NET with C#. and I know that it needs to use Session for saving information of loggined user. so I used Session like this on "login.aspx.cs" with Login View Control. [login.aspx.cs] // Database query. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Join"].ConnectionString); SqlCommand cmd = new SqlCommand("SELECT * From User WHERE id = @ID", con); cmd.Parameters.AddWithValue("@ID", ((TextBox)LoginView1.FindControl("TextBox1")).Text); string name = rd["name"].ToString(); string pw = rd["password"].ToString(); string pw1 = ((TextBox)LoginView1.FindControl("TextBox2")).Text; string id = rd["id"].ToString(); // Save to Session variables. There are a lot of components about Session. Keys, Item, Add, //and so on.. Session["memberid"] = id; Session["membername"] = name; if ((string.Equals(pw, pw1)) == true) { FormsAuthentication.RedirectFromLoginPage(name, false); } .... And I made codes like this whether lggined user is same with Writer on board or not. [Board.aspx] public static bool IsWriter(string pdsId, string Writer) { string sql = "Select id From t_Pds Where boardId = @boardId And writer = @writer"; SqlCommand cmd = new SqlCommand(sql, DbConn.GetConn()); cmd.Parameters.AddWithValue("@boardId", pdsId); cmd.Parameters.AddWithValue("@writer", Writer); cmd.Connection.Open(); string result = (string)cmd.ExecuteScalar(); // result = ID of writer value. cmd.Connection.Close(); // I can't use Session. When I use "Session", there are no components of Session. // There are only two values of Session. ( SessionPageStatePersister , SessionParameter) if (Session["memberid"] == result) //ERROR. { return true; } else return false; } }