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
.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
.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
.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);
}
}


brunda kRank #3,804 · 389 reputationPosted Sep 17, 2012, 1:25 AM EDT
divya146 reputationPosted Sep 17, 2012, 2:42 AM EDT
thanku
brunda kRank #3,804 · 389 reputationPosted Sep 17, 2012, 3:09 AM EDT
AddQuantity(); looks like this is only causing problem.
remove the code and try.
Thanks
divya146 reputationPosted Sep 17, 2012, 4:46 AM EDT
iam facing a problem in else block only,here
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)
{
//Update quantity
}
else
{
//add new row
}
}
here for second row on words new row is inserted instead of update.
thanka & regards
divya
R ACHUTHA181 reputationPosted Sep 17, 2012, 6:28 AM EDT
Hi,
try the following code solve your problem.
bool status=true;
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)
{
//Update quantity
status=false;
break;
}
}
if(status=="true")
{
//add new row
}