2012-04-26 45 views
0

行更新事件我想用sql命令獲取值,我知道我可以通過e.oldvalues/e.newvalues獲取它。但我想用sql。這就是我已經試過:使用sql查詢從dataTable中檢索值(gridview)

SQL = "SELECT Name FROM MyTable where [email protected]"; 
      SqlDataSource1.SelectCommand = SQL; 
      Label1.Text = SQL.ToString(); 
  • 我得到錯誤:必須聲明標量變量「@RowID」。

但是,RowID列已經創建 - >類型int,增加1,主鍵。

我不知道爲什麼不工作

回答

1

select語句SELECT Name FROM MyTable where [email protected]要求@RowID有從某處的值。您將需要定義一個指定值的SQL參數,否則SQL將不知道要查看哪個記錄以返回Name字段。

@RowID = SCOPE_IDENTITY()會爲您提供剛剛插入到MyTable中的記錄的主鍵值,如果您處於存儲過程並且想要使用該新記錄。

+0

我這樣做:GridViewR ow row = GridView1.Rows [e.RowIndex]; string SQL =「SELECT Nume FROM dtaca where Nr =」+ row;和..我得到的多部分標識符「System.Web.UI.WebControls.GridViewRow」不能被綁定。 – 2012-04-26 17:08:03

0

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

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

0

在下面的示例中,我在網格中顯示數據,然後允許用戶異步活動或取消激活註釋。我正在使用UPDATE PANEL來異步激活它。

您需要同時使用的RowDataBound和RowCommand事件來實現這一

這樣你可以得到你想要與該行任編輯做什麼都行的id和道瓊斯指數,刪除或做一些像我我在做這個例子中,我更新一列

<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"; 

      } 
     }