c# - 必须声明标量变量 "@LoggedInUser"。尝试向 SqlDataSource 添加参数时出错

标签 c# sqldatasource

我正在使用 SqlDataSource 来显示基于登录用户的记录,

<asp:SqlDataSource ID="ModifyCustomerDataSource" SelectCommand="SELECT cApplicationNo,cFirstName,cMiddleName,cLastName,nTelNo,nMobileNo,cPanGirNo from Data_Customer_Log where cAddedBy=@LoggedInUser" ConnectionString="<%$ ConnectionStrings:CwizDataConnectionString %>"   runat="server"></asp:SqlDataSource>

在页面加载中我添加了如下参数

protected void Page_Load(object sender, EventArgs e)
        {
            string user = HttpContext.Current.User.Identity.Name;
            ModifyCustomerDataSource.SelectParameters.Clear();
            ModifyCustomerDataSource.SelectParameters.Add("@LoggedInUser", user.Trim());
        }

但它给我错误

Must declare the scalar variable "@LoggedInUser".
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@LoggedInUser".

任何人都可以指出我哪里出错了。 谢谢

最佳答案

添加参数时不需要@ eg:

ModifyCustomerDataSource.SelectParameters.Add("LoggedInUser", user.Trim());

查询中的@ 符号表示后面的文本是参数名称,但是@ 不被视为参数名称的一部分。

关于c# - 必须声明标量变量 "@LoggedInUser"。尝试向 SqlDataSource 添加参数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10633065/

相关文章:

c# - Javascript函数不会立即运行

c# - 尝试访问 .ashx 文件时出现 500 Internal Server Error

c# - SqlDataSource 中的动态 WHERE 子句

c# - Entity Framework 按类更改数据库物理位置

C# - 要关闭 NetworkStream,请调用 stream.Close 或 socket.Shutdown?

C# 为什么要使用 SuspendLayout()?

c# - ASP :SqlDataSource SelectCommand property does not persist when paging

ms-access-2016 - 错误您尝试打开的数据库需要较新版本的 Microsoft Access

c# - 我可以获取从 SqlDataSource 执行的查询吗?

asp.net - 在SQLDatasource中使用存储过程时,Gridview不显示