Hi jmprateek,
You are looping through the GridViewRow on btnInward_Click and checking the condition and calling method inwardUpdate and inwardConfirm.
But inside nwardUpdate and inwardConfirm method there is no need to again loop through the GridViewRow.
You have to pass the values as parameter to these methods and update it.
So refer the modified code.
//Below code binds another GridView showing the purchase order item details
protected void lnkInward_Click(object sender, EventArgs e)
{
LinkButton lnkInward = (LinkButton)sender;
lblInvcNo.Text = lnkInward.CommandArgument.ToString();
string strQuery = "select * from PurchaseOrder where invcno!='0' and rem!='1' and invcno='" + lnkInward.CommandArgument.ToString() + "' and invcno!=''";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(constring);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
divInvcDetails.Visible = true;
divPOList.Visible = false;
grdInvcDetails.DataSource = dt;
grdInvcDetails.DataBind();
}
//Below code uses bool value to check if the item already exists in the database. If exists, will call inwardUpdate(); else, will call inwardConfirm();
protected void btnInward_Click(object sender, EventArgs e)
{
bool exists = false;
foreach (GridViewRow invcdetailsrow in grdInvcDetails.Rows)
{
Label ITEMNAME = invcdetailsrow.FindControl("lblItemname") as Label;
Label HSN = invcdetailsrow.FindControl("lblHsn") as Label;
Label QTTY = invcdetailsrow.FindControl("lblQtty") as Label;
Label MRP = invcdetailsrow.FindControl("lblMrp") as Label;
Label SGST = invcdetailsrow.FindControl("lblSgst") as Label;
Label CGST = invcdetailsrow.FindControl("lblCgst") as Label;
Label DISC = invcdetailsrow.FindControl("lblDisc") as Label;
using (SqlConnection CheckCon = new SqlConnection(constring))
{
CheckCon.Open();
string CheckQuery = "select count(*) from [Stock] where itemname = @itemname";
using (SqlCommand CheckCmd = new SqlCommand(CheckQuery, CheckCon))
{
CheckCmd.Parameters.AddWithValue("@itemname", ITEMNAME.Text);
exists = (int)CheckCmd.ExecuteScalar() > 0;
}
CheckCon.Close();
if (exists)
{
inwardUpdate(ITEMNAME.Text, QTTY.Text);
divInvcDetails.Visible = false;
divPOList.Visible = true;
}
else
{
inwardConfirm(ITEMNAME.Text, HSN.Text, QTTY.Text, MRP.Text, SGST.Text, CGST.Text, DISC.Text);
divInvcDetails.Visible = false;
divPOList.Visible = true;
}
}
}
}
//Below code is used when item already exists in the database and only quantity will increase
protected void inwardUpdate(string ITEMNAME, string QTTY)
{
using (SqlConnection InwardCon = new SqlConnection(constring))
{
string InwardQuery = "Update Stock set qtty=CAST(IsNULL(qtty, '0') as int) + '" + Convert.ToInt32(QTTY) + "' where itemname = @itemname;";
InwardCon.Open();
using (SqlCommand InwardCmd = new SqlCommand(InwardQuery, InwardCon))
{
InwardCmd.Parameters.AddWithValue("@itemname", ITEMNAME);
int i = InwardCmd.ExecuteNonQuery();
if (i > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "swal", "swal('Stock Updated!', 'Vinod Enterprises, Vijayapura', 'success');", true);
UpdatePOInwarded();
BindPOList();
}
}
InwardCon.Close();
}
}
//Below code is used when item does not exist in the database and an insertion will take place.
protected void inwardConfirm(string ITEMNAME, string HSN, string QTTY, string MRP, string SGST, string CGST, string DISC)
{
using (SqlConnection InsertInwardCon = new SqlConnection(constring))
{
string InsertInwardQuery = "insert into Stock(itemname, hsn, qtty, mrp, sgst, cgst, disc, status, rem) values(@i1, @i2, @i3, @i4, @i5, @i6, @i7, @i8, @i9);";
InsertInwardCon.Open();
using (SqlCommand InsertInwardCmd = new SqlCommand(InsertInwardQuery, InsertInwardCon))
{
InsertInwardCmd.Parameters.AddWithValue("@i1", ITEMNAME);
InsertInwardCmd.Parameters.AddWithValue("@i2", HSN);
InsertInwardCmd.Parameters.AddWithValue("@i3", QTTY);
InsertInwardCmd.Parameters.AddWithValue("@i4", MRP);
InsertInwardCmd.Parameters.AddWithValue("@i5", SGST);
InsertInwardCmd.Parameters.AddWithValue("@i6", CGST);
InsertInwardCmd.Parameters.AddWithValue("@i7", "0");
InsertInwardCmd.Parameters.AddWithValue("@i8", "1");
InsertInwardCmd.Parameters.AddWithValue("@i9", "0");
int pmj = InsertInwardCmd.ExecuteNonQuery();
if (pmj > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "swal", "swal('Inward Successful!', 'Vinod Enterprises, Vijayapura', 'success');", true);
UpdatePOInwarded();
BindPOList();
}
}
InsertInwardCon.Close();
}
}
//Below code is to set remove status to 0 after inward process
protected void UpdatePOInwarded()
{
using (SqlConnection con = new SqlConnection(constring))
{
string query = "Update PurchaseOrder set rem='1' where invcno=@invcno";
con.Open();
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@invcno", lblInvcNo.Text);
cmd.ExecuteNonQuery();
}
con.Close();
}
}