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要求@RowID从某处获得一个值。您将需要定义一个指定值的 SQL 参数,否则 SQL 将不知道要查看哪个记录来返回该Name字段。

@RowID = SCOPE_IDENTITY()MyTable如果您在存储过程中并且想要使用该新记录,则会为您提供刚刚插入的记录的主键值。

于 2012-04-26T16:58:44.253 回答
0

在下面的示例中,我在网格中显示数据,然后允许用户异步激活或停用评论。我正在使用 UPDATE PANEL 来异步激活它。

您需要同时使用 rowDataBound 和 RowCommand 事件来实现这一点

通过这种方式,您可以获得行的 id 并查看您想对该行执行的操作,无论是编辑、删除还是像我在此示例中所做的那样,我更新了一个 COLUMN

    <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 回答