I am a new C# programmer and I am trying to make a program to send an email containing the contents of a MSSQL table. The main program looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace NotifySFUpdate
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
When I debug it shows that the line Application.Run(new Form1()); is throwing an ObjectDisposedException.
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.IO;
using System.Text;
namespace NotifySFUpdate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
using (sfDataContext db = new sfDataContext())
{
db.queryName();
var x = from s in db.zz
select s;
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
smtp.Host = "hostaddress";
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.NetworkCredential smtpuser = new System.Net.NetworkCredential("website", "password");
smtp.UseDefaultCredentials = false;
smtp.Credentials = smtpuser;
string outputSubject = "Pages to publish";
string template;
using (StreamReader sr = new StreamReader(@"C:\locationofTemplate"))
{
template = sr.ReadToEnd();
}
StringBuilder sbNew = new StringBuilder();
foreach (zz e in x)
{
sbNew.Append(e.URL);
sbNew.Append("<br/>");
}
template = template.Replace("{pages}", sbNew.ToString());
message.Subject = outputSubject;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Body = template;
message.IsBodyHtml = true;
message.From = new MailAddress("website@myserver", "nameofInstitution");
message.To.Add("*myemailAddress*");
smtp.Send(message);
db.zz.DeleteAllOnSubmit(x);
db.SubmitChanges();
}
try
{
this.Close();
}
catch
{
}
Application.Exit();
}
}
}
When the guy that run the webserver deploys it it runs correctly the first time after being compiled, but all times after that the first time it sends a cached email of what the table contents were the first time it ran.
Any help would be greatly appreciated. If I have not supplied enough information please let me know.