ASP.NET C#:带有存储过程和参数的SqlDataSource [英] ASP.NET C#: SqlDataSource with Stored Procedure and Parameters

查看:70
本文介绍了ASP.NET C#:带有存储过程和参数的SqlDataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用存储过程和参数以编程方式对SqlDataSource进行编码.后来我想将此SqlDataSource分配给列表框作为数据源.但是我收到一个错误,指出存储过程需要一个未提供的参数.我不明白为什么即使提供它也会给我错误.

I am trying to have a SqlDataSource coded programmatically with stored procedure and with parameters. Later I want to assign this SqlDataSource to a listbox as a datasource.But I am getting an error that the stored procedure needs a parameter that wasn't supplied. I do not understand why its giving me the error despite supplying it.

我正在使用的代码如下:

The Code I am using is as below:

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!

有人可以指导我哪里出问题了吗?

can someone guide me where I am going wrong?

推荐答案

我同意@kumbaya.面临同样的问题.删除了@,它工作正常.

I agree with @kumbaya. Faced the same issue. Removed the @, and it worked fine.

您在第4行的代码应修改为

Your code at line 4 should be edited as

sqlDS.SelectParameters.Add("aPara_Name", TypeCode.String, aPara_Value);

这篇关于ASP.NET C#:带有存储过程和参数的SqlDataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