Hello,
I am just started learning Asp.net with C#. I had created a contact form following instruction from the book and I did successfully locally however now when I tested live using one of the free web hosting, it doesn't work. I have no idea why as I totally new to this. I have been spending day and night on this error and can't move on any further so if you could help me with this I would be the happiest person in the whole world.
Thank you very much in anticipation.
Following is the error I got and the code in ContactForm.ascx.cs
Error
Could not find file 'C:\sites\content\App_Data\ContactForm.txt'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\sites\content\App_Data\ContactForm.txt'.
Source Error:
Line 38: {
Line 39: string fileName= Server.MapPath("~/App_Data/ContactForm.txt");
Line 40: string mailBody = System.IO.File.ReadAllText(fileName);
Line 41:
Line 42: mailBody = mailBody.Replace("##Name##", txtName.Text);
ContactForm.ascx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
public partial class Controls_ContactForm : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (txtPhoneHome.Text != string.Empty || txtMobile.Text != string.Empty)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
protected void btnSend_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string fileName= Server.MapPath("~/App_Data/ContactForm.txt");
string mailBody = System.IO.File.ReadAllText(fileName);
mailBody = mailBody.Replace("##Name##", txtName.Text);
mailBody = mailBody.Replace("##Email##", txtEmailAddress.Text);
mailBody = mailBody.Replace("##HomePhone##", txtPhoneHome.Text);
mailBody = mailBody.Replace("##BusinessPhone##", txtMobile.Text);
mailBody = mailBody.Replace("##Comments##", txtComments.Text);
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Response from web site";
myMessage.Body = mailBody;
myMessage.From = new MailAddress("
[email protected]", "Sender Name Here");
myMessage.To.Add(new MailAddress("
[email protected]", "Receiver Name Here"));
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessage);
lblMessage.Visible = true;
FormTable.Visible = false;
PlaceHolder1.Visible = false;
Heading.Visible = false;
}
}
}