我想将参数从asp.net传递到我的存储过程。这些参数将是从下拉列表中选择的值,然后从存储过程中获取数据,并在用户单击网页上的“提交”按钮时显示在asp.net的Gridview中。当我单击提交按钮时,GridView不会弹出。我现在坚持了一段时间。当我尝试调试它时,Fill方法弹出一个异常,提示我需要传递参数。异常复制到下面。
System.Data.SqlClient.SqlException:'过程或函数'Submit'需要未提供的参数'@Address'。'
下面是我的存储过程
USE [Database]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [Submit]
@Address varchar(12),
@Number varchar(5),
@Name varchar(24),
@Type varchar(3)
as
begin
select a.*, b.*
from Employee as a
inner join Department as b
on a.Number = b.Number
where a.Address = @Address and a.Number = @Number and a.name = @Name and b.Type = @Type
end
下面是我的.cs代码
protected void Button1_Click1(object sender, EventArgs e)
{
con.Open();
string s = "Submit";
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Submit";
cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = DropDownList5.SelectedItem.Value;
cmd.Parameters.Add("@Number", SqlDbType.VarChar).Value = DropDownList2.SelectedItem.Value;
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = DropDownList3.SelectedItem.Value;
cmd.Parameters.Add("@Type", SqlDbType.VarChar).Value = DropDownList4.SelectedItem.Value;
SqlDataAdapter da = new SqlDataAdapter(s, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
答案 0 :(得分:0)
您仅使用字符串s
而不是SqlCommand cmd
创建DataAdapter:
SqlDataAdapter da = new SqlDataAdapter(s, con);
答案 1 :(得分:0)
尝试一下
DataTable dt = new DataTable();
using(var reader = cmd.ExecuteReader()) {
dt.Load(reader);
}
GridView1.DataSource=dt;
GridView1.Databind():