I have 3 tables in a database that I am iterating through using a SQL statement. It searches for applications that need to have contracts renewed. I use SQL date math to check whether managers need to be notified of a contract renewal. If today's date = the date in the notificationDate field, the console app should send an email to the person listed as the analyst/manager of that application. Here is my code so far:
namespace ContractApp
{
class Program
{
//initializes strings for storing information from the table
static string aretheyManager;
static string listedanalystEmail;
static string listedmanagerEmail;
static void Main(string[] args)
{
int warningWindow = 10;
try
{
//connects to the AppInfo_dev table in the database
SqlConnection conn = new SqlConnection("server=10.52.2.169\\sqlcluster,1206;uid=TannerAppsWriter;pwd=TannerAppsWriter;database=AppInfo_dev;");
conn.Open();
//sets up a sequal command called selectedValues
SqlCommand selectValues = conn.CreateCommand();
//Pulls information from three tables in the database (AppInfo_dev, SoftwareApp, IT_Personnel)
//Takes the AppID from the Contracts list and compares it to AppID in the Apps list and displays matches
//Then it finds employee information related to the Primary Analyst that is listed for that application
//Adds a field called "notificationDate" that is filled by subtracting the "warningWindow" and "TerminationWindow" from the RenewalDate
//Finds contracts listed that have a "notificationDate" that is the same as the current date
//Takes the eMail fields and appends "@tanner.org" to the end of the text in the field
selectValues.CommandText = "My SQL statement goes here...it works so I didn't bother posting it since it is really long"
//Reads values in specified columns in the database
using (SqlDataReader dataReader = selectValues.ExecuteReader())
{
if (dataReader.HasRows)
{
while (dataReader.Read())
{
//Converts the values in the tables to strings
aretheyManager = Convert.ToString(dataReader["isManager"]);
listedanalystEmail = Convert.ToString(dataReader["analystEmail"]);
listedmanagerEmail = Convert.ToString(dataReader["managerEmail"]);
}
}
}
}
//If there is an error, catch it
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
}
private void sendEmailNotification()
{
//Create an email to send notifying of contract termination
MailMessage message = new MailMessage();
//Check to see if the listed analyst is a manager
//If they are, send the email to them
//If they are not, send they email to their manager.
if (aretheyManager == "True")
{
message.To.Add(listedanalystEmail);
}
else
{
message.To.Add(listedmanagerEmail);
}
message.Subject = "This contract requires your attention!";
message.From = new MailAddress("no response email address goes here");
message.Body = "There is an application contract that is in need of renewal.";
message.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient("client info goes here");
client.Send(message);
}
}
}
The SQL statements works as expected. It iterates through the rows in the table and pulls contracts with a notificationDate = the current date. I am having trouble with the datareader. It iterates through the contracts pulled by the SQL statement, but only stores the last value it reads into the strings. I need it to store any and all values it pulls so an email gets sent to each person if there are multiple people that need to be notified.
using {}block takes care of that. You also don't need theif...HasRowsblock. If there aren't any rows, thewhile {}block will just jump to the end of the block.