• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 975
  • Last Modified:

Session.Remove doesn't seem to work.

I have a form that sends an email which is brought via a session from another page.  Upon clicking the send button, I would like that session to be killed.

I am attaching code below.  I have a label for testing that displays a value if the session is not null or "No data!" if it is null.  If I submit the form, return to it, and hit refresh, I still see the value instead of "No data!"  If I close the browser, the session is eliminated.

I would like the session to end as soon as the data is no longer needed.  It's only 8 characters, but that could add up some day.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Web.Mail;
 
namespace email_form
{
    public partial class _Default : System.Web.UI.Page
    {
 
        // Global variables.
        string strDomain;
        string strResult;
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["emailUser"] != null)
            {
                // Get a string from the session variable.
                string strEmailUser = Session["emailUser"].ToString();
                // Set a string with the domain.
                strDomain = "@co.frederick.va.us";
                // Concatenate the user and domain into a new string.
                strResult = strEmailUser + strDomain;
                lblTestSession.Text = strResult;
            }
            else
            {
                lblTestSession.Text = "No data!";
            }
        }
 
        protected void btnSend_Click(object sender, EventArgs e)
        {
            MailMessage objMail0 = new MailMessage();
 
            // Initial setup.
            objMail0.To = strResult;
            objMail0.Cc = Server.HtmlEncode(txtCC.Text);
            string strCC = txtCC.Text;
            objMail0.From = Server.HtmlEncode(txtSenderEmail.Text);
            string strFrom = Server.HtmlEncode(txtSenderEmail.Text);
            objMail0.Headers.Add("Reply-To", Server.HtmlEncode(txtSenderEmail.Text));
            objMail0.BodyFormat = MailFormat.Text;
            objMail0.Priority = MailPriority.Normal;
            objMail0.Subject = Server.HtmlEncode(txtSubject.Text);
            string strToday = DateTime.Now.ToString();
            string strIPAddress = Request.ServerVariables["remote_addr"];
            // System.Net.IPHostEntry host = new System.Net.IPHostEntry();
            // host = System.Net.Dns.GetHostByAddress(Request.ServerVariables["REMOTE_HOST"]);
            // string strMachineName = host.HostName;
            string strMessage = Server.HtmlEncode(txtMessage.Text);
 
            //Format the email.
            objMail0.Body = "**This message was sent via the website email form.**" +
                            "\r\n\r\nFrom: " + strFrom +
                            "\r\nIP address: " + strIPAddress + 
                            // "\r\nHost: " + strMachineName + 
                            "\r\nDate: " + strToday + 
                            "\r\nCC: " + strCC +
                            "\r\n\r\nMessage: " + "\r\n" + strMessage
                            ;
 
            //Send the email. 
            SmtpMail.SmtpServer = "10.16.231.234";
            try
            {
                SmtpMail.Send(objMail0);
            }
            catch (Exception ex)
            {
                Response.Redirect("problem.html");
            }
 
            Response.Redirect("thanks.html");
 
            // Kill the session.
            Session.Remove("emailUser");
        }
    }
}

Open in new window

0
mrcoulson
Asked:
mrcoulson
  • 5
  • 4
2 Solutions
 
BuginatorCommented:
Hi,

Session.Remove("emailUser") should theoretically remove the session. So that's a little strange.

Is this your only session in the web application? In that case, you can use Session.Abandon();

You can also try Session("emailUser") = null; although this won't delete the session itself.
0
 
BuginatorCommented:
By the way, you say you still see "No data!" after the click event, but that isn't strange when you remove the session?

In your Page_Load it display's "No data!" if the session is null. And in the click event you remove the session at the bottom of your code, and when you then return back to the page you will see "No data!", because, after all,  the session got deleted.

Or have someone of us misunderstood something?
0
 
M3mph15Commented:
Hi,

I agree that it is strange that it doesn't remove it. Another alternative to testing if the session is null is to test the length of it. It is possible to technically have the session variable not null but just have an empty string.

So grab into a string like you are and then test if Length = 0. This way will also make the user unable to submit when there is no Email User info.

Also when you "kill" the session you can just replace the current value with an empty string.

Hope this helps
-M3mph15
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mrcoulsonAuthor Commented:
Buginator: Yeah, I tried Session.Abandon() as well.  And for your second question: I see an email address when I should see "No data!".  If I fill out the form and hit the button, it processes all that stuff and then the last thing it does is kill the session.  So, if I come back to Default.aspx without first getting there by way of a link, I should see "No data!", but instead I see the email address.  

Maybe I need to kill the session earlier.  Maybe it doesn't need to be around that far.  Perhaps I can kill it as soon as variables are assigned from its data.

M3mph15: I'll give it a shot.

Jeremy
0
 
mrcoulsonAuthor Commented:
Got my own solution to it.  The answer was to put Session.Remove("emailUser") in the Page_Load.  Points nonetheless because you guys helped!

Jeremy
0
 
BuginatorCommented:
> Also when you "kill" the session you can just replace the current value with an empty string.

This is just what I said, but more inefficient.
Session("emailUser") = null; uses less resources than Session("emailUser") = "";
0
 
mrcoulsonAuthor Commented:
Thanks.  I learned two things from your posts and that's very important for me.
0
 
mrcoulsonAuthor Commented:
So, now that the question is closed, this is just for the sake of my own education.

This variable will only hold email usernames.  Our email usernames are limited to 8 characters.  Therefore, this variable will never hold more than 8 characters.  If garbage collection happens on sessions after 20 minutes, is this really something to be concerned about?  
0
 
BuginatorCommented:
No this isn't a problem. It depends on the number of users using the server at the same time. But if you use few sessions, and it still becomes a problem, it's time to find other solutions or upgrade the server. A session containing a username/id is very often the main session since it's used to control user restrictions, so you might say that this particular session is the most important one in many cases.
0
 
mrcoulsonAuthor Commented:
Ah ha!  There's more.

Killing the session here:

if (Session["emailUser"] != null)
            {
                // Get a string from the session variable.
                string strEmailUser = Session["emailUser"].ToString();
                // Set a string with the domain.
                strDomain = "@co.frederick.va.us";
                // Concatenate the user and domain into a new string.
                strResult = strEmailUser + strDomain;
                lblTestSession.Text = strResult;
                Session.Remove("emailUser");


made the emailing function break.

So, I just switched out the place I had it in before.  Now it's here:

            SmtpMail.SmtpServer = "10.16.231.234";
            try
            {
                SmtpMail.Send(objMail0);
            }
            catch (Exception ex)
            {
                Response.Redirect("problem.html");
            }
            Session.Remove("emailUser");
            Response.Redirect("thanks.html");

Before, I had it after Response.Redirect.  Now I kill the session before doing the redirect and all is well!

Jeremy
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now