C# 使用sql查询从dataTable检索值(gridview)
在行更新事件中,我想用sql命令获取一个值,我知道我可以用e.oldvalues/e.newvalues获取它。但是我想使用sql。这就是我尝试过的:C# 使用sql查询从dataTable检索值(gridview),c#,asp.net,sql,C#,Asp.net,Sql,在行更新事件中,我想用sql命令获取一个值,我知道我可以用e.oldvalues/e.newvalues获取它。但是我想使用sql。这就是我尝试过的: SQL = "SELECT Name FROM MyTable where RowID=@RowID"; SqlDataSource1.SelectCommand = SQL; Label1.Text = SQL.ToString(); 我得到错误:必须声明标量变量“@RowID” 但是Row
SQL = "SELECT Name FROM MyTable where RowID=@RowID";
SqlDataSource1.SelectCommand = SQL;
Label1.Text = SQL.ToString();
- 我得到错误:必须声明标量变量“@RowID”
我不知道为什么不工作select语句
从MyTable中选择名称,其中RowID=@RowID
要求@RowID
从某处有一个值。您需要定义一个SQL参数来指定该值,否则SQL将不知道返回Name
字段时要查看哪个记录
@RowID=SCOPE_IDENTITY()。您必须告诉它名称/类型/值
sqlDataSource.Parameters.Add(“@RowId”,System.Data.DbType.Int,1) 在下面的示例中,我在网格中显示数据,然后允许用户异步保存或停用注释。我正在使用更新面板以异步方式激活此项
您需要同时使用rowDataBound和RowCommand事件来实现这一点
通过这种方式,您可以获得行的id和dow,无论您想对行执行什么操作,要么编辑、删除,要么像我在本例中所做的那样,更新一列
<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>
我这样做了:GridViewRow=GridView1.Rows[e.RowIndex];string SQL=“从dtaca中选择Nume,其中Nr=“+行;并且..我获取了无法绑定的多部分标识符“System.Web.UI.WebControl.GridViewRow”。
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";
}
}