过程或功能';spRegistrations网格视图';期望参数'@RegistrationId

本文关键字:期望 参数 #39@RegistrationId 视图 spRegistrations 功能 过程 网格 | 更新日期: 2023-09-27 18:22:40

我使用的是GridView,并遵循了此处的说明:http://www.aspsnippets.com/Articles/GridView-CRUD-Select-Insert-Edit-Update-Delete-using-Single-Stored-Procedure-in-ASPNet.aspx

现在我得到了错误:过程或函数"spRegistrationGridView"需要参数"@RegistrationId",而没有提供该参数

这是我的存储过程:

     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();
                }
            }
        }
    }
}

过程或功能';spRegistrations网格视图';期望参数'@RegistrationId

调用存储过程时未添加参数@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,
...