簡體   English   中英

使用SQL查詢(gridview)從dataTable檢索值

[英]Retrieve value from dataTable with sql query (gridview)

在行更新事件中,我想使用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。

我不知道為什么不起作用

選擇語句SELECT Name FROM MyTable where RowID=@RowID要求@RowID具有某個位置的值。 您將需要定義一個SQL參數來指定該值,否則SQL將不知道要返回“ Name字段要查看的記錄。

@RowID = SCOPE_IDENTITY()將為您提供剛插入到MyTable的記錄的主鍵值(如果您在存儲過程中並希望使用該新記錄)。

您的代碼不知道您的參數。 您必須告訴它名稱/類型/值。

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

在下面的示例中,我在網格中顯示數據,然后允許用戶異步激活或停用評論。 我正在使用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";

            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM