![](/img/trans.png)
[英]Stored procedure on the SqlDataSource is not working with single quotation inside SelectCommand with single quotation C# asp.net
[英]ASP.NET C#: SqlDataSource with Stored Procedure and Parameters
我正在尝试使用存储过程和参数以编程方式编码 SqlDataSource。 后来我想将此 SqlDataSource 作为数据源分配给列表框。但我收到一个错误,即存储过程需要一个未提供的参数。 我不明白为什么尽管提供了它给我错误。
我使用的代码如下:
sqlDS = new SqlDataSource();
sqlDS.ConnectionString = DC.ConnectionString;
sqlDS.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
sqlDS.SelectParameters.Add("@aPara_Name", TypeCode.String, aPara_Value);
sqlDS.SelectParameters[0].Direction = ParameterDirection.Input;
sqlDS.SelectCommand = "usp_StoredProcedure_1";
sqlDS.DataBind();
this.Controls.Add(sqlDS);
Listbox1.DataSource = sqlDS;
Listbox1.DataTextField = "Title";
Listbox1.DataValueField = "Value";
Listbox1.DataBind(); //this is where I get the error saying that stored procedure requires a parameter that wasn't passed!
有人可以指导我哪里出错了吗?
你可以试试这个方法吗?
var com = new SqlConnection(DC.ConnectionString).CreateCommand();
com.CommandType = CommandType.StoredProcedure;
com.CommandText = @"usp_StoredProcedure_1";
com.Parameters.Add("@aPara_Name", SqlDbType.VarChar, 20).Value = aPara_Value;
var table = new DataTable();
new SqlDataAdapter(com).Fill(table);
Listbox1.DataSource = table;
我遇到了完全相同的问题,终于得到了答案。 这只是在“SelectParameters.Add()”方法中声明参数时不插入“@”符号的问题。 因此,您所要做的就是更改以下行:
sqlDS.SelectParameters.Add("@aPara_Name", TypeCode.String, aPara_Value);
到:
sqlDS.SelectParameters.Add("aPara_Name", TypeCode.String, aPara_Value);
希望这可以帮助。
我同意@kumbaya。 面临同样的问题。 删除@,它工作正常。
您在第 4 行的代码应编辑为
sqlDS.SelectParameters.Add("aPara_Name", TypeCode.String, aPara_Value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.