Board index » Web Programming » Retrive data from session variables

Retrive data from session variables

Web Programming401
Dear All



i am writting a code sending mail with attachement.



i am writting code for sending mail in one page and code for attaching

a file in the next page.

aftet attaching a file i am taking name of that file from attaching

file page to email page through in session file .i am giving a

facility of attaching five files to user . and i am taking names of

both files in session variables but user attach less than five five

then values of another session becomes null null and when i am coming

on send email page it throws an error object is not set to referance of

an object. can any one tell me how i can manage the value of session if

it is null.



any help will be greatly apreciated.





Here is my code.



Code with SendEmail Page



if(!Page.IsPostBack)

{

try

{

if(Request.QueryString.Count>0 )//&&

Session["file1_Length"].ToString()==null)

{

mailTo_TextBox.Text=Session["toMailId"].ToString();

mailCC_TextBox.Text=Session["mailCC"].ToString();

mailBCC_TextBox.Text=Session["mailBCC"].ToString();

mailSubject_TextBox.Text=Session["mailSubject"].ToString();

mailBody_TextBox.Text=Session["mailBody"].ToString();



attachedFileName_Label.Visible=true;

fileSize_Label.Visible=true;

fileSize_Label.Text =

Request.QueryString["file1_Name"].ToString();//Session["file1_Length"].ToString()+"K";

removeAttachedFile_LinkButton.Visible=true;

attachedFileName_Label.Text=Request.QueryString["file1_Name"].ToString();

//Session["file1_Name"].ToString();



attachedFileName1_Label.Visible=true;

fileSize2_Label.Visible=true;

fileSize2_Label.Text = Session["file2_Length"].ToString()+"K";

removeAttachedFile1_LinkButton.Visible=true;

attachedFileName1_Label.Text=Session["file2_Name"].ToString();



attachedFileName2_Label.Visible=true;

fileSize3_Label.Visible=true;

fileSize3_Label.Text = Session["file3_Length"].ToString()+"K";

removeAttachedFile2_LinkButton.Visible=true;

attachedFileName2_Label.Text=Session["file3_Name"].ToString();



attachedFileName3_Label.Visible=true;

fileSize4_Label.Visible=true;

fileSize4_Label.Text = Session["file4_Length"].ToString()+"K";

removeAttachedFile3_LinkButton.Visible=true;

attachedFileName3_Label.Text=Session["file4_Name"].ToString();



attachedFileName4_Label.Visible=true;

fileSize_Label.Visible=true;

fileSize_Label.Text = Session["file5_Length"].ToString()+"K";

removeAttachedFile4_LinkButton.Visible=true;

attachedFileName4_Label.Text=Session["file5_Name"].ToString();



}

}

catch( Exception ex)

{

Response.Write("<script language=javascript>

alert('"+ex.Message.ToString() +"');</script>");

}

}

}



#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}



/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.sendMail1_Button.Click += new

System.EventHandler(this.sendMail1_Button_Click);

this.addCC_LinkButton.Click += new

System.EventHandler(this.addCC_LinkButton_Click);

this.addBCC_LinkButton.Click += new

System.EventHandler(this.addBCC_LinkButton_Click);

this.removeAttachedFile_LinkButton.Click += new

System.EventHandler(this.removeAttachedFile_LinkButton_Click);

this.removeAttachedFile1_LinkButton.Click += new

System.EventHandler(this.removeAttachedFile1_LinkButton_Click);

this.removeAttachedFile2_LinkButton.Click += new

System.EventHandler(this.removeAttachedFile2_LinkButton_Click);

this.removeAttachedFile3_LinkButton.Click += new

System.EventHandler(this.removeAttachedFile3_LinkButton_Click);

this.removeAttachedFile4_LinkButton.Click += new

System.EventHandler(this.removeAttachedFile4_LinkButton_Click);

this.attachFiles_Button.Click += new

System.EventHandler(this.attachFiles_Button_Click);

this.bold_ImageButton.Click += new

System.Web.UI.ImageClickEventHandler(this.bold_ImageButton_Click);

this.italic_ImageButton.Click += new

