locked
Error - Invalid expression term else RRS feed

  • Question

  • User-2074858223 posted

     Hi please i have this code that i modified in this forum the original code inserts into sells table then updates products, but i wanted to modify it so that it will first insert sells then update payment table but if the record doesnt exist in payment table insert data

    string productcategory = ddlproductcategory.SelectedItem.Text.Trim();
                                string name = ddlcustomername.SelectedItem.Text.Trim();
                                string address = ddlcustomeraddress.SelectedItem.Text.Trim();
                                decimal unitPrice = Convert.ToDecimal(txtprice.Text.Trim());
                                //  decimal deposit = Convert.ToDecimal(txtdeposit.Text.Trim());
                                int quantity = Convert.ToInt32(txtQuantity.Text.Trim());
                                int inserted = 0;
    
                                // int availableInventoryQty = lblQuantityAvailable();
                                if (txtprice.Text.Length > 0 && txtQuantity.Text.Length > 0)
                                {
                                    //  double row = Convert.ToDouble(txtrow.Text);
                                    //  double deposit = Convert.ToDouble(txtamountpaid.Text);
                                    double stock = Convert.ToDouble(txtQuantity.Text);
                                    double customerqty = Convert.ToDouble(txtQuantity.Text);
                                    double tqty = stock - customerqty;
                                    double gqty = tqty;
                                    {
                                        if (txtprice.Text.Length > 0 && txtQuantity.Text.Length > 0)
                                        {
                                            float balance = !string.IsNullOrEmpty(txtamountpaid.Text.Trim()) ? float.Parse(txtamountpaid.Text.Trim()) : float.Parse("0"); ;
                                            double unitprice2 = Convert.ToDouble(txtprice.Text);
                                            double customerqty2 = Convert.ToDouble(txtQuantity.Text);
                                            double grandtotal = unitprice2 * customerqty;
                                            double total = grandtotal;
                                            double bal = grandtotal - balance;
                                            double remain = bal;
                                            // double finalamount = grandtotal - deposit;
                                            // double semamount = finalamount;
                                            {
                                                if (txtrow.Text.Length > 0 && lblQuantityAvailable.Text.Length > 0)
                                                {
                                                    float rowavailable = !string.IsNullOrEmpty(lblQuantityAvailable.Text.Trim()) ? float.Parse(lblQuantityAvailable.Text.Trim()) : float.Parse("0"); ;
                                                    float row = !string.IsNullOrEmpty(txtrow.Text.Trim()) ? float.Parse(txtrow.Text.Trim()) : float.Parse("0"); ;
                                                    // double unitprice2 = Convert.ToDouble(txtprice.Text);
                                                    // double customerqty2 = Convert.ToDouble(txtQuantity.Text);
                                                    double totalrow = row / rowavailable;
                                                    double finalrow = totalrow;
                                                    // double bal = grandtotal - balance;
                                                    // double remain = bal;
                                                    // {
    
    
    
                                                    //  if ((availableQuantity != 0) && (quantity != 0) && (quantity < availableQuantity))
                                                    //{
                                                    using (SqlConnection con = new SqlConnection())
                                                    {
                                                        con.ConnectionString = str;
                                                        using (SqlCommand cmd = new SqlCommand())
                                                        {
                                                            cmd.CommandType = CommandType.Text;
                                                            cmd.Connection = con;
                                                            //Change the query like below
                                                            //Make sure the column name matches the with the name provided in code as per your table
                                                            cmd.CommandText = "INSERT INTO Sells (UserName,CustomerName,CustomerAddress,ProductCategory,UnitPrice,Quantity,PaymentStatus,Rows_Ordered,Cartons,TotalAmount,AmountPaid,Balance) VALUES(@UserName,@CustomerName,@CustomerAddress,@ProductCategory,@UnitPrice,@Quantity,@PaymentStatus,@Rows_Ordered,@Cartons,@TotalAmount,@AmountPaid,@Balance)";
                                                            cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
                                                            cmd.Parameters.AddWithValue("@CustomerName", ddlcustomername.SelectedItem.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@CustomerPhone", ddlcustomerph.SelectedItem.Text.Trim());
                                                            //  cmd.Parameters.AddWithValue("@CustomerPhone", customerphone);
                                                            cmd.Parameters.AddWithValue("@ProductCategory", productcategory);
                                                            //  cmd.Parameters.AddWithValue("@Quantity_PerSqM", txtQuantity.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@UnitPrice", txtprice.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@Quantity", txtQuantity.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@PaymentStatus", ddlpaymentstatus.SelectedItem.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@TotalAmount", grandtotal);
                                                            cmd.Parameters.AddWithValue("@Rows_Ordered", txtrow.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@Cartons", finalrow);
                                                            cmd.Parameters.AddWithValue("@AmountPaid", txtamountpaid.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@Balance", bal);
                                                            con.Open();
                                                            inserted = cmd.ExecuteNonQuery();
                                                            con.Close();
                                                            string message = string.Empty;
                                                            {
                                                                lblMessage2.Visible = true;
                                                                lblMessage2.Text = "Data Submitted Successfully";
                                                            }
                                                            ScriptManager.RegisterClientScriptBlock(btnSave, this.GetType(), "alert", "<script>alert('Data Submitted Successfully ... !!')</script>", false);
    
                                                        }
    
                                                    }
    
                                                    if (inserted > 0)
                                                    {
                                                        {
                                                            // float updatepayment = amountavailable + amountpaid;
                                                           // float balanceup = amountavailable - amountpaid;
                                                           // float balance = balanceup;
                                                            using (SqlConnection con = new SqlConnection())
                                                            {
                                                                con.ConnectionString = str;
                                                                using (SqlCommand cmd = new SqlCommand())
                                                                {
                                                                    cmd.CommandType = CommandType.Text;
                                                                    cmd.Connection = con;
                                                                    cmd.CommandText = "UPDATE Payments SET PaymentStatus =@PaymentStatus,CustomerName=@CustomerName,AmountPaid=@AmountPaid,Balance=@Balance WHERE CustomerName = @CustomerName AND CustomerPhone=@CustomerPhone";
                                                                    cmd.Parameters.AddWithValue("@PaymentStatus", ddlpaymentstatus.SelectedItem.Text);
                                                                    cmd.Parameters.AddWithValue("@TotalAmount", balanceup);
    cmd.Parameters.AddWithValue("@CustomerPhone", ddlcustomerph.SelectedItem.Text.Trim());
                                                                    cmd.Parameters.AddWithValue("@AmountPaid", txtamount.Text.Trim());
                                                                    cmd.Parameters.AddWithValue("@Balance", balanceup);
                                                                    cmd.Parameters.AddWithValue("@CustomerName", ddltableid.SelectedItem.Text);
    
                                                                    con.Open();
    
                                                                    inserted = cmd.ExecuteNonQuery();
                                                                    con.Close();
                                                                }
                                                              
                                                                
                                                                else
                                                                {
    using (SqlConnection con = new SqlConnection())
                                                    {
                                                        con.ConnectionString = str;
                                                        using (SqlCommand cmd = new SqlCommand())
                                                        {
                                                            cmd.CommandType = CommandType.Text;
                                                            cmd.Connection = con;
                                                            //Change the query like below
                                                            //Make sure the column name matches the with the name provided in code as per your table
                                                            cmd.CommandText = "INSERT INTO Payments (UserName,CustomerName,CustomerPhone,ProductCategory,PaymentStatus,Rows_Ordered,Cartons,TotalAmount,AmountPaid,Balance) VALUES(@UserName,@CustomerName,@CustomerAddress,@ProductCategory,@PaymentStatus,@Rows_Ordered,@Cartons,@TotalAmount,@AmountPaid,@Balance)";
                                                            cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
                                                            cmd.Parameters.AddWithValue("@CustomerName", ddlcustomername.SelectedItem.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@CustomerPhone", ddlcustomerph.SelectedItem.Text.Trim());
                                                            //  cmd.Parameters.AddWithValue("@CustomerPhone", customerphone);
                                                            cmd.Parameters.AddWithValue("@ProductCategory", productcategory);
                                                            //  cmd.Parameters.AddWithValue("@Quantity_PerSqM", txtQuantity.Text.Trim());
                                                           // cmd.Parameters.AddWithValue("@UnitPrice", txtprice.Text.Trim());
                                                            //cmd.Parameters.AddWithValue("@Quantity", txtQuantity.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@PaymentStatus", ddlpaymentstatus.SelectedItem.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@TotalAmount", grandtotal);
                                                            cmd.Parameters.AddWithValue("@Rows_Ordered", txtrow.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@Cartons", finalrow);
                                                            cmd.Parameters.AddWithValue("@AmountPaid", txtamountpaid.Text.Trim());
                                                            cmd.Parameters.AddWithValue("@Balance", bal);
                                                            con.Open();
                                                            inserted = cmd.ExecuteNonQuery();
                                                            con.Close();
                                                                }
                                                            }
                                                        }
                                                        BindProductsSells();
                                                    }
    
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    Tuesday, December 5, 2017 11:55 AM

Answers

  • User465171450 posted

    Your code in imbalanced with regards to opening and closing braces. The else is falling within the using block: using (SqlConnection con = new SqlConnection()) { <sub></sub><sup></sup>

    You may be missing a closing } 

    You also have extra { that don't serve any purpose and could be throwing off the balance of where things should be closed. Example:

    if (inserted > 0) { { <sub></sub><sup></sup>

    There is no need for the second {

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 5, 2017 5:14 PM