2012-03-04 31 views
0

我试图在网格视图中使用GridViewCommandEventArgs,但是当我运行它时总是说:除非指定了UpdateCommand,否则数据源'SqlDataSource1'不支持更新。GridView中的OnRowCommand给出了错误

我有多个Linkbuttons在网格因此OnRowCommand

OnRowCommand =“Grid_Row”

<asp:TemplateField> 
       <ItemTemplate> 
       <asp:LinkButton ID="Update" CommandArgument='<%#Eval("Serno") %>' runat="server" OnClientClick="return confirm('Are you sure you want to Update this?');" CommandName ="Update" >Update</asp:LinkButton> 
      </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField> 
       <ItemTemplate> 
      <asp:LinkButton ID="Remove" CommandArgument='<%#Eval("Serno") %>' runat="server" OnClientClick="return confirm('Are you sure you want to Remove this Item?');" CommandName ="Remove" >Remove</asp:LinkButton> 
       </ItemTemplate> 
      </asp:TemplateField> 




    protected void Grid_Row(Object sender, GridViewCommandEventArgs e) 
{ 



    LinkButton Remove = (LinkButton)GridView1.FindControl("Remove"); 
    LinkButton Update = (LinkButton)GridView1.FindControl("Update"); 
    if (e.CommandName == "Remove") 
    { 
     try 
     { 

      int index = Convert.ToInt32(e.CommandArgument); 
      SqlConnection con = new SqlConnection(Constring); 
      con.Open(); 
      SqlCommand cmd = new SqlCommand(" update [Order_Items] set status=0 WHERE [Serno] =" + index.ToString() + "", con); 
      if (cmd.ExecuteNonQuery() > 0) 
      { 


       GridView1.DataBind(); 
       msg_lbl.Text = "Record Deleted"; 

      } 
      else 
      { 

      } 
      con.Close(); 
     } 
     catch 
     { 


     } 
    } 
    else if (e.CommandName == "Update") 
    { 
     try 
     { 


      int index = Convert.ToInt32(e.CommandArgument); 

      [Convert.ToDouble(e.CommandArgument)].FindControl("Quantity"); 

      GridViewRow clickedRow = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow; 
      string Q = ((TextBox)clickedRow.FindControl("Quantity_Txt") as TextBox).Text; 
      string P = ((Label)clickedRow.FindControl("Amount_Lbl") as Label).Text; 


      double Quantity = Convert.ToDouble(Q); 

      double Price = Convert.ToDouble(P); 
      double Calc = Quantity * Price; 


      SqlConnection con = new SqlConnection(Constring); 
      con.Open(); 
      SqlCommand cmd = new SqlCommand("update [Order_Items] set Quantity='"+Quantity +"', Money='" + Calc + "' WHERE [Serno] =" + index.ToString() + "", con); 
      if (cmd.ExecuteNonQuery() > 0) 
      { 


       GridView1.DataBind(); 
       msg_lbl.Text = "Record Updated"; 



      } 
      else 
      { 

      } 
      con.Close(); 
      } 

     catch 
     { 


     } 

    } 



} 
+0

什么是你的SQL查询和'SelectCommand',你可以发布你使用的代码? – 2012-03-04 09:30:55

回答

2

那是因为你指定的关键字为你命令他们name.change像换句话说:UPDdelete

+0

谢谢....... :) – 2012-03-04 12:07:40

1

看来,你有没有在您的数据源中指定的更新命令。

与指定select命令和选择参数的方式相同,您必须指定update命令。

希望这有助于

+0

您也可以尝试将commandName更改为与“更新”或“删除”不同的内容。这些可能是一些关键字。尝试将其设置为例如'Update1',看看它是否有效。 – Dave 2012-03-04 10:38:29