System.Web.UI.ImageClickEventHandler(this.italic_ImageButton_Click);

this.underline_ImageButton.Click += new

System.Web.UI.ImageClickEventHandler(this.underline_ImageButton_Click);

this.sendMail_Button.Click += new

System.EventHandler(this.sendMail_Button_Click);

this.Load += new System.EventHandler(this.Page_Load);



}

#endregion



private void LinkButton1_Click(object sender, System.EventArgs e)

{

Response.Redirect("ResourceDefination.aspx",false);

}



MailAttachment attach_1,attach_2,attach_3,attach_4,attach_5 ;

private void sendMail1_Button_Click(object sender, System.EventArgs

e)

{

try

{

MailMessage newMessage = new MailMessage();



newMessage.To = mailTo_TextBox.Text.Trim();

newMessage.From ="santoshmshinde_it@yahoo.com";

newMessage.Bcc = mailBCC_TextBox.Text.Trim();

newMessage.Cc = mailCC_TextBox.Text.Trim();

newMessage.Body = mailBody_TextBox.Text;

newMessage.Subject = mailSubject_TextBox.Text;

newMessage.BodyFormat = MailFormat.Html;



if(Session["file1_Name"].ToString()!=""

||Session["file1_Name"].ToString()!= null )

{

attach_1 = new

MailAttachment(Server.MapPath(Session["file1_Name"].ToString()));

/* Attach the newly created email attachment */

newMessage.Attachments.Add(attach_1);

}

if(Session["file2_Name"].ToString()!=""

||Session["file2_Name"].ToString()!= null )

{

attach_2 = new

MailAttachment(Server.MapPath(Session["file2_Name"].ToString()));

/* Attach the newly created email attachment */

newMessage.Attachments.Add(attach_2);

}

if(Session["file3_Name"].ToString()!=""

||Session["file3_Name"].ToString()!= null )

{

attach_3 = new

MailAttachment(Server.MapPath(Session["file3_Name"].ToString()));

/* Attach the newly created email attachment */

newMessage.Attachments.Add(attach_3);

}

if(Session["file4_Name"].ToString()!=""

||Session["file4_Name"].ToString()!= null )

{

attach_4 = new

MailAttachment(Server.MapPath(Session["file4_Name"].ToString()));

/* Attach the newly created email attachment */

newMessage.Attachments.Add(attach_4);

}

if(Session["file5_Name"].ToString()!=""

||Session["file5_Name"].ToString()!= null )

{

attach_5 = new

MailAttachment(Server.MapPath(Session["file5_Name"].ToString()));

/* Attach the newly created email attachment */

newMessage.Attachments.Add(attach_5);

}



SmtpMail.Send(newMessage);

}

catch(Exception ex)

{

Response.Write("<script language=javascript>alert('"+

ex.Message.ToString() +"');</script>");



}

}







Code for attaching a file to mail







private void attachFiles_Button_Click(object sender, System.EventArgs

e)

