簡體   English   中英

過程或函數“ sp_getchech”需要未提供的參數“ @ID”

[英]Procedure or function 'sp_getchech' expects parameter '@ID', which was not supplied

我有一個非常簡單的存儲過程,它根據用戶提供的ID選擇記錄。

ALTER PROCEDURE [dbo].[sp_getcheck]
@ID int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.

SET NOCOUNT ON;
If NOT EXISTS(select * from mytable where ID=@ID)
  BEGIN
    RAISERROR (
    'The ID you entered %d is invalid',
    11,
    1,
    @ID);
  END
END

我們想要做的是讓用戶輸入一個ID並檢查它是否存在於我們的數據庫中。

如果是,則將用戶重定向到他/她選擇的頁面。

如果否,我們想讓用戶在同一屏幕上看到一條消息,即輸入的ID號Q1254無效。

該錯誤在存儲過程中引發,我們希望C#代碼在屏幕上為用戶顯示該消息。

這是該代碼:

protected void chk_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(accountnumber.Text))
    {
        SqlConnection Conn = default(SqlConnection);

        //Read in connection String
        Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Constr"].ConnectionString);
        Conn.Open();

        SqlCommand cmd = new SqlCommand("sp_getcheck", Conn);
        //Retrieve the value of the output parameter

        //Add the output parameter to the command object
        cmd.Parameters.AddWithValue("@ID", checknumber.Text);
        SqlParameter outPutParameter = new SqlParameter();
        SqlDataReader dr = cmd.ExecuteReader();

        //Retrieve the value of the output parameter

        if (dr.Read())
        {
            try
            {
                //we add a hidden field called IsValid to the markup and set the IsValid value in the event handler here.
                imgstatus.ImageUrl = "images/Icon_Available.gif";
                lblStatus.ForeColor = Color.Green;

                //redirect based on value of hidden form field called evalue
                var evalue = hequipID.Value;
                if (evalue == "1")
                {
                    Response.Redirect("Air.aspx?pageId=1");
                }
                if (evalue == "2")
                {
                    Response.Redirect("Land.aspx?pageId=2");
                }

                //ID exists, get form fields and store db values in them:
                //txtfname.Text = (dr["FullName"].ToString());

            }
            catch (SqlException ex)
            {
                //Something is wrong with this taxpayer's account.
                imgstatus.ImageUrl = "images/NotAvailable.jpg";
                lblStatus.ForeColor = Color.Red;
                lblStatus.Text = ex.Message;
                chk.BackColor = Color.Silver;
                System.Threading.Thread.Sleep(2000);
            }
        }
    }
  }

當我嘗試運行代碼時,收到以下錯誤消息:

Procedure or function 'sp_getchech' expects parameter '@ID', which was not supplied

我當然做錯了。

任何想法如何解決這個問題?

引用的線程與我的不同。 該線程缺少一些參數。

我沒有丟失任何參數,因此,不是引用線程的重復項。

看來您可能會將空白,空值或空ID發送到存儲的proc。

您的代碼檢查accountnumber.Text是否為null或為空,但是您使用checknumber.Text的值。

當然,如果您連接調試器並查看要發送到存儲過程的值,則可以很容易地發現所有這些。

您永遠不會告訴SqlCommand這是關於調用存儲過程的 -這樣做:

SqlCommand cmd = new SqlCommand("sp_getcheck", Conn);
cmd.CommandType = CommandType.StoredProcedure;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM