0

何らかの理由で、フォームに保存した新しいレコードがデータベースに追加されません。

修正方法がわからないというエラーが表示されます

必要な情報が他にある場合はお知らせください。誰かが助けてくれる場合は、さらに資料をアップロードします

        double addCrd;
        double addSav;
        double addHomeLoan;
        double addHomeRate;


        bool CredOK = double.TryParse(creditBox.Text, out addCrd);
        bool SavOK = double.TryParse(savingsBox.Text, out addSav);
        bool HoloOK = double.TryParse(homeLoanBalanceBox.Text, out addHomeLoan);
        bool HoRatOK = double.TryParse(homeLoanRateBox.Text, out addHomeRate);
        bool exists = false;

        foreach (Client client in records)
        {
            if (nameBox.Text == client.Name)
            {
                exists = true;
            }
        }
        try
        {
            if (YesLoanRadio.Checked)
            {
                clientTypeBox.Text = "LC";
                if (exists == false)
                {
                    if (!CredOK || !SavOK || !HoloOK || !HoRatOK)
                    {
                        MessageBox.Show("Invalid Data");
                    }
                    else
                    {
                        Client add = new LoanClient(clientTypeBox.Text,MakeID(), nameBox.Text, suburbBox.Text, addCrd, addSav, addHomeLoan, addHomeRate);
                        records.Add(add);
                        sql_1 = "INSERT INTO Client VALUES ('LC'," + "'" + (records[position] as LoanClient).ID + "'" + "," + "'" + (records[position] as LoanClient).Name + "'" + "," + "'" + (records[position] as LoanClient).Suburb + "'" + "," + "'" + (records[position] as LoanClient).CreditBalance + "'" + "," + "'" + (records[position] as LoanClient).SavingsBalance + "'" + "," + "'" + (records[position] as LoanClient).HomeLoanBalance + "'" + "," + "'" + (records[position] as LoanClient).HomeLoanInterestRate + "')";
                        showform();
                        MessageBox.Show("User Added");
                        position++;
                    }
                }
            }
            else if (NoLoanRadio.Checked)
            {
                clientTypeBox.Text = "BC";
                if (exists == false)
                {
                    if (!CredOK || !SavOK)
                    {
                        MessageBox.Show("Invalid Data");
                    }
                    else
                    {
                        Client add = new Client(clientTypeBox.Text, MakeID(), nameBox.Text, suburbBox.Text, addCrd, addSav);
                        records.Add(add);
                        sql_1 = "INSERT INTO Client VALUES ('BC', '" + records[position].ID + "'" + "," + "'" + records[position].Name + "'" + "," + "'" + records[position].Suburb + "'" + "," + "'" + records[position].CreditBalance + "'" + "," + "'" + records[position].SavingsBalance + "','','')";
                        showform();
                        MessageBox.Show("User Added");
                        position++;
                    }
                }
            }
            else
            {
                MessageBox.Show("Please Select if you have a home Loan !!!!");
            }
            try
            {
                DBQuery(sql_1);
            }
            catch (OleDbException e4)
            {
                MessageBox.Show(e4.ToString());
                records.Remove(records[position]);
                position = records.Count - 1;
            }
        }
        catch
        {
            MessageBox.Show("Invalid entry. You must enter a number value into Balance and Credit Used.");
        }

データベースクエリメソッドは

   public void DBQuery(string sql) // changed static from public 
   {
       try
       {
           conn.Open();
           OleDbCommand cmd = new OleDbCommand(sql, conn);
           cmd.ExecuteNonQuery();

       }
       catch (OleDbException e)
       {
           MessageBox.Show(e.ToString());
       }
       conn.Close();
   }
4

1 に答える 1

1

SQL例外コードを試してください...

catch (DbUpdateException ex)
                        {
                            var sqlex = ex.InnerException.InnerException as SqlException;

                            if (sqlex != null)
                            {
                                switch (sqlex.Number)
                                {
                                    case 547: throw new ExNoExisteUsuario("No existe usuario destino."); //FK exception
                                    case 2627:
                                    case 2601:
                                        throw new ExYaExisteConexion("Ya existe la conexion."); //primary key exception

                                    default: throw sqlex; //otra excepcion que no controlo.


                                }
                            }

                            throw ex;
                        }
于 2013-10-16T02:50:06.547 に答える