{

string

strFile1_Name,strFile2_Name,strFile3_Name,strFile4_Name,strFile5_Name;

int

attachFile1_Length,attachFile2_Length,attachFile3_Length,attachFile4_Length,attachFile5_Length;

string unit;

/* Beginning of Attachment1 process &

Check the first open file dialog for a attachment */

if (attachFile1.PostedFile != null)

{

/* Get a reference to PostedFile object */

HttpPostedFile attFile1 = attachFile1.PostedFile;

/* Get size of the file */

attachFile1_Length = attFile1.ContentLength;

/* Make sure the size of the file is>0 */

if (attachFile1_Length>0)

{

/* Get the file name */

strFile1_Name = Path.GetFileName(attachFile1.PostedFile.FileName);

attachFile1.PostedFile.SaveAs(Server.MapPath(strFile1_Name));

attachedFileName_Label.Text = strFile1_Name.ToString();

fileSize1_Label.Text = Convert.ToInt32(attachFile1_Length)+ " KB "

+ attFile1.ContentType.ToString();

Request.QueryString["file1_Name"]=strFile1_Name;

//Session["file1_Name"]=strFile1_Name;

Request.QueryString["file1_Length"]=Convert.ToInt32(attachFile1_Length).ToString();

SetControls();

}

}



if (attachFile2.PostedFile != null)

{

/* Get a reference to PostedFile object */

HttpPostedFile attFile2 = attachFile2.PostedFile;

/* Get size of the file */

attachFile2_Length = attFile2.ContentLength;

/* Make sure the size of the file is>0 */

if (attachFile2_Length>0)

{

/* Get the file name */

strFile2_Name = Path.GetFileName(attachFile2.PostedFile.FileName);

attachFile2.PostedFile.SaveAs(Server.MapPath(strFile2_Name));

attachedFileName1_Label.Text = strFile2_Name.ToString();

fileSize2_Label.Text = Convert.ToInt32(attachFile2_Length) + " KB

" + attFile2.ContentType.ToString();

Session["file2_Name"]=strFile2_Name;

Session["file2_Length"]=Convert.ToInt32(attachFile2_Length);

SetControls();

}

}

if (attachFile3.PostedFile != null)

{

/* Get a reference to PostedFile object */

HttpPostedFile attFile3 = attachFile3.PostedFile;

/* Get size of the file */

attachFile3_Length = attFile3.ContentLength;

/* Make sure the size of the file is>0 */

if (attachFile3_Length>0)

{

/* Get the file name */

strFile3_Name = Path.GetFileName(attachFile3.PostedFile.FileName);

attachFile3.PostedFile.SaveAs(Server.MapPath(strFile3_Name));

attachedFileName2_Label.Text = strFile3_Name.ToString();

fileSize3_Label.Text = Convert.ToInt32(attachFile3_Length) + " KB

" + attFile3.ContentType.ToString();

Session["file3_Name"]=strFile3_Name;

Session["file3_Length"]=Convert.ToInt32(attachFile3_Length);

SetControls();

}

}

if (attachFile4.PostedFile != null)

{

/* Get a reference to PostedFile object */

HttpPostedFile attFile4 = attachFile4.PostedFile;

/* Get size of the file */

attachFile4_Length = attFile4.ContentLength;

/* Make sure the size of the file is>0 */

if (attachFile4_Length>0)

{

/* Get the file name */

strFile4_Name = Path.GetFileName(attachFile4.PostedFile.FileName);

attachFile4.PostedFile.SaveAs(Server.MapPath(strFile4_Name));

attachedFileName3_Label.Text = strFile4_Name.ToString();

fileSize4_Label.Text = Convert.ToInt32(attachFile4_Length) + " KB

" + attFile4.ContentType.ToString();

Session["file4_Name"]=strFile4_Name;

Session["file4_Length"]=Convert.ToInt32(attachFile4_Length);

SetControls();

}

}

if (attachFile5.PostedFile != null)

{

/* Get a reference to PostedFile object */

HttpPostedFile attFile5 = attachFile5.PostedFile;

/* Get size of the file */

attachFile5_Length = attFile5.ContentLength;

unit = attFile5.ContentType.ToString();

/* Make sure the size of the file is>0 */

if (attachFile5_Length>0)

{

/* Get the file name */

strFile5_Name = Path.GetFileName(attachFile5.PostedFile.FileName);

attachFile5.PostedFile.SaveAs(Server.MapPath(strFile5_Name));

attachedFileName4_Label.Text = strFile5_Name.ToString();

fileSize5_Label.Text = Convert.ToInt32(attachFile5_Length)+ " KB "

+ attFile5.ContentType.ToString();

Session["file5_Name"]=strFile5_Name;

Session["file5_Length"]=Convert.ToInt32(attachFile5_Length);

SetControls();

}

}

else

{

return;

}

}







private void continueToMessage_Button_Click(object sender,

System.EventArgs e)

{

string msgOne = "attachFiles";

//string send = "?A" + "?B" + "?C" +"?D" + "?E" + msgOne + " + "+

Session["file1_Name"].ToString()+" ";



string send = "?A" + "+ msgOne +";

Response.Redirect("EMail.aspx" + send);

}


-
 

Re:Retrive data from session variables

The test

Session["file1_Length"].ToString()==null

is wrong. A "ToString()" will always return a string (possibly an empty

one), never a null. More important, if Session["file1_length"] does not

