I am working on a console based app that will email the user of the app an email with a description of a problem with clients trying to use a mobile app. What I am currently attempting right now is setting up the emailing piece of the code. The only problem I am having is when I try to run it, it keeps telling me that I have a null reference exception. Below is the code I am trying to work with. What it is supposed to do is loop through all of the transactions in this list, if it was fixed then nothing will really happen. Otherwise it will take the transaction, match it to user information they have on a website and then puts in all in a string so we can put it in a email.
The problem is on the line with
myHomeBody = matchRecords.ToString();
When I went through it in debugging, I got the value I needed in here DashSupportMonitorBO.TransactionInformation.UserName.get
returned, but in matchRecords
where I want that value, it is coming up as null.
So is there a way I can just get that value and move it into match records, or is there just a simple way of fixing this that I am just completely missing?
foreach (TransactionInformation transaction in recentFailedTransactions)
{
string displayErrorBody;
string myHomeBody;
if (TransactionSuccessFound(transaction))
{
BOAssistant.WriteLine("Transaction fixed");
}
else
{
displayErrorBody = transaction.DisplayException.ToString();
MyHomeInformation matchRecords = GetUserDataFromMyHome(transaction.UserName);
myHomeBody = matchRecords.ToString();
string emailBody = "Here is the displayErrorBody: \n" + displayErrorBody + "Here is all the user information regarding the error: \n" + myHomeBody;
BOAssistant.WriteLine(emailBody);
SendEmail(emailBody, "Errors in Dash Mobile");
}
}
Here is the code for GetUserDataFromMyHome
private static MyHomeInformation GetUserDataFromMyHome(string username)
{
MyHomeInformation myHomeInformation = null;
using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.MyHomeConnectionString))
{
SqlCommand sqlError = connection.CreateCommand();
sqlError.CommandText = @"SELECT USER_NAME, EMAIL, FIRST_NAME, LAST_NAME, TRAVELER_UID FROM [USER] WHERE USER_NAME = @USER_NAME";
sqlError.Parameters.Add("@USER_NAME", System.Data.SqlDbType.VarChar).Value = username;
connection.Open();
SqlDataReader reader = sqlError.ExecuteReader();
if(reader.Read())
{
myHomeInformation = new MyHomeInformation();
myHomeInformation.myHomeUserName = Utilities.FromDBValue<string>(reader["USER_NAME"]);
myHomeInformation.myHomeEmail = Utilities.FromDBValue<string>(reader["EMAIL"]);
myHomeInformation.myHomeFirstName = Utilities.FromDBValue<string>(reader["FIRST_NAME"]);
myHomeInformation.myHomeLastName = Utilities.FromDBValue<string>(reader["LAST_NAME"]);
myHomeInformation.myHomeTravelerUID = (Utilities.FromDBValue<Guid>(reader["TRAVELER_UID"])).ToString();
}
}
return myHomeInformation;
}