C# 程序或功能';sp#u getchech';应为参数'@ID';,这是没有提供的
我有一个非常简单的存储过程,它根据用户提供的ID选择记录C# 程序或功能';sp#u getchech';应为参数'@ID';,这是没有提供的,c#,asp.net,stored-procedures,C#,Asp.net,Stored Procedures,我有一个非常简单的存储过程,它根据用户提供的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) BE
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并检查它是否存在于我们的数据库中
如果是,用户将被重定向到他/她选择的页面
若否,我们希望用户在同一屏幕上显示一条消息,说明输入的Q1254 ID号无效
该错误在存储过程中出现,我们希望C#code在屏幕上为用户显示该消息
这是代码:
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);
}
}
}
}
当我尝试运行代码时,收到以下错误消息:
过程或函数“sp_getchech”需要未提供的参数“@ID”
我肯定做错了什么
有没有办法解决这个问题
引用的线程与我的不同。该线程缺少一些参数
My没有丢失任何参数,因此,没有引用线程的副本。看起来您可能正在向存储的进程发送一个空白、null或空ID 代码检查
accountnumber.Text
是否不为null或空,但随后使用checknumber.Text
的值
当然,如果您附加一个调试器并查看发送到存储过程的值,就可以很容易地发现这一切。您永远不会告诉您的
SqlCommand
这是关于调用存储过程的。-这样做:
SqlCommand cmd = new SqlCommand("sp_getcheck", Conn);
cmd.CommandType = CommandType.StoredProcedure;
旁注:存储过程不应使用
sp
前缀。微软已经这样做了,而且你确实有可能在将来的某个时候发生名称冲突。最好只是简单地避免使用sp.
并使用其他东西作为前缀,或者根本不使用前缀!这应该有助于cmd.CommandType=CommandType.StoredProcess@阿米尔,谢谢,但马克·苏早就发现了。我只需要一个方法:1,阻止页面在ID无效时爆炸。用户应该看到的只是错误消息,而不是页面错误。2、当输入代码ID时,它不会像应该的那样重定向。有什么想法吗?可能是重复的,你们读过这些问题吗,或者你们只看标题。在发布此帖子之前,我已经检查过了。他们不一样。帐号是个打字错误。我向大家道歉。这不是我保证的理由。修正后,还是同样的错误。接球不错。愚蠢的新手错误,即使我已经做了很多次。通过这种更改,当我故意输入错误的ID时,我会收到消息,但页面会出错。第二,当我输入正确的ID时,它不是重定向