exist (is null), then this will give a NullReferenceException.



You need to test

Session["file1_length"] == null



This test will also solve your other problems: if some file is not

defined, don't try to do something with it.



Hans Kesting



Quote
Dear All



i am writting a code sending mail with attachement.



i am writting code for sending mail in one page and code for attaching

a file in the next page.

aftet attaching a file i am taking name of that file from attaching

file page to email page through in session file .i am giving a

facility of attaching five files to user . and i am taking names of

both files in session variables but user attach less than five five

then values of another session becomes null null and when i am coming

on send email page it throws an error object is not set to referance of

an object. can any one tell me how i can manage the value of session if

it is null.



any help will be greatly apreciated.



Here is my code.



Code with SendEmail Page



if(!Page.IsPostBack)

{

try

{

if(Request.QueryString.Count>0 )//&&

Session["file1_Length"].ToString()==null)

{

mailTo_TextBox.Text=Session["toMailId"].ToString();

mailCC_TextBox.Text=Session["mailCC"].ToString();

mailBCC_TextBox.Text=Session["mailBCC"].ToString();

mailSubject_TextBox.Text=Session["mailSubject"].ToString();

mailBody_TextBox.Text=Session["mailBody"].ToString();



attachedFileName_Label.Visible=true;

fileSize_Label.Visible=true;

fileSize_Label.Text =

Request.QueryString["file1_Name"].ToString();//Session["file1_Length"].ToString()+"K";

removeAttachedFile_LinkButton.Visible=true;

attachedFileName_Label.Text=Request.QueryString["file1_Name"].ToString();

//Session["file1_Name"].ToString();



attachedFileName1_Label.Visible=true;

fileSize2_Label.Visible=true;

fileSize2_Label.Text = Session["file2_Length"].ToString()+"K";

removeAttachedFile1_LinkButton.Visible=true;

attachedFileName1_Label.Text=Session["file2_Name"].ToString();



attachedFileName2_Label.Visible=true;

fileSize3_Label.Visible=true;

fileSize3_Label.Text = Session["file3_Length"].ToString()+"K";

removeAttachedFile2_LinkButton.Visible=true;

attachedFileName2_Label.Text=Session["file3_Name"].ToString();



attachedFileName3_Label.Visible=true;

fileSize4_Label.Visible=true;

fileSize4_Label.Text = Session["file4_Length"].ToString()+"K";

removeAttachedFile3_LinkButton.Visible=true;

attachedFileName3_Label.Text=Session["file4_Name"].ToString();



attachedFileName4_Label.Visible=true;

fileSize_Label.Visible=true;

fileSize_Label.Text = Session["file5_Length"].ToString()+"K";

removeAttachedFile4_LinkButton.Visible=true;

attachedFileName4_Label.Text=Session["file5_Name"].ToString();



}

}

catch( Exception ex)

{

Response.Write("<script language=javascript>

alert('"+ex.Message.ToString() +"');</script>");

}

}

}



#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}



/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.sendMail1_Button.Click += new

System.EventHandler(this.sendMail1_Button_Click);

this.addCC_LinkButton.Click += new

System.EventHandler(this.addCC_LinkButton_Click);

this.addBCC_LinkButton.Click += new

System.EventHandler(this.addBCC_LinkButton_Click);

this.removeAttachedFile_LinkButton.Click += new

System.EventHandler(this.removeAttachedFile_LinkButton_Click);

this.removeAttachedFile1_LinkButton.Click += new

System.EventHandler(this.removeAttachedFile1_LinkButton_Click);

this.removeAttachedFile2_LinkButton.Click += new

System.EventHandler(this.removeAttachedFile2_LinkButton_Click);

this.removeAttachedFile3_LinkButton.Click += new

System.EventHandler(this.removeAttachedFile3_LinkButton_Click);

this.removeAttachedFile4_LinkButton.Click += new

System.EventHandler(this.removeAttachedFile4_LinkButton_Click);

this.attachFiles_Button.Click += new

System.EventHandler(this.attachFiles_Button_Click);

this.bold_ImageButton.Click += new

