如何在 Asp.net Web 表单应用程序的代码隐藏中设置 SelectParameter?
How to set SelectParameter in code behind in Asp.net web forms application?
我一直收到错误消息,尝试使用以下内容时必须声明标量变量“@inspIdFk”:
protected void rgInspections_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridDataItem item in rgInspections.SelectedItems)
{
hdn_insp_id_pk.Value = item["inspIdPk"].Text;
}
string inspidvalue = hdn_insp_id_pk.Value;
if (HttpContext.Current.User.IsInRole("AKOB") || (HttpContext.Current.User.IsInRole("aMgr")))
{
SqlDataSource sdcRgActExps2 = new SqlDataSource();
sdcRgActExps2.ID = "sdcRgActExps2";
this.Page.Controls.Add(sdcRgActExps2);
sdcRgActExps2.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
sdcRgActExps2.SelectCommand = "SELECT ae.actExpIdPk, ae.inspIdFk, ae.actExpDt, aet.actExpType, ae.actExpQty, ae.actExpRate, ae.actExpBill, (ae.actExpQty * ae.actExpRate) AS subTotal FROM inspActExps ae LEFT JOIN actExpTypes aet ON ae.actExpType = aet.actExpTypeIdPk WHERE ([inspIdFk] = @inspIdFk) AND actExpSecCd = 1 ORDER BY actExpDt";
sdcRgActExps2.SelectParameters.Add("@inspIdFk", inspidvalue);
rgActExps2.DataSource = sdcRgActExps2;
rgActExps2.DataBind();
}
else
{
...
}
}
如何成功设置@inspIdFk的值?我通过从 WHERE 子句中删除 inspIdFk = @inspIdFk 来验证查询确实提取了数据。
据我了解,SqlDataSource.SelectParameters.Add 采用参数名称 不带 符号,因此您的添加应如下所示:
sdcRgActExps2.SelectParameters.Add("inspIdFk", inspidvalue);
我一直收到错误消息,尝试使用以下内容时必须声明标量变量“@inspIdFk”:
protected void rgInspections_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridDataItem item in rgInspections.SelectedItems)
{
hdn_insp_id_pk.Value = item["inspIdPk"].Text;
}
string inspidvalue = hdn_insp_id_pk.Value;
if (HttpContext.Current.User.IsInRole("AKOB") || (HttpContext.Current.User.IsInRole("aMgr")))
{
SqlDataSource sdcRgActExps2 = new SqlDataSource();
sdcRgActExps2.ID = "sdcRgActExps2";
this.Page.Controls.Add(sdcRgActExps2);
sdcRgActExps2.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
sdcRgActExps2.SelectCommand = "SELECT ae.actExpIdPk, ae.inspIdFk, ae.actExpDt, aet.actExpType, ae.actExpQty, ae.actExpRate, ae.actExpBill, (ae.actExpQty * ae.actExpRate) AS subTotal FROM inspActExps ae LEFT JOIN actExpTypes aet ON ae.actExpType = aet.actExpTypeIdPk WHERE ([inspIdFk] = @inspIdFk) AND actExpSecCd = 1 ORDER BY actExpDt";
sdcRgActExps2.SelectParameters.Add("@inspIdFk", inspidvalue);
rgActExps2.DataSource = sdcRgActExps2;
rgActExps2.DataBind();
}
else
{
...
}
}
如何成功设置@inspIdFk的值?我通过从 WHERE 子句中删除 inspIdFk = @inspIdFk 来验证查询确实提取了数据。
据我了解,SqlDataSource.SelectParameters.Add 采用参数名称 不带 符号,因此您的添加应如下所示:
sdcRgActExps2.SelectParameters.Add("inspIdFk", inspidvalue);