I have a function which will get the records from the database.
我有一个从数据库中获取记录的函数。
public List<Issue> Load_Issues()
{
SqlDataReader Sdr;
List<Issue> ObjList = new List<Issue>();
cmd.CommandText = "Get_All_Issue";
try
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
Sdr = cmd.ExecuteReader();
while (Sdr.Read())
{
// here I pull out records from database..
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return ObjList;
}
The function I am using to bind the Gridview is as follows
我用于绑定Gridview的函数如下所示
public void Bind_Issues()
{
gdIssues.DataSource = Bl.Load_Issues()();
gdIssues.DataBind();
}
My stored procedure doesn't take any arguments. While the page loads for the first time it is working fine and binding the records to the gridview.
我的存储过程不接受任何参数。当页面首次加载时,它运行良好,并将记录绑定到gridview。
We have option to edit the records also, so what happening is after updating records I need to again bind the records to gridview. So I am again using my Load_Issues
function to do it. But this time it is throwing error
我们也可以编辑记录,所以在更新记录之后,我需要再次将记录绑定到gridview。我再次使用Load_Issues函数来完成它。但这一次它犯了错误
Get_All_Issues has no parameters and arguments were supplied
Get_All_Issues没有提供参数和参数
3 个解决方案
#1
2
You're most probably re-using the cmd
instance in multiple places and you don't clear the parameters associated with it, thus creating the exception you're seeing.
您很可能在多个地方重用cmd实例,并且不清除与它相关的参数,从而创建您正在看到的异常。
Easiest fix would be to not re-use cmd
, but if for whatever reason it's better for you, just make sure you use Clear on parameters before you execute it.
最简单的解决方案是不重复使用cmd,但是如果出于某种原因对您更有利,那么在执行它之前,请确保在参数上使用了Clear。
cmd.Parameters.Clear();
#2
2
Try not using global connections, commands etc: open and close them within the method
尝试不使用全局连接、命令等:在方法中打开并关闭它们。
public List<Issue> Load_Issues() {
//TODO: Put actual connection string here
using (SqlConnection con = new SqlConnection("Connection String here")) {
con.Open();
// Put IDisposable into using
using (SqlCommand cmd = new SqlCommand()) {
cmd.Connection = con;
cmd.CommandText = "Get_All_Issue";
cmd.CommandType = CommandType.StoredProcedure;
List<Issue> ObjList = new List<Issue>();
// Put IDisposable into using
using (var Sdr = cmd.ExecuteReader()) {
while (Sdr.Read()) {
//TODO: Pull out records from database into ObjList
}
}
return ObjList;
}
}
}
#3
1
Try these
试试这些
exec 'stored_procedure_name'
go
or
或
alter proc stored_procedure_name
as
begin
--Block of Statements
end
go
or
或
create proc stored_procedure_name
as
begin
--Block of Statements
end
go
Where go keyword will solved your problem.
go Where关键字将解决你的问题。
#1
2
You're most probably re-using the cmd
instance in multiple places and you don't clear the parameters associated with it, thus creating the exception you're seeing.
您很可能在多个地方重用cmd实例,并且不清除与它相关的参数,从而创建您正在看到的异常。
Easiest fix would be to not re-use cmd
, but if for whatever reason it's better for you, just make sure you use Clear on parameters before you execute it.
最简单的解决方案是不重复使用cmd,但是如果出于某种原因对您更有利,那么在执行它之前,请确保在参数上使用了Clear。
cmd.Parameters.Clear();
#2
2
Try not using global connections, commands etc: open and close them within the method
尝试不使用全局连接、命令等:在方法中打开并关闭它们。
public List<Issue> Load_Issues() {
//TODO: Put actual connection string here
using (SqlConnection con = new SqlConnection("Connection String here")) {
con.Open();
// Put IDisposable into using
using (SqlCommand cmd = new SqlCommand()) {
cmd.Connection = con;
cmd.CommandText = "Get_All_Issue";
cmd.CommandType = CommandType.StoredProcedure;
List<Issue> ObjList = new List<Issue>();
// Put IDisposable into using
using (var Sdr = cmd.ExecuteReader()) {
while (Sdr.Read()) {
//TODO: Pull out records from database into ObjList
}
}
return ObjList;
}
}
}
#3
1
Try these
试试这些
exec 'stored_procedure_name'
go
or
或
alter proc stored_procedure_name
as
begin
--Block of Statements
end
go
or
或
create proc stored_procedure_name
as
begin
--Block of Statements
end
go
Where go keyword will solved your problem.
go Where关键字将解决你的问题。