System.Web.UI.ImageClickEventHandler(this.bold_ImageButton_Click);

this.italic_ImageButton.Click += new

System.Web.UI.ImageClickEventHandler(this.italic_ImageButton_Click);

this.underline_ImageButton.Click += new

System.Web.UI.ImageClickEventHandler(this.underline_ImageButton_Click);

this.sendMail_Button.Click += new

System.EventHandler(this.sendMail_Button_Click);

this.Load += new System.EventHandler(this.Page_Load);



}

#endregion



private void LinkButton1_Click(object sender, System.EventArgs e)

{

Response.Redirect("ResourceDefination.aspx",false);

}



MailAttachment attach_1,attach_2,attach_3,attach_4,attach_5 ;

private void sendMail1_Button_Click(object sender, System.EventArgs

e)

{

try

{

MailMessage newMessage = new MailMessage();



newMessage.To = mailTo_TextBox.Text.Trim();

newMessage.From ="santoshmshinde_it@yahoo.com";

newMessage.Bcc = mailBCC_TextBox.Text.Trim();

newMessage.Cc = mailCC_TextBox.Text.Trim();

newMessage.Body = mailBody_TextBox.Text;

newMessage.Subject = mailSubject_TextBox.Text;

newMessage.BodyFormat = MailFormat.Html;



if(Session["file1_Name"].ToString()!=""

>>Session["file1_Name"].ToString()!= null )

{

attach_1 = new

MailAttachment(Server.MapPath(Session["file1_Name"].ToString()));

/* Attach the newly created email attachment */

newMessage.Attachments.Add(attach_1);

}

if(Session["file2_Name"].ToString()!=""

>>Session["file2_Name"].ToString()!= null )

{

attach_2 = new

MailAttachment(Server.MapPath(Session["file2_Name"].ToString()));

/* Attach the newly created email attachment */

newMessage.Attachments.Add(attach_2);

}

if(Session["file3_Name"].ToString()!=""

>>Session["file3_Name"].ToString()!= null )

{

attach_3 = new

MailAttachment(Server.MapPath(Session["file3_Name"].ToString()));

/* Attach the newly created email attachment */

newMessage.Attachments.Add(attach_3);

}

if(Session["file4_Name"].ToString()!=""

>>Session["file4_Name"].ToString()!= null )

{

attach_4 = new

MailAttachment(Server.MapPath(Session["file4_Name"].ToString()));

/* Attach the newly created email attachment */

newMessage.Attachments.Add(attach_4);

}

if(Session["file5_Name"].ToString()!=""

>>Session["file5_Name"].ToString()!= null )

{

attach_5 = new

MailAttachment(Server.MapPath(Session["file5_Name"].ToString()));

/* Attach the newly created email attachment */

newMessage.Attachments.Add(attach_5);

}



SmtpMail.Send(newMessage);

}

catch(Exception ex)

{

Response.Write("<script language=javascript>alert('"+

ex.Message.ToString() +"');</script>");



}

}



Code for attaching a file to mail



private void attachFiles_Button_Click(object sender, System.EventArgs

e)

