문제

I realize there are quite a few others having posted similar questions, but I can't seem to find one quite on point.

I have a simple stored procedure performing an insert:

ALTER PROCEDURE [dbo].[usp_CategoryTreeInsert]
   @CategoryId Int,
   @InvenexNodeId Int,
   @ParentNodeId Int,
   @NodeId Int,
   @Node nvarchar(max),
   @NodeQuery nvarchar(max)
AS
BEGIN
BEGIN TRANSACTION

    INSERT INTO dbo.CategoryTree(
                CategoryId,
                InvenexNodeId,
                ParentNodeId,
                NodeId,
                Node,
                NodeQuery
                )VALUES(
                @CategoryId,
                @InvenexNodeId,
                @ParentNodeId,
                @NodeId,
                @Node,
                @NodeQuery
                )

                IF @@ERROR <> 0
                    BEGIN
                        ROLLBACK TRANSACTION
                        Return -1
                    END
                ELSE
                BEGIN
                    COMMIT TRANSACTION
                    RETURN 1
                END
END

I'm calling this procedure out of a class library using the Enterprise Data Access Application Block as follows.

(Note: I'd ordinarily use a SqlParameter[] rather than this method, but this was supposed to be a "quick and dirty")

SqlCommand Cmd = new SqlCommand("usp_CategoryTreeInsert");
Cmd.CommandType = CommandType.StoredProcedure;

Cmd.Parameters.Add("@CategoryId",SqlDbType.Int, Convert.ToInt32(Dt.Rows[CurrentRow]["CategoryId"]));
Cmd.Parameters.Add("@InvenexNodeId",SqlDbType.Int, Convert.ToInt32(Dt.Rows[CurrentRow]["InvenexNodeId"]));
Cmd.Parameters.Add("@ParentNodeId",SqlDbType.Int, Convert.ToInt32(Dt.Rows[CurrentRow]["ParentNodeId"]));
Cmd.Parameters.Add("@NodeId",SqlDbType.Int, Convert.ToInt32(Dt.Rows[CurrentRow]["NodeId"]));
Cmd.Parameters.AddWithValue("@Node", Dt.Rows[CurrentRow]["Node"]);
Cmd.Parameters.AddWithValue("@NodeQuery", Dt.Rows[CurrentRow]["NodeQuery"]);

Database Db = DatabaseFactory.CreateDatabase("DefaultConnection");
Db.ExecuteNonQuery(Cmd);

The error being returned is

Procedure or Function Expects Parameter "@CategoryId" Which Was Not Supplied

I've got a watch on the SqlCommand "Cmd", as well as all the parameter values, which appear to be properly named, typed and populated.

I'm figuring this has got to be something relatively simple that a fresh eye could point out quickly.

Can anybody help?

Thanks so much in advance!

EDIT: Solution

As Sriram noted, I had placed the intended parameter value improperly in the overload.

The corrected code is as follows:

            SqlCommand Cmd = new SqlCommand("usp_CategoryTreeInsert");
            Cmd.CommandType = CommandType.StoredProcedure;

            Cmd.Parameters.Add("@CategoryId",SqlDbType.Int);
            Cmd.Parameters["@CategoryId"].Value = Convert.ToInt32(Dt.Rows[CurrentRow]["CategoryId"]);
            Cmd.Parameters.Add("@InvenexNodeId",SqlDbType.Int);
            Cmd.Parameters["@InvenexNodeId"].Value = Convert.ToInt32(Dt.Rows[CurrentRow]["InvenexNodeId"]);
            Cmd.Parameters.Add("@ParentNodeId",SqlDbType.Int);
            Cmd.Parameters["@ParentNodeId"].Value = Convert.ToInt32(Dt.Rows[CurrentRow]["ParentNodeId"]);
            Cmd.Parameters.Add("@NodeId",SqlDbType.Int);
            Cmd.Parameters["@NodeId"].Value = Convert.ToInt32(Dt.Rows[CurrentRow]["NodeId"]);
            Cmd.Parameters.AddWithValue("@Node", Dt.Rows[CurrentRow]["Node"]);
            Cmd.Parameters.AddWithValue("@NodeQuery", Dt.Rows[CurrentRow]["NodeQuery"]);

            Database Db = DatabaseFactory.CreateDatabase("DefaultConnection");
            Db.ExecuteNonQuery(Cmd);

Thanks so much for the insanely quick reply Sriram!

도움이 되었습니까?

해결책

Cmd.Parameters.Add("@CategoryId",SqlDbType.Int, Convert.ToInt32(Dt.Rows[CurrentRow]["CategoryId"]));

In above line you're using the following overload of add

public SqlParameter Add(string parameterName, SqlDbType sqlDbType, int size);

as you can see you're passing CategoryId in size parameter and hence this fails.

You need something like this

var param = new SqlParameter("@CategoryId",SqlDbType.Int);
param.Value = Dt.Rows[CurrentRow]["CategoryId"];
Cmd.Parameters.Add(param);

Also you're missing same thing in InvenexNodeId,ParentNodeId,NodeId parameters as well.

다른 팁

You can use Parameters.AddWithValue method

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top