I am working on an application where one can manage patient data, including their date of birth. I can insert this data just fine into SQL using dapper, but pulling the date of birth out is its own challenge.
I have a SQL query that returns a list of relevant patients based on the search and in c# I cast these into the Patient class I have defined in the application. However, when I do this, I can't specify how the string containing the date of birth gets casted into a date time.
This means that instead of it using my custom format to cast to, it uses whatever default format it is made to do. This is causing the date to simply return 1/1/0001 12:00 AM. Here is my code for pulling out of the database:
public List<PatientModel> SearchPatientsByName(string name)
{
List<PatientModel> output;
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
{
var p = new DynamicParameters();
p.Add("@Name", name);
output = connection.Query<PatientModel>("spPatients_SearchByName", p, commandType: CommandType.StoredProcedure).AsList();
}
return output;
}
Here is my code for inserting into Sql:
public PatientModel CreatePatientModel(PatientModel model)
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
{
model.Address = AesCryp.Encrypt(model.Address);
model.CIN = AesCryp.Encrypt(model.CIN);
var p = new DynamicParameters();
p.Add("@Name", model.Name);
p.Add("@DOB", model.DateOfBirth);
p.Add("@Address", model.Address);
p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@LastName", model.LastName);
p.Add("@Gender", model.Gender);
p.Add("@City", model.City);
p.Add("@PostalCode", model.PostalCode);
p.Add("@Agency", model.AgencyId);
p.Add("@CIN", model.CIN);
p.Add("@CardCode", 0);
p.Add("@MobileNumber", model.MobileNumber);
p.Add("@HomeNumber", model.HomeNumber);
p.Add("@InsuranceCompany", model.InsuranceCoId);
connection.Execute("spPatient_Insert", p, commandType: CommandType.StoredProcedure);
model.Id = p.Get<int>("@id");
return model;
}
}