我使用的是 GridView,我按照此处的说明进行操作:http://www.aspsnippets.com/Articles/GridView-CRUD-Select-Insert-Edit-Update-Delete-using-Single-Stored-Procedure-in-ASPNet.aspx
现在我收到错误:过程或函数“spRegistrantsGridView”需要参数“@RegistrantId”,但未提供该参数
这是我的存储过程:
ALTER PROCEDURE [dbo].[spRegistrantsGridView]
@Action nvarchar(10),
@RegistrantId int,
@FirstName nvarchar(20),
@LastName nvarchar(25),
@AddressLine1 nvarchar(50),
@AddressLine2 nvarchar(50),
@City nvarchar(30),
@State nvarchar(2),
@Zip nvarchar(10),
@Country nvarchar(20),
@Phone nvarchar(15),
@PhoneExt nvarchar(4),
@Email nvarchar(50),
@MemberId bigint,
@Comments nvarchar(300)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
--SELECT
IF @Action = 'SELECT'
BEGIN
SELECT RegistrantId,
FirstName,
LastName,
AddressLine1,
AddressLine2,
City,
State,
Zip,
Country,
Phone,
PhoneExt,
Email,
Comments
FROM Registrant
END
--INSERT
IF @Action = 'INSERT'
BEGIN
INSERT INTO Registrant(
FirstName,
LastName,
AddressLine1,
AddressLine2,
City,
State,
Zip,
Country,
Phone,
PhoneExt,
Email,
MemberId,
Comments)
VALUES (
@FirstName,
@LastName,
@AddressLine1,
@AddressLine2,
@City,
@State,
@Zip,
@Country,
@Phone,
@PhoneExt,
@Email,
@MemberId,
@Comments)
END
--UPDATE
IF @Action = 'UPDATE'
BEGIN
UPDATE Registrant SET
FirstName = @FirstName,
LastName = @LastName,
AddressLine1 = @AddressLine1,
AddressLine2 = @AddressLine2,
City = @City,
State = @State,
Zip = @Zip,
Country = @Country,
Phone = @Phone,
PhoneExt = @PhoneExt,
Email = @Email,
MemberId = @MemberId,
Comments = @Comments
WHERE RegistrantId = @RegistrantId
END
--DELETE
IF @Action = 'DELETE'
BEGIN
DELETE FROM Registrant
WHERE RegistrantId = @RegistrantId
END
END
以及我的 C# 中抛出错误的部分(特别是在 sda.Fill(dt);
):
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["Events2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("spRegistrantsGridView"))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "SELECT");
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
最佳答案
调用存储过程时没有添加参数@RegistrantId
像这样将参数添加到您的代码中:
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["Events2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("spRegistrantsGridView"))
{
cmd.CommandType = CommandType.StoredProcedure;
// missing parameter
cmd.Parameters.AddWithValue("@RegistrantId", [insert id]);
cmd.Parameters.AddWithValue("@Action", "SELECT");
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
编辑
现在您的 SP 在您的问题中,问题是您指定了多个参数,但您只在 c# 代码中添加了一个。从您的 SP 中删除参数或通过添加 = null
例如
ALTER PROCEDURE [dbo].[spRegistrantsGridView]
@Action nvarchar(10),
@RegistrantId int = null,
@FirstName nvarchar(20) = null,
...
关于c# - 过程或函数 'spRegistrantsGridView' 需要参数 '@RegistrantId',但未提供,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33784788/