I know what nullreference means but having a problem pinpointing exactly what is causing the problem. I have a .NET C# Console Application that works perfectly in my development environment when run, but when deployed to a server, I get the NullReference object not set to an instance of an object... error message. I am unable to see the exact line because it is in the compiled executable console. I have tried to no avail to see where the error is occurring. My database connections are good, my SELECT queries return values when I test them directly in SQL Server Management Studio so I am at a pause for what is producing this error and it actually works without error when run in VS on my deve machine. Here is the code:
namespace BWC2Mailer
{
class Program
{
static void Main(string[] args)
{
String constr = ConfigurationManager.ConnectionStrings["binddropdown3"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd1 = new SqlCommand("SELECT distinct TestEmail, TestBWC FROM TestTable2 WHERE CONVERT(CHAR(10), GETDATE(), 101) = DATEADD(day,-2, TestBWC) AND TestBWC <> '' AND TestEmail <> ''", con); // Test
SqlDataReader rd1 = cmd1.ExecuteReader();
if (rd1.HasRows)
{
SqlConnection con3 = new SqlConnection(constr);
while (rd1.Read())
{
var EmailToSendPre = rd1["TestEmail"].ToString(); // Test Email
string EmailToSend = EmailToSendPre.Replace("`", "");
string ExpirationDate = rd1["TestBWC"].ToString();
if (emailIsValid(EmailToSend))
{
var TodayIs = DateTime.Today.ToString("MM/dd/yyyy");
con3.Open();
var QType = "BWC2";
var BWCSubject = "5th Notice-2 days prior";
string BWCBody = "<p>...</p>";
SqlCommand cmd3 = new SqlCommand("SELECT * From EmailNotificationStatus WHERE Email = '" + EmailToSend + "' AND SendDate = '" + TodayIs + "' AND QueryType ='" + QType + "'", con3);
SqlDataReader rd3 = cmd3.ExecuteReader();
if (!rd3.Read())
{
MailMessage mailObj = new MailMessage(
"noreply@xxxx.com", EmailToSend, BWCSubject, BWCBody);
SmtpClient SMTPServer = new SmtpClient("xxxx.xxxxx.com"); //
MailAddress cc = new MailAddress("xxxx@xxxx.com");
mailObj.CC.Add(cc);
mailObj.IsBodyHtml = true;
try
{
SMTPServer.Send(mailObj);
}
catch (Exception ex)
{
}
var TodayNow = DateTime.Today.ToString("MM/dd/yyyy");
SqlConnection con2 = new SqlConnection(constr);
con2.Open();
SqlCommand cmd2 = new SqlCommand("Insert INTO EmailNotificationStatus(Email,Sent,QueryType,SendDate) values(@email,@Sent,@QueryType,@SendDate)", con2);
cmd2.Parameters.Add("@email", SqlDbType.VarChar).Value = EmailToSend;
cmd2.Parameters.Add("@Sent", SqlDbType.VarChar).Value = "YES";
cmd2.Parameters.Add("@QueryType", SqlDbType.VarChar).Value = "BWC2";
cmd2.Parameters.Add("@SendDate", SqlDbType.VarChar).Value = TodayNow;
cmd2.ExecuteNonQuery();
con2.Close();
}
}
con3.Close();
}
}
}
public static bool emailIsValid(string email)
{
string expresion;
expresion = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
if (Regex.IsMatch(email, expresion))
{
if (Regex.Replace(email, expresion, string.Empty).Length == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}