Link to home
Start Free TrialLog in
Avatar of BeginningWebDesign
BeginningWebDesign

asked on

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object

Hi
Does anyone know why I get the error in the title, my code is below.

                               string strLocation, strCountry, strYear, strMessage, strTaken, strIDNumber;
            private void Page_Load(object sender, System.EventArgs e)
            {
                  strLocation = tbLocation.Text;
                  strCountry= ddlCountry.SelectedItem.Text;
                  strYear =  ddlYear.SelectedItem.Text;
                  strMessage =   tbMsg.Text;
                  strTaken = tbVisitorName.Text;
                  strIDNumber = Session.SessionID;
                  // Put user code to initialize the page here
            }

private void btnSubmit_Click(object sender, System.EventArgs e)
            {
                  MailMessage mail = new MailMessage();
                  mail.From = "picture@**.com";
                  mail.To = "picture@**.com";
                  mail.Subject = "Image";
                  mail.Body = "Country: strCountry"  + "\n" + "Location: strLocation" + "\n" + "Year: strYear" + "\n" + "Message: strMessage" + "\n" + "Taken by: strTaken";
                  mail.BodyFormat = MailFormat.Html;

                  string strdir = "C:\\Documents and Settings\\Administrator\\Desktop\\UploadImages\\";

                  string strfilename = Path.GetFileName(txtFile.PostedFile.FileName);

                  txtFile.PostedFile.SaveAs(strdir+strfilename);

                  mail.Attachments.Add(new MailAttachment(strdir+strfilename));

                  try
                  {
                        SmtpMail.Send(mail);
                  }
                  catch(Exception ex)
                  {
                        Response.Write("<b>Exception Occured:</b>   " +ex);
                  }
                  finally
                  {
                        Response.Write("Your E-mail has been sent sucessfully");
                  }

                  // Uploaded file deleted after sending e-mail

                  File.Delete(strdir+strfilename);
            }

Regards
Caz
Avatar of ANamika
ANamika

when are you getting teh error: on page load on submit click
In page load it might be because there may not be any value selected for ddlCountry etc.(assuming it is dropdown) and you are trying to access selecteditem.text
Avatar of BeginningWebDesign

ASKER

Hi ANamika

On submit click

Caz
by any chance have you debugged and have any idea on which lien teh error is coming.

The error message is generic and can come on any line of your code. We can pinpoint teh defect, if we know what line is triggereing the error
Hi ANamika

It appears to be this line
string strfilename = Path.GetFileName(txtFile.PostedFile.FileName);

Caz
ASKER CERTIFIED SOLUTION
Avatar of ANamika
ANamika

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Anamika

Sorted

Caz