如何使用参数和存储过程

时间:2012-11-07 19:48:24

标签: asp.net c#-4.0

我在这里尝试做的就是使用存储过程将Status字段设置为“Complete”,但由于某种原因,在运行存储过程后它不会更新我的表。有人可以帮我在这里,告诉我我做错了什么?谢谢 //这里是存储过程

CREATE PROCEDURE sp_Update 
  @ID varchar 

AS 
BEGIN 
 -- SET NOCOUNT ON added to prevent extra result sets from 
 -- interfering with SELECT statements. 
 SET NOCOUNT ON; 

 if exists (select Post_ID from  MyTable  WHERE Post_ID = @ID) 
  BEGIN 
        UPDATE MyTable 
        SET Status = 'Complete' 
        WHERE Post_ID = @ID 
  END 

END 

//and here is the code behind 
foreach (GridViewRow gr in GV_Action.Rows) 
        { 

                //string strID = gr.Cells[1].Text; 
                string ID = gr.Cells[1].Text; 
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString); 

                SqlCommand cmd = new SqlCommand("sp_Update", con); 
                cmd.CommandType = CommandType.StoredProcedure; 

                cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID; 

                cmd.Connection = con; 
                con.Open(); 
                cmd.ExecuteNonQuery(); 
                con.Close(); 

        }

1 个答案:

答案 0 :(得分:0)

在代码“gr.Cells [1] .Text”中,Cells [x]基于零。如果ID在第一列,则需要'gr.Cells [0] .Text'。在下一行放置一个断点,看看你有什么价值。