ASP.SqlDataSource与存储过程和参数

本文关键字:参数 存储过程 SqlDataSource ASP | 更新日期: 2023-09-27 18:18:57

我试图用存储过程和参数以编程方式编码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!

有人可以引导我哪里我错了?

ASP.SqlDataSource与存储过程和参数

我有完全相同的问题,最后得到了答案。这只是在"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);

你可以试试这个方法吗?

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;