Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have two tables called purchasedetails and itemmaster , what I want to do is, once I press submit button need to update the itemmaster table. for these I am using stored procedure.

this is what I write the stored procedure. Its working for one row. in my web page am using gridview. how to work it out , please help me.


ALTER PROCEDURE [dbo].[UpdateItem]
@ItemCode NVARCHAR(20),
@CurrentQuantity int = 0,
@WholesalePrice int = 0

AS
BEGIN
UPDATE ItemMaster SET CurrentQuantity = @CurrentQuantity + (SELECT CurrentQuantity

FROM
ItemMaster WHERE ItemCode=@ItemCode) , WholesalePrice=@WholesalePrice WHERE ItemCode = @ItemCode

END


this is my asp code.

try
{

foreach (GridViewRow gr in grvList.Rows)
{


objItemMaster.ItemCode = ((Label)gr.FindControl("txtItemCode")).Text;
objItemMaster.CurrentQuantity = Convert.ToDecimal(((TextBox)gr.FindControl("txtUnit")).Text);
objItemMaster.WholesalePrice = Convert.ToDecimal(((TextBox)gr.FindControl("txtPrice")).Text);
objItemMaster.ModifiedBy = Session["admin"].ToString();
objItemMaster.ModifiedDate = DateTime.Now;

}
objItemDb.UpdateItem(txtItemCode.Text);
}
catch
{

}

-----------------------------------------------------------------------

public bool UpdateItem(string PID)

{
try
{
strSql = "EXEC UpdateItem '" + PID + "'";
return objDB.ExecuteNonQuerySQL(strSql);
}
catch (Exception ex)`enter code here`
{
objDB.LogError(ex.ToString());
return false;
}
}

------------------------------------------------------------------

its not giving error or not giving any result even...
Posted

1 solution

You need to make the update inside the loop, and then actually perform the update with the item code, quantity/price from the grid. Like:

C#
foreach (GridViewRow gr in grvList.Rows)
{
   objItemMaster.ItemCode = ((Label)gr.FindControl("txtItemCode")).Text;
   objItemMaster.CurrentQuantity = Convert.ToDecimal(((TextBox)gr.FindControl("txtUnit")).Text);
   objItemMaster.WholesalePrice = Convert.ToDecimal(((TextBox)gr.FindControl("txtPrice")).Text);
   objItemMaster.ModifiedBy = Session["admin"].ToString();
   objItemMaster.ModifiedDate = DateTime.Now;
   
   objItemDb.UpdateItem(objItemMaster.ItemCode, objItemMaster.CurrentQuantity, objItemMaster.WholesalePrice); 
}


Where the update item function is something like:
C#
public bool UpdateItem(string itemCode, decimal quantity, decimal price)
{
  try
  {
    strSql = "EXEC UpdateItem '" + PID + "', " + quantity + ", " + price;
    return objDB.ExecuteNonQuerySQL(strSql);
  }
  catch (Exception ex)`enter code here`
  {
    objDB.LogError(ex.ToString());
    return false;
  }
}


That should make it work :)
 
Share this answer
 
Comments
tastini 2-Oct-14 5:26am    
Thanks ... so instead of PID in that code want to make it ItemCode right?
tastini 2-Oct-14 5:30am    
thanks it works , amazing. thanks really appreciate your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900