I have working code which I have listed below here it does exactly what I want. However, I would like to minimize it down so I only call the DB once instead of twice. I currently have the query written to only bring back 1 record that has 1 column.
This column brings back a date which I use in my code. The issues is I also need to bring back the user as well. Which I made work by basically using the same query but have it bring back the user rather than the date.
THE BIG QUESTION: I have the query to use to bring back both pieces of information, however, I am not sure how to get the information back to use in my code? when there is more that one column being returned from query. side note the new query still only brings back one record but with 2 columns.
THE WORKING CODE: I would add in "UpdatedBy" to last select statement to add the 2nd column.
SqlConnection conn = new SqlConnection(@"Data Source=(localdb)\......");
SqlCommand cmd = conn.CreateCommand();
string updatedOn;
conn.Open();
cmd.CommandText = ("USE CORNERSTONE DECLARE @SID int SET @SID = " + SupplierId + "; " +
"with MaxDate as " +
"(select UpdatedOn, UpdatedBy, SupplierId from CORNERSTONE.Suppliers.Supplier where SupplierId = @SID " +
"UNION " +
"select UpdatedOn, UpdatedBy, SupplierId from Suppliers.CompanyLocation where SupplierId = @SID " +
"UNION " +
"select UpdatedOn, UpdatedBy, SupplierId from Suppliers.AssignedContact where SupplierId = @SID) " +
"select UpdatedOn " +
"from MaxDate " +
"where UpdatedOn = (Select MAX(UpdatedOn) from MaxDate)");
updatedOn = cmd.ExecuteScalar().ToString();
DateTime ParsedDateTime = DateTime.Parse(updatedOn);
AuditUpdatedOn = ParsedDateTime;
conn.Close();