程序或功能' stored_pr'期望参数' @ name',这是未提供的

时间:2016-10-11 09:18:04

标签: sql asp.net sql-server

我尝试使用asp创建一个简单的应用程序,我遇到了问题。

  

程序或功能' stored_pr'期望参数' @ name',这是   没提供。

我的存储过程:

    using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    SqlConnection con;
    //Here we declare the parameter which we have to use in our application
    SqlCommand cmd = new SqlCommand();
    SqlParameter @name = new SqlParameter();


    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)

    {
        con = new SqlConnection("server=(local); database=**;uid=DefaultAppPool;pwd=*****");
        cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = ((TextBox)this.Page.FindControl("Pole")).Text;

        cmd = new SqlCommand("spq", con);
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

我在ASP中的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">

</head>
<body>
<form id="form1" runat="server">
<div>

<asp:TextBox ID="Pole" runat="server"></asp:TextBox>


<asp:Button ID="Button1" runat="server" Text="Submit Record" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>

files

请帮忙.. 谢谢

2 个答案:

答案 0 :(得分:1)

错误!交换这两行:

   cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = ((TextBox)this.Page.FindControl("Pole")).Text;

    cmd = new SqlCommand("spq", con);

答案 1 :(得分:1)

您的问题很可能在于这两行,尽管代码中可能还有其他问题:

cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = ((TextBox)this.Page.FindControl("Pole")).Text;

cmd = new SqlCommand("spq", con);

您基本上是在命令中添加一个参数,然后通过再次添加它来有效地清除它。交换2行,你应该克服这个错误。

查看示例文档:

MSDN: SqlCommand.Parameters Property