无法保存到表或数据库

时间:2019-05-01 00:49:39

标签: c# database save

this.allDataTablesSuperDrive.TimeSheet.Rows.Add(rowCount, nameSelected, day, hour, null, null, null, null);

this.allDataTablesSuperDrive.TimeSheet.AcceptChanges();

我无法使用此代码进行保存,我刷新了数据网格和表,但没有任何显示。

我尝试使用更新和填充,但两者都返回错误,提示它们对此不可用。请帮助

编辑:我在运行代码时没有任何异常,唯一的问题是当我检查数据库或gridview是否已更新时,什么都没有改变。我尝试填写并更新,还刷新了其他表单,但是什么也没有出现。

还有allDataTablesSuperDrive,这是我使用的绑定源。

public int allTimes(int buttonPressed)
    {
        int status = 0;
        string nameSelected = employeeComboBox.Text;
        int selection = buttonPressed;
        DateTime day = DateTime.Now.Date;
        string hour = DateTime.Now.ToString("HH:mm:ss");
        DateTime hourR = DateTime.Parse(hour);
        string selectionMade = clockInButton.Text.ToString();
        selectionMade = selectionMade.Trim();
        if (selectionMade == "Clock In")
        {
            string employeeSelected = employeeComboBox.Text.ToString();
            employeeSelected = employeeSelected.Trim();
            string passwordInput = passwordTextBox.Text.ToString();               
            int rowCount = allDataTablesSuperDrive.TimeSheet.Rows.Count;
            int employeeCheck = allDataTablesSuperDrive.Table.Rows.Count;
            for (int x = 0; x <= employeeCheck;)
            {
                string employeeOnFile = allDataTablesSuperDrive.Table.Rows[x][1].ToString();
                employeeOnFile = employeeOnFile.Trim();
            if(employeeSelected == employeeOnFile)
            {
                string passWordCheck = passwordInput.Trim();
                string passwordOnFile = allDataTablesSuperDrive.Table.Rows[x][2].ToString();
                passwordOnFile = passwordOnFile.Trim();
            if (passWordCheck == passwordOnFile)
            {                            
                if (selection == 1)
                {
                    rowCount++;
                    this.allDataTablesSuperDrive.TimeSheet.Rows.Add(rowCount, nameSelected, day, hour, null, null, null, null);                        
                    this.allDataTablesSuperDrive.TimeSheet.AcceptChanges();
                    status = 0;
                    return status;
                }
                else
                {
                    for (int i = 0; i < rowCount; i++)
                    {
                        string employeeName = allDataTablesSuperDrive.TimeSheet.Rows[i][1].ToString();
                        string dateLogged = allDataTablesSuperDrive.TimeSheet.Rows[i][2].ToString();
                        string daySecond = day.ToString();
                        if (employeeName == nameSelected && daySecond == dateLogged)
                        {
                            if (selection == 2)
                            {
                                string clockOutHour = DateTime.Now.ToString("HH:mm:ss");
                                DateTime clockOut = DateTime.Parse(clockOutHour);
                                allDataTablesSuperDrive.TimeSheet.Rows[i][4] = clockOut;
                                allDataTablesSuperDrive.AcceptChanges();
                                if(allDataTablesSuperDrive.TimeSheet.Rows[i][5] != null && allDataTablesSuperDrive.TimeSheet.Rows[i][6] != null)
                                {
                                    string timeOne = allDataTablesSuperDrive.TimeSheet.Rows[i][5].ToString();
                                    string timeTwo = allDataTablesSuperDrive.TimeSheet.Rows[i][6].ToString();
                                    string timeIn = allDataTablesSuperDrive.TimeSheet.Rows[i][3].ToString();
                                    string timeOut = allDataTablesSuperDrive.TimeSheet.Rows[i][4].ToString();
                                    DateTime first = DateTime.Parse(timeOne);
                                    DateTime last = DateTime.Parse(timeTwo);
                                    DateTime inTime = DateTime.Parse(timeIn);
                                    DateTime outTime = DateTime.Parse(timeOut);
                                    TimeSpan lunchHourFinal = last - first;
                                    TimeSpan workFinal = outTime - inTime;
                                    TimeSpan totalTime = workFinal - lunchHourFinal;
                                    allDataTablesSuperDrive.TimeSheet.Rows[i][7]=totalTime;
                                    allDataTablesSuperDrive.AcceptChanges();
                                    status = 0;
                                    return status;
                                }
                            }
                            else if (selection == 3)
                            {
                                string lunchHour = DateTime.Now.ToString("HH:mm:ss");
                                DateTime LunchOut = DateTime.Parse(lunchHour);
                                allDataTablesSuperDrive.TimeSheet.Rows[i][5] = LunchOut;
                                allDataTablesSuperDrive.AcceptChanges();
                                status = 0;
                                return status;
                            }
                            else if (selection == 4)
                            {
                                string returnHour = DateTime.Now.ToString("HH:mm:ss");
                                DateTime LunchIn = DateTime.Parse(returnHour);
                                allDataTablesSuperDrive.TimeSheet.Rows[i][6] = LunchIn;
                                allDataTablesSuperDrive.AcceptChanges();
                                status = 0;
                                return status;
                            }
                        }
                    }
                }

                }
                else
                {
                    status = -1;
                    return status;
                }
            }
        else
        {
            x++;
        }
        }
        }
        else if(selectionMade == "Return")
        {
            string batchesInput = batchesTextBox.Text.ToString();
            string quantity = qtyTextBox.Text.ToString();
            string passwordInput = passwordBox.Text.ToString();
            string receivedTime = DateTime.Now.ToString("HH:mm:ss");
            DateTime timeReceived = DateTime.Parse(receivedTime);
            int quantityGiven = Int32.Parse(quantity);
            int rowCount = allDataTablesSuperDrive.TimeSheet.Rows.Count;
            for (int i =0; i<rowCount; i++)
            {
                string nameSearch = allDataTablesSuperDrive.Table.Rows[i][1].ToString();
                string passwordCheck = allDataTablesSuperDrive.Table.Rows[i][2].ToString();
                passwordCheck = passwordCheck.Trim();
                if (passwordInput == passwordCheck)
                {
                    if (selection == 5)
                    {
                        rowCount++;
                        string timeGiven = allDataTablesSuperDrive.Table.Rows[i][3].ToString();
                        int timeProvided = Convert.ToInt32(timeGiven);
                        int expectedTime = timeProvided*quantityGiven;
                        string expectedTime2 = Convert.ToString(expectedTime);
                        DateTime finalExpectedTime = DateTime.Parse(expectedTime2);
                        allDataTablesSuperDrive.TimeSheet.Rows.Add(rowCount, nameSearch, batchesInput, quantity,receivedTime,null, finalExpectedTime,null);
                        allDataTablesSuperDrive.AcceptChanges();
                        MessageBox.Show($"Time${finalExpectedTime}");
                        status = 0;
                        return status;
                    }
                    else if (selection == 6)
                    {
                        status = 0;
                        return status;
                    }
                }
                else
                {
                    status = -1;
                    return status;
                }
            }
        }
        else
        {
            status = -1;
            return status;
        }
        return status;
    }

0 个答案:

没有答案