0

行更新イベントで、SQL コマンドで値を取得したいのですが、e.oldvalues/e.newvalues で取得できることがわかっています。しかし、私は sql が欲しいです。これは私が試したことです:

  SQL = "SELECT Name FROM MyTable where RowID=@RowID";
          SqlDataSource1.SelectCommand = SQL;
            Label1.Text = SQL.ToString();
  • エラーが発生しました: スカラー変数 "@RowID" を宣言する必要があります。

ただし、RowID 列は既に作成されています -> int 型、1 ずつインクリメント、主キー。

なぜ機能しないのかわかりません

4

3 に答える 3

1

select ステートメントには、どこかからの値がSELECT Name FROM MyTable where RowID=@RowID必要です。値を指定する SQL パラメータを定義する必要があります。そうしないと、SQL はフィールド@RowIDを返すためにどのレコードを参照すればよいかわかりません。Name

@RowID = SCOPE_IDENTITY()MyTableストアド プロシージャを使用していて、その新しいレコードを操作したい場合は、挿入されたばかりのレコードの主キー値が得られます。

于 2012-04-26T16:58:44.253 に答える
0

以下の例では、グリッドにデータを表示し、ユーザーがコメントを非同期的にアクティブ化または非アクティブ化できるようにしています。これを非同期的にアクティブにするためにUPDATEPANELを使用しています。

これを実現するには、rowDataBoundイベントとRowCommandイベントの両方を使用する必要があります

このようにして、行のIDを取得し、行でやりたいことを編集、削除、またはこの例で行っているようなことを行うことができます。1つの列が更新されます。

    <asp:GridView ID="gvSHowMostViewedArticles"  runat="server" AllowPaging="True" 
         AutoGenerateColumns="False"  Width="920px" BackColor="White" 
         BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
         Font-Names="Verdana" Font-Size="X-Small" ForeColor="Black" 
         GridLines="Horizontal" PageSize="10"   onrowdatabound="gvSHowMostViewedArticles_RowDataBound" 
onrowcommand="gvSHowMostViewedArticles_RowCommand" onpageindexchanging="gvSHowMostViewedArticles_PageIndexChanging">

         <Columns>
          <asp:TemplateField HeaderText="Sno">
                <ItemTemplate>
                  <%# Container.DataItemIndex + 1 %>
               </ItemTemplate>
          </asp:TemplateField>
   <asp:BoundField DataField="ArticleTitle" HeaderText="Article Title" />
   <asp:BoundField DataField="FullName" HeaderText="Name" />
   <asp:BoundField DataField="Country" HeaderText="Country" />

<asp:TemplateField HeaderText="Message">
       <ItemTemplate>
           <asp:LinkButton ID="lnkBtnShowMessage" runat="server" Text="Read" CommandName="showMessage" CommandArgument='<%# Eval("ID") %>' />
                                                </ItemTemplate>
                                         </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Activate">
                                                <ItemTemplate>
                                                    <asp:LinkButton ID="lnkBtnActivateComment" runat="server" Text="Activate" CommandName="ActivateComment" CommandArgument='<%# Eval("ID") %>' />
                                                </ItemTemplate>
                                         </asp:TemplateField>
                                        <asp:TemplateField HeaderText="De Activate">
                                                <ItemTemplate>
                                                    <asp:LinkButton ID="lnkBtnDeActivateComment" runat="server" Text="De-Activate" CommandName="DeActivateComment" CommandArgument='<%# Eval("ID") %>' />
                                                </ItemTemplate>
                                         </asp:TemplateField>

        protected void gvSHowMostViewedArticles_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //Show Message
            LinkButton lb = e.Row.FindControl("lnkBtnShowMessage") as LinkButton;
            if (lb != null)
                ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lb);


            //Activate
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lbActivate = e.Row.FindControl("lnkBtnActivateComment") as LinkButton;
                if (lbActivate != null)
                    ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lbActivate);

                lbActivate.Attributes.Add("onclick", "javascript:return " +
                "confirm('Are you sure you want to Activate this comment " +
                DataBinder.Eval(e.Row.DataItem, "ID") + "')");
            }
            //De Activate
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lbActivate = e.Row.FindControl("lnkBtnDeActivateComment") as LinkButton;
                if (lbActivate != null)
                    ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lbActivate);

                lbActivate.Attributes.Add("onclick", "javascript:return " +
                "confirm('Are you sure you want to De-Activate this comment " +
                DataBinder.Eval(e.Row.DataItem, "ID") + "')");
            }
        }


      protected void gvSHowMostViewedArticles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //Show Message
            if (e.CommandName == "showMessage")
            {
                int sno = Convert.ToInt32(e.CommandArgument);
                string strSql = "SELECT * FROM Comments WHERE comID = " + sno;
                DataSet ds = DataProvider.Connect_Select(strSql);
                lblCommentMessage.Text = ds.Tables[0].Rows[0]["comMessage"].ToString();
            }

            // Activate Comment
            if (e.CommandName == "ActivateComment")
            {
                int sno = Convert.ToInt32(e.CommandArgument);
                String strSql = "UPDATE Comments SET Visible = 1 WHERE ID = " + sno;
                DataProvider.Connect_Select(strSql);
                lblCommentMessage.Text = "Activated";
            }

            // De Activate Comment
            if (e.CommandName == "DeActivateComment")
            {
                int sno = Convert.ToInt32(e.CommandArgument);
                String strSql = "UPDATE Comments SET Visible = 0 WHERE ID = " + sno;
                DataProvider.Connect_Select(strSql);

                lblCommentMessage.Text = "Deactivate";

            }
        }
于 2012-05-02T05:27:35.230 に答える
0

あなたのコードはあなたのパラメータについて何も知りません。名前/タイプ/値を伝える必要があります。

sqlDataSource.Parameters.Add("@RowId", System.Data.DbType.Int, 1);

于 2012-04-26T17:00:51.697 に答える