Philemon
Philemon

Reputation: 101

Comparing column with column using linq query

I encountered a problem that I'm currently stuck at. Example:

EmployeeShiftID |     ShiftTime_Start     |     hiftTime_Stop     |     Name     | Emp_Start | Emp_Stop
                |(linked with foreign key)|    (linked with FK)   |              |           |
    1           |       0000              |         1000          |      Ken     |    0000   |    1000

These data was shown in a data grid view with foreign keys linked. And the Shift start and stop also match the Emp_Start and stop. The problem is that when I update the Emp_start and stop, the ShiftTime_Start and stop doesn't compare to what the Emp_Start and Stop and remains as 0000 and 1000 as I tried to change the Emp_Start and stop to 0430 and 2100.

The type of time I saved in database is String instead of type 'time'. Can anyone help me with that? I will show whatever codes I done for it.

private void btnUpdate_Click(object sender, EventArgs e)
{
    using (testEntities Setupctx = new testEntities())
    {
        int ID = Int32.Parse(lblID.Text);
        var ESquery = (from es in Setupctx.employeeshifts
                       where es.EmployeeShiftID == ID
                       select es).First();

        ESquery.StartTime = txtStart.Text;
        ESquery.EndTime = txtStop.Text;
        ESquery.Date = txtDate.Text;
        Setupctx.SaveChanges();
        txtStart.Text = "";
        txtStop.Text = "";
        txtDate.Text = "";
        this.Edit_Employee_Shift_Load(null, EventArgs.Empty);
        MessageBox.Show("Employee's Shift Has Been Updated.");
    }
}


private void LoadAllEditEmpShift()
{
    using (testEntities Setupctx = new testEntities())
    {
        BindingSource BS = new BindingSource();
        var Viewemp = from ES in Setupctx.employeeshifts
                      join shifthour sh in Setupctx.shifthours on ES.ShiftHourID equals sh.idShiftHours
                      select new
                      {
                          ES.EmployeeShiftID,
                          ShiftHour_Start = sh.shiftTiming_start,
                          ShiftHour_Stop = sh.shiftTiming_stop,
                          ES.EmployeeName,
                          ES.StartTime,
                          ES.EndTime,
                          ES.Date
                      };

        BS.DataSource = Viewemp;
        dgvEmpShift.DataSource = BS;
    }
}

Upvotes: 0

Views: 151

Answers (2)

chameleon
chameleon

Reputation: 1004

I think it's should look like this:

private void btnUpdate_Click(object sender, EventArgs e)
{
    using (testEntities Setupctx = new testEntities())
    {
        int ID = Int32.Parse(lblID.Text);
        var ESquery = (from es in Setupctx.employeeshifts
                       where es.EmployeeShiftID == ID
                       select es).First();
        var SHquery = (from sh in Setupctx.shifthours where sh.idShiftHours == ESQuery.ShiftHourID 
                      select sh).First();

        ESquery.StartTime = txtStart.Text;
        ESquery.EndTime = txtStop.Text;
        ESquery.Date = txtDate.Text;
        SHquery.shiftTiming_start = ESquery.StartTime;
        SHquery.shiftTiming_stop = ESquery.EndTime;
        Setupctx.SaveChanges();
        txtStart.Text = "";
        txtStop.Text = "";
        txtDate.Text = "";
        this.Edit_Employee_Shift_Load(null, EventArgs.Empty);
        MessageBox.Show("Employee's Shift Has Been Updated.");
    }
}

Upvotes: 1

chameleon
chameleon

Reputation: 1004

You should update shifthours object's properties implicitly in your btnUpdate_Click method.

Upvotes: 0

Related Questions