locked
Specified cast is not valid. error C# RRS feed

  • Question

  • try
                {
                    if(MessageBox.Show("Confirm Monthly Process to Continue...!", "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        myMonthlySetting.IntYearCode = Convert.ToInt32(cmbYear.Text);
                        myMonthlySetting.IntMonthCode = Convert.ToInt32(cmbMonth.SelectedValue.ToString());

                        Decimal BoughtLeafRateBest = myMonthlySetting.getBoughtLeafRateBest(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()),1);
                        Decimal BoughtLeafRateGood = myMonthlySetting.getBoughtLeafRateGood(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), 2);
                        Decimal BoughtLeafRateNormal = myMonthlySetting.getBoughtLeafRateNormal(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()),3);
                        Decimal StampDutyAmount = myMonthlySetting.getStampDutyAmount(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()));
                        Decimal StampDutyLevel = myMonthlySetting.getStampDutyLevel(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()));
                        
                        
                        myRoute.StrRouteCode = cmbRoute.SelectedValue.ToString();
                        Decimal TransportRate = myRouteSetting.getTransportRate();

                        if (BoughtLeafRateBest > 0 && BoughtLeafRateGood > 0 && BoughtLeafRateNormal > 0)
                        {
                            using (TransactionScope scope = new TransactionScope())
                            
                            {
                                DataTable dt = mySupplier.ListActiveSuppliers(cmbRoute.SelectedValue.ToString());
                                if(dt.Rows.Count > 0)
                                {
                                    progressBarProcess.Minimum = 0;
                                    progressBarProcess.Maximum = dt.Rows.Count;

                                    for ( Int32 i=0; i <= dt.Rows.Count - 1; i++)
                                    {
                                        if (dt.Rows[i][0].ToString() == "101")
                                        {
                                            String dd = "";
                                        }

                                        myGreenLeaf.StrSupplierCode = dt.Rows[i][0].ToString();
                                    

                                        Decimal TotalGreenLeafBest = myGreenLeaf.getUnProcessedSupplierWiseGreenLeaf(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString(), 1);
                                        Decimal TotalGreenLeafGood = myGreenLeaf.getUnProcessedSupplierWiseGreenLeaf(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString(), 2);
                                        Decimal TotalGreenLeafNormal = myGreenLeaf.getUnProcessedSupplierWiseGreenLeaf(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString(), 3);

                                        Decimal TotalGreenLeaf = TotalGreenLeafBest + TotalGreenLeafGood + TotalGreenLeafNormal;

                                        Decimal BestLeafValue = BoughtLeafRateBest * TotalGreenLeafBest;
                                        Decimal GoodLeafValue = BoughtLeafRateGood * TotalGreenLeafGood;
                                        Decimal NormalLeafValue = BoughtLeafRateNormal * TotalGreenLeafNormal;

                                        Decimal TotalLeafValue = BestLeafValue + GoodLeafValue + NormalLeafValue;

                                        Decimal TransportCharges = TotalGreenLeaf * TransportRate;

                                        myAdvancePayment.StrSupplierCode = dt.Rows[i][0].ToString();

                                        Decimal MonthlyAdvanceTotalAmount = myAdvancePayment.getSupplierAdvances(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString());//Correct

                                        Decimal FertlizerAmount = myFertilizerIssue.getFertilizerIssue(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString(), dt.Rows[i][0].ToString()); //Correct
                                        Decimal FertilizerBalance = myFertilizerIssue.getFertilizerBalance(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString(), dt.Rows[i][0].ToString());//correct


                                        Decimal LoanDeductionInstallment = myLoan.getSupplierLoan(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString(), dt.Rows[i][0].ToString());//Correct
                                        Decimal LoanBalance = myLoan.getLoanBalance(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString(), dt.Rows[i][0].ToString());//correct

                                        Decimal OtherAddition = myOtherAddition.getSupplierAdditionUnProcess(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString(), dt.Rows[i][0].ToString());

                                        myMadeTeaIssue.StrSupplierCode = dt.Rows[i][0].ToString();

                                        Decimal MadeTeaIssueAmount = myMadeTeaIssue.getTeaIssueSupplier(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString());

                                        Decimal IncentivePayment = TotalGreenLeaf * myIncentive.getSupplierIncentiveList(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString(), dt.Rows[i][0].ToString());

                                        Decimal BFCoins = myProcess.getBFCoins(Convert.ToInt32(cmbYear.Text), Convert.ToInt32(cmbMonth.SelectedValue.ToString()), cmbRoute.SelectedValue.ToString(), dt.Rows[i][0].ToString());


                                        Decimal TotalEarnings = TotalLeafValue + IncentivePayment + OtherAddition + BFCoins;
                                        Decimal TotalDeduction = MonthlyAdvanceTotalAmount + MadeTeaIssueAmount + LoanDeductionInstallment + TransportCharges + StampDutyAmount;

                                        Decimal StampDutyPay = 0;

                                        Decimal NewEarnings = TotalLeafValue + IncentivePayment + OtherAddition;

                                        if (NewEarnings >= StampDutyLevel)
                                        {
                                            StampDutyPay = StampDutyAmount;
                                        }
                                        else
                                        {
                                            StampDutyPay = 0;
                                        }

                                        myProcess.DecAdvanceIssueAmount = MonthlyAdvanceTotalAmount;

                                        myProcess.DecFertilizerIssueInstallment = FertlizerAmount;

                                        myProcess.DecMadeTeaIssueAmount = MadeTeaIssueAmount;

                                        myProcess.DecStampDutyDeduction = StampDutyPay;

                                        myProcess.DecTotalLeafBest = TotalGreenLeafBest;
                                        myProcess.DecTotalLeafGood = TotalGreenLeafGood;
                                        myProcess.DecTotalLeafNormal = TotalGreenLeafNormal;
                                        myProcess.DecTotalGreenLeaf = TotalGreenLeaf;

                                        myProcess.DecBestLeafValue = BestLeafValue;
                                        myProcess.DecGoodLeafValue = GoodLeafValue;
                                        myProcess.DecNormalLeafValue = NormalLeafValue;
                                        myProcess.DecTotalGreenLeafValue = TotalLeafValue;

                                        myProcess.DecOtherAdditonAmount = OtherAddition;
                                        myProcess.DecIncentivePayment = IncentivePayment;
                                        myProcess.DecBFCoins = BFCoins;
                                        myProcess.DecTotalEarnings = TotalEarnings;

                                        myProcess.DecLoanInstallment = LoanDeductionInstallment;
                                        myProcess.DecTransportDeduction = TransportCharges;

                                        myProcess.IntYearCode = Convert.ToInt32(cmbYear.Text);
                                        myProcess.IntMonthCode = Convert.ToInt32(cmbMonth.SelectedValue.ToString());
                                        myProcess.StrRouteCode = cmbRoute.SelectedValue.ToString();
                                        myProcess.StrSupplierCode = dt.Rows[i][0].ToString();

                                        Decimal FinalBalance = 0;
                                        Decimal BF = 0;

                                        Decimal SubTotal = TotalEarnings - StampDutyPay;

                                        if (TotalEarnings > 0)
                                        {
                                            BF = (TotalEarnings - StampDutyPay) % 10;
                                        }
                                        FinalBalance = TotalEarnings - BF - StampDutyPay;

                                        Decimal CashBalance = 0;
                                        
                                        if (FinalBalance < 0)
                                            CashBalance = FinalBalance;

                                        myProcess.InsertMonthlyEarningMaster();

                                        myProcess.InsertMonthlyPayment();

                                        progressBarProcess.Value = i;
                                    }
                                }
                                dt.Dispose();

                                scope.Complete();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Bought Leaf Rate Not Defined for This Month");
                        }
                        MessageBox.Show("Process Complete Successfully...!!");
                        progressBarProcess.Value = 0;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    Sunday, November 19, 2017 6:57 AM

Answers

  • Look for those Convert lines and check if value that is being passed is a valid value which can be converted to your prescribed type '

    for example

    Convert.ToInt32(cmbYear.Text)

    will throw error if cmbYear doesnt have valid numeric value etc


    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    ----------------------------
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

    Sunday, November 19, 2017 7:18 AM

All replies

  • Look for those Convert lines and check if value that is being passed is a valid value which can be converted to your prescribed type '

    for example

    Convert.ToInt32(cmbYear.Text)

    will throw error if cmbYear doesnt have valid numeric value etc


    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    ----------------------------
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

    Sunday, November 19, 2017 7:18 AM
  • thanks ... i'll recheck it

    Sunday, November 19, 2017 8:33 AM