I am building an Administration web site for the Office details in my database. Right now, I am just trying to display the office details.
public class OfficeRepository
{
public static Office GetOffice(int syncID)
{
List<Office> results = new List<Office>();
var sql = @"Select so.SyncID, so.title From Offices o Left Outer Join SyncOffices so On so.id = o.SyncID Where o.SyncID = @syncID";
using (var connection = new SqlConnection(Settings.ConnectionString))
using (var command = new SqlCommand(sql, connection))
{
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@syncID", syncID);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var office = new Office();
office.SyncID = reader.GetInt32(0);
office.OfficeName = reader.GetString(1);
results.Add(office);
}
}
}
Office returnOffice = new Office();
returnOffice.SyncID = results.FirstOrDefault().SyncID;
returnOffice.OfficeName = results.FirstOrDefault().OfficeName;
return returnOffice;
}
}
public class HomeController : Controller
{
public ActionResult Index(int id = 0)
{
Office currentOffice = OfficeRepository.GetOffice(id);
var officeMgrViewModel = new OfficeMgrViewModel();
officeMgrViewModel.Office = currentOffice;
return View(officeMgrViewModel);
}
}
public class OfficeMgrViewModel
{
public Office Office { get; set; }
}
public class Office
{
public Int32 SyncID { get; set; }
public string OfficeName { get; set; }
}