我正在尝试使用存储过程和参数以编程方式编码 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!
有人可以指导我哪里出错了吗?
我遇到了完全相同的问题,终于得到了答案。 只需在“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;