{

string

strFile1_Name,strFile2_Name,strFile3_Name,strFile4_Name,strFile5_Name;

int

attachFile1_Length,attachFile2_Length,attachFile3_Length,attachFile4_Length,attachFile5_Length;

string unit;

/* Beginning of Attachment1 process &

Check the first open file dialog for a attachment */

if (attachFile1.PostedFile != null)

{

/* Get a reference to PostedFile object */

HttpPostedFile attFile1 = attachFile1.PostedFile;

/* Get size of the file */

attachFile1_Length = attFile1.ContentLength;

/* Make sure the size of the file is>0 */

if (attachFile1_Length>0)

{

/* Get the file name */

strFile1_Name = Path.GetFileName(attachFile1.PostedFile.FileName);

attachFile1.PostedFile.SaveAs(Server.MapPath(strFile1_Name));

attachedFileName_Label.Text = strFile1_Name.ToString();

fileSize1_Label.Text = Convert.ToInt32(attachFile1_Length)+ " KB "

+ attFile1.ContentType.ToString();

Request.QueryString["file1_Name"]=strFile1_Name;

//Session["file1_Name"]=strFile1_Name;

Request.QueryString["file1_Length"]=Convert.ToInt32(attachFile1_Length).ToString();

SetControls();

}

}



if (attachFile2.PostedFile != null)

{

/* Get a reference to PostedFile object */

HttpPostedFile attFile2 = attachFile2.PostedFile;

/* Get size of the file */

attachFile2_Length = attFile2.ContentLength;

/* Make sure the size of the file is>0 */

if (attachFile2_Length>0)

{

/* Get the file name */

strFile2_Name = Path.GetFileName(attachFile2.PostedFile.FileName);

attachFile2.PostedFile.SaveAs(Server.MapPath(strFile2_Name));

attachedFileName1_Label.Text = strFile2_Name.ToString();

fileSize2_Label.Text = Convert.ToInt32(attachFile2_Length) + " KB

" + attFile2.ContentType.ToString();

Session["file2_Name"]=strFile2_Name;

Session["file2_Length"]=Convert.ToInt32(attachFile2_Length);

SetControls();

}

}

if (attachFile3.PostedFile != null)

{

/* Get a reference to PostedFile object */

HttpPostedFile attFile3 = attachFile3.PostedFile;

/* Get size of the file */

attachFile3_Length = attFile3.ContentLength;

/* Make sure the size of the file is>0 */

if (attachFile3_Length>0)

{

/* Get the file name */

strFile3_Name = Path.GetFileName(attachFile3.PostedFile.FileName);

attachFile3.PostedFile.SaveAs(Server.MapPath(strFile3_Name));

attachedFileName2_Label.Text = strFile3_Name.ToString();

fileSize3_Label.Text = Convert.ToInt32(attachFile3_Length) + " KB

" + attFile3.ContentType.ToString();

Session["file3_Name"]=strFile3_Name;

Session["file3_Length"]=Convert.ToInt32(attachFile3_Length);

SetControls();

}

}

if (attachFile4.PostedFile != null)

{

/* Get a reference to PostedFile object */

HttpPostedFile attFile4 = attachFile4.PostedFile;

/* Get size of the file */

attachFile4_Length = attFile4.ContentLength;

/* Make sure the size of the file is>0 */

if (attachFile4_Length>0)

{

/* Get the file name */

strFile4_Name = Path.GetFileName(attachFile4.PostedFile.FileName);

attachFile4.PostedFile.SaveAs(Server.MapPath(strFile4_Name));

attachedFileName3_Label.Text = strFile4_Name.ToString();

fileSize4_Label.Text = Convert.ToInt32(attachFile4_Length) + " KB

" + attFile4.ContentType.ToString();

Session["file4_Name"]=strFile4_Name;

Session["file4_Length"]=Convert.ToInt32(attachFile4_Length);

SetControls();

}

}

if (attachFile5.PostedFile != null)

{

/* Get a reference to PostedFile object */

HttpPostedFile attFile5 = attachFile5.PostedFile;

/* Get size of the file */

attachFile5_Length = attFile5.ContentLength;

unit = attFile5.ContentType.ToString();

/* Make sure the size of the file is>0 */

if (attachFile5_Length>0)

{

/* Get the file name */

strFile5_Name = Path.GetFileName(attachFile5.PostedFile.FileName);

attachFile5.PostedFile.SaveAs(Server.MapPath(strFile5_Name));

attachedFileName4_Label.Text = strFile5_Name.ToString();

fileSize5_Label.Text = Convert.ToInt32(attachFile5_Length)+ " KB "

+ attFile5.ContentType.ToString();

Session["file5_Name"]=strFile5_Name;

Session["file5_Length"]=Convert.ToInt32(attachFile5_Length);

SetControls();

}

}

else

{

return;

}

}



private void continueToMessage_Button_Click(object sender,

System.EventArgs e)

{

string msgOne = "attachFiles";

//string send = "?A" + "?B" + "?C" +"?D" + "?E" + msgOne + " + "+

Session["file1_Name"].ToString()+" ";



string send = "?A" + "+ msgOne +";

Response.Redirect("EMail.aspx" + send);

}





-