Невозможно сохранить в таблицу или базу данных - PullRequest
0 голосов
/ 01 мая 2019
this.allDataTablesSuperDrive.TimeSheet.Rows.Add(rowCount, nameSelected, day, hour, null, null, null, null);

this.allDataTablesSuperDrive.TimeSheet.AcceptChanges();

Я не могу сохранить с помощью этого кода, я обновляю таблицы данных и таблицы, и ничего не отображается.

Я попытался использовать обновление и заполнить, оба возвращаются с ошибкой, говоря, что они не доступны с этим. Пожалуйста, помогите

Редактировать: я не получаю никаких исключений при запуске кода, единственная проблема заключается в том, что когда я проверяю, обновлены ли база данных или таблицы, ничего не изменилось. Я попытался заполнить и обновить, а также обновить другие формы, но ничего не появляется.

Также 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;
    }
...