I encountered a problem that I can’t manage to save Excel start time and end time into the data base. For Excel, the time is as type, “String” but for the data base, it’s in type, “Time”. Is there any way that I could save the Excel Time into data base.
Here is my codes that save the data into data base but it only save the Name and Date but not the timings. Any help will be appreciated.
private void btnSave_Click(object sender, EventArgs e)
{
try
{
foreach (DataRow dr in dt.Rows)
{
string strEmployee = dr["Employee Name"].ToString();
//store data into employeeshift DB
using (satsEntities db = new satsEntities())
{
ufi empShift;
IList<employeeschedule> employeeList = dict[strEmployee];
foreach (employeeschedule es in employeeList)
{
empShift = new ufi();
TimeSpan a = new TimeSpan(Convert.ToInt32(es.startTime.Substring(0,2), Convert.ToInt32(es.startTime.Substring(2,2))));
TimeSpan b = new TimeSpan(Convert.ToInt32(es.endTime.Substring(0,2), Convert.ToInt32(es.endTime.Substring(2,2))));
empShift.UFISDate = es.day;
empShift.EmployeeName = strEmployee;
empShift.startTime = a;
empShift.endTime = b;
db.ufis.AddObject(empShift);
}
db.SaveChanges();
}
}
MessageBox.Show("Data is stored to database successfully.");
}
catch (Exception ex)
{
//showMessage("UNABLE to store to database successfully.");
//show error
}
}
I can’t post my own answer due to my low reputation. After all, this is my answer that get my code working.
private void btnSave_Click(object sender, EventArgs e)
{
foreach (DataRow dr in dt.Rows)
{
string strEmployee = dr["Employee Name"].ToString();
//store data into employeeshift DB
using (satsEntities db = new satsEntities())
{
ufi empShift;
IList<employeeschedule> employeeList = dict[strEmployee];
foreach (employeeschedule es in employeeList)
{
empShift = new ufi();
TimeSpan a = new TimeSpan(Convert.ToInt32(es.startTime.Substring(0,2)), Convert.ToInt32(es.startTime.Substring(2,2)), 0);
TimeSpan b = new TimeSpan(Convert.ToInt32(es.endTime.Substring(0,2)), Convert.ToInt32(es.endTime.Substring(2,2)), 0);
empShift.UFISDate = es.day;
empShift.EmployeeName = strEmployee;
empShift.startTime = a;
empShift.endTime = b;
db.ufis.AddObject(empShift);
db.SaveChanges();
}
}
}
MessageBox.Show("Data is stored to database successfully.");
}
will this help