divya

divya

  • NA
  • 186
  • 111.9k

help me to write loop for below code??

Sep 15 2012 1:35 AM
       
        Hai,
   
           when i click add it should check whether the item exists or not if exists update the quantity other wise add the new row.for first row it is working fine after that instead of getting increase the quantity new row is inserted.iam giving my code please correct where the issue is.

     private void btnAdd_Click(object sender, EventArgs e)
        {
            int Quantity1 = 1;
            try
            {
                if (txtItemNo.Text == "")
                {
                    MessageBox.Show("Please enter Item no.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (CheckItemNo() == false)
                        return;                  
                        AddQuantity();
                        if (GvSellCd.Rows.Count == 0)
                        {
                            AddQuantity();
                            int Quantity = 1;
                            DataRow rd = this.myTable.NewRow();
                            // set values
                            rd[0] = txtItemNo.Text;
                            rd[1] = LblItemName.Text;
                            rd[2] = lblItemDescription.Text;
                            rd[3] = lblCdCost.Text;
                            rd[4] = Quantity.ToString();
                            rd[5] = lblCdCost.Text;
                           this.myTable.Rows.Add(rd);
                            decimal sum = GvSellCd.Rows.OfType<DataGridViewRow>()
                                .Sum(row => Convert.ToDecimal(row.Cells["Amount"].Value));
                            txtTotalAmount.Text = sum.ToString();
                            txtItemNo.Clear();
                        }
                        else
                        {

                            for (int j = 0; j < GvSellCd.Rows.Count; j++)
                            {
                                string a = GvSellCd.Rows[j].Cells[0].Value.ToString();
                                string v = txtItemNo.Text;
                                if (v == a)
                                {
                                    int Quantity = Convert.ToInt32(GvSellCd.Rows[j].Cells[4].Value.ToString());
                                    int addingvalue = Convert.ToInt16(Quantity + 1);
                                    string str = "select * from vwItemList where ItemNo='" + txtItemNo.Text + "'";
                                    SqlDataReader dr = cd.SqlReaderQuery(str);
                                    if (dr.Read())
                                    {
                                        int num = Convert.ToInt16(dr["NoOfCopies"].ToString());
                                        if (addingvalue > num)
                                        {
                                            MessageBox.Show("You cant sell items than exists i.e,+GvSellCd.Rows[i].Cells[1].Value.ToString()", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                                        }
                                        else
                                        {
                                            GvSellCd.Rows[j].Cells[4].Value = addingvalue;
                                            double cost = Convert.ToDouble(GvSellCd.Rows[j].Cells[3].Value.ToString());
                                            GvSellCd.Rows[j].Cells[5].Value = addingvalue * cost;
                                            decimal sum = GvSellCd.Rows.OfType<DataGridViewRow>()
                                       .Sum(row => Convert.ToDecimal(row.Cells["Amount"].Value));
                                            txtTotalAmount.Text = sum.ToString();
                                        }
                                        // GvSellCd.Rows.RemoveAt(i1);
                                        dr.Close();
                                        return;
                                    }

                                }
                                else if (v != a)
                                {
                                    DataRow rd = this.myTable.NewRow();
                                    // set values
                                    rd[0] = txtItemNo.Text;
                                    rd[1] = LblItemName.Text;
                                    rd[2] = lblItemDescription.Text;
                                    rd[3] = lblCdCost.Text;
                                    rd[4] = Quantity1.ToString();
                                    rd[5] = lblCdCost.Text;
                                    this.myTable.Rows.Add(rd);
                                    decimal sum = GvSellCd.Rows.OfType<DataGridViewRow>()
                                        .Sum(row => Convert.ToDecimal(row.Cells["Amount"].Value));
                                    txtTotalAmount.Text = sum.ToString();
                                    txtItemNo.Clear();
                                    return;
                                }



                            }   
                        
                                }
                            }
                       // }
                   
              //  }
               
            }
            catch (Exception ex)
            {
                            MessageBox.Show(ex.Message,"Message",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }

       
        }

Answers (5)