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.Te xt;
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\\D esktop\\Up loadImages \\";
string strfilename = Path.GetFileName(txtFile.P ostedFile. FileName);
txtFile.PostedFile.SaveAs( strdir+str filename);
mail.Attachments.Add(new MailAttachment(strdir+strf ilename));
try
{
SmtpMail.Send(mail);
}
catch(Exception ex)
{
Response.Write("<b>Excepti on Occured:</b> " +ex);
}
finally
{
Response.Write("Your E-mail has been sent sucessfully");
}
// Uploaded file deleted after sending e-mail
File.Delete(strdir+strfile name);
}
Regards
Caz
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.Te
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\\D
string strfilename = Path.GetFileName(txtFile.P
txtFile.PostedFile.SaveAs(
mail.Attachments.Add(new MailAttachment(strdir+strf
try
{
SmtpMail.Send(mail);
}
catch(Exception ex)
{
Response.Write("<b>Excepti
}
finally
{
Response.Write("Your E-mail has been sent sucessfully");
}
// Uploaded file deleted after sending e-mail
File.Delete(strdir+strfile
}
Regards
Caz
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
ASKER
Hi ANamika
On submit click
Caz
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
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
ASKER
Hi ANamika
It appears to be this line
string strfilename = Path.GetFileName(txtFile.P ostedFile. FileName);
Caz
It appears to be this line
string strfilename = Path.GetFileName(txtFile.P
Caz
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Anamika
Sorted
Caz
Sorted
Caz