使用asp.net的存储过程更新多行 [英] update multiple rows using stored procedure with asp.net

查看:67
本文介绍了使用asp.net的存储过程更新多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名为purchasedetails和itemmaster的表,我想要做的是,一旦按下提交按钮需要更新itemmaster表。对于这些我使用存储过程。



这是我写的存储过程。它的工作量为一排。在我的网页上使用gridview。如何解决,请帮助我。





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





这是我的asp代码。



试试

{



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)



{

尝试

{

strSql =EXEC UpdateItem'+ PID +' ;

返回objDB.ExecuteNonQuerySQL(strSql);

}

catch(Exception ex)`在这里输入代码`

{

objDB.LogError(ex.ToString());

返回false;

}

}



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



它没有给出错误或者没有给出任何结果...

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...

推荐答案

你需要在循环中进行更新,然后用项目代码实际执行更新,来自电网的数量/价格。喜欢:



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:

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:

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 :)


这篇关于使用asp.net的存储过程更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