2012-03-04 26 views
0

グリッド表示でGridViewCommandEventArgsを使用しようとしていますが、実行時にUpdateCommandが指定されていない限り、データソース 'SqlDataSource1'で更新がサポートされていません。GridviewのOnRowCommandでエラーが発生する

私はあなたがあなたのデータソースにUpdateCommandのを指定していないようですので、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'は何ですか、使用したコードを投稿できますか? –

答えて

2

に役立ちます。

+0

ありがとう....... :) –

1

グリッド内の複数のLinkbuttonsを持っています。

selectコマンドとselectパラメータを指定するのと同じ方法で、updateコマンドを指定する必要があります。 UPDまたはdelete

希望これはあなたのような他の単語にそれらをname.changeコマンドのキーワードを指定しているためだ

+0

また、commandNameを 'Update'または 'Remove'以外のものに変更することもできます。これらはいくつかのキーワードかもしれません。たとえばに設定してみてください。 'Update1'が動作しているか確認してください。 – Dave

関連する問題