2012-03-04 85 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