简体   繁体   English

过程需要一个未提供的参数

[英]Procedure expects a parameter which was not supplied

Towards the end of my code I am calling a stored procedure which updates a table based on the parameters passed by my page. 在我的代码结束时,我正在调用一个存储过程,该存储过程根据我的页面传递的参数更新表。 I get the following error: 我收到以下错误:

Procedure or function 'Res_invpush_UpdateInv' expects parameter '@InventoryPushSubscriptionId', which was not supplied. 过程或函数'Res_invpush_UpdateInv'需要参数'@InventoryPushSubscriptionId',它未提供。

Even thought my parameter value is being successfully passed - I know this because I have tested using breakpoints and when I mouse over on the parameter mentioned it gives the value of 1 so I don't know why the message is still coming. 甚至认为我的参数值被成功传递 - 我知道这是因为我已经使用断点进行了测试,当我将鼠标悬停在提到的参数上时,它给出了值1,所以我不知道为什么消息仍然存在。

Can somebody please show me where exactly am I going wrong or how to fix it? 有人可以告诉我哪里出错了或者如何解决?

SendInvUpdate.InvServices.UpdateRatePackagesRequest ur = new SendInvUpdate.InvServices.UpdateRatePackagesRequest();
    SendInvUpdate.InvServices.UpdateRatePackagesOperationResponse or = new SendInvUpdate.InvServices.UpdateRatePackagesOperationResponse();


    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string connStr = ConfigurationManager.ConnectionStrings["bb"].ConnectionString;
            SqlConnection Con = new SqlConnection(connStr);
            Con.Open();
            SqlCommand cmd = new SqlCommand("invpush_PollForAvailableChanges", Con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter NewSysChangeVersionParam = new SqlParameter("@NewSysChangeVersion", SqlDbType.Int);
            NewSysChangeVersionParam.Value = (object)NewSysChangeVersionParam ?? DBNull.Value;
            NewSysChangeVersionParam.Direction = ParameterDirection.InputOutput;
            NewSysChangeVersionParam.SqlDbType = SqlDbType.BigInt;
            SqlDataReader sdr = cmd.ExecuteReader();

            InventoryPushSubscriptionRecord rec = new InventoryPushSubscriptionRecord();

            while (sdr.Read())
            {

                rec.InventoryPushSubId = sdr.GetInt32(0);
                rec.CMName = sdr.GetString(1);
                rec.NotifUrl = sdr.GetString(2);
                rec.Options = sdr.GetString(3);
                rec.LastSysChangeVersion = sdr.IsDBNull(4)?(long?)null:sdr.GetInt32(4);

            }

            if(!sdr.NextResult()) throw new System.Exception("Expected Result set 1 for InventoryChangeRecord");
            InventoryChangeRecord inrec = new InventoryChangeRecord();
            while (sdr.Read())
            {
                inrec.InventoryPushSubId= sdr.GetInt32(0);
                inrec.SysChangeVersion=sdr.IsDBNull(1)?(long?)null:sdr.GetInt32(1);
                inrec.InvDate=sdr.GetDateTime(2);
                inrec.ResId=sdr.GetInt32(3);
                inrec.RoomType=sdr.GetString(4);
                inrec.InvCount=sdr.GetInt32(5);
                inrec.ResName=sdr.GetString(6);

            }

            sdr.Close();
            sdr.Dispose();

            if (NewSysChangeVersionParam != null)
            {
                SendInvUpdate.InvServices.InventoryServiceClient isc = new SendInvUpdate.InvServices.InventoryServiceClient();
                        or = isc.UpdateRatePackages(request);


                        res = or.Results.ToString();
                        int Subid;
                        SubId=inrec.InventoryPushSubscriptionId;
                        SqlCommand ucmd = new SqlCommand("Res_invpush_UpdateInv", Con);
                        ucmd.CommandType = CommandType.StoredProcedure;
                        SqlParameter LastChange = new SqlParameter("@NewLastSysChangeVersion", SqlDbType.Int);
                        LastChange.Value = NewSysChangeVersionParam;
                        SqlParameter SubscriptionId = new SqlParameter("InventoryPushSubscriptionId", SqlDbType.Int);
                        SubscriptionId.Value = SubId;
                        ucmd.ExecuteNonQuery();


                    }

                }
            }                             

        }

        catch (Exception ex)
        {
            throw (ex);
        }
    }

}

You are not setting the parameters property for command object, your code is, 您没有为命令对象设置parameters属性,您的代码是,

SqlCommand ucmd = new SqlCommand("Res_invpush_UpdateInv", Con);
                    ucmd.CommandType = CommandType.StoredProcedure;

                    SqlParameter LastChange = new SqlParameter("@NewLastSysChangeVersion", SqlDbType.Int);
                    LastChange.Value = NewSysChangeVersionParam;

                    SqlParameter SubscriptionId = new SqlParameter("InventoryPushSubscriptionId", SqlDbType.Int);

You can see that parameter are not assigned to command object's parameter property and hence stored procedure is not getting parameters. 您可以看到该参数未分配给命令对象的参数属性,因此存储过程未获取参数。 Just add the following before ExecuteNonQuery statement, 只需在ExecuteNonQuery语句之前添加以下内容,

command.Parameters.Add(LastChange);
command.Parameters.Add(SubscriptionId );

First, as Mike mentions, you should be consistent with the parameter naming. 首先,正如迈克提到的,你应该与参数命名保持一致。

eg 例如

SqlParameter SubscriptionId = new SqlParameter("InventoryPushSubscriptionId", SqlDbType.Int);

should be 应该

SqlParameter SubscriptionId = new SqlParameter("@InventoryPushSubscriptionId", SqlDbType.Int);

And then as @NSGaga points out, you are not really passing the parameters to the command, you just create the objects and aren't using them anywhere. 然后正如@NSGaga指出的那样,你并没有真正将参数传递给命令,你只是创建了对象并且没有在任何地方使用它们。

Like this: 像这样:

SqlParameter LastChange = new SqlParameter("@NewLastSysChangeVersion", SqlDbType.Int);
LastChange.Value = NewSysChangeVersionParam;
ucmd.Parameters.Add(LastChange);
SubscriptionId = new SqlParameter("@InventoryPushSubscriptionId", SqlDbType.Int);
SubscriptionId.Value = SubId;
ucmd.Parameters.Add(SubscriptionId);

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM