2016-10-11 123 views
1

我试图用asp制作一个simpe应用程序,我有一个问题。过程或函数'stored_pr'期望参数'@name',这是没有提供

过程或函数'stored_pr'需要参数'@name',它是 未提供的。

我的存储过程:

ALTER proc [dbo].[spq] 
@name nvarchar(max) 
as 
insert into tableq (name) 
Values 
(@name) 
GO 

我的代码在ASP:

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(); 
    } 
} 

<%@ 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> 

请任何帮助.. 谢谢

+0

作为参数的类型是'nvarchar'的,你可能需要SqlDbType.NVarChar'正确设置它的值。 –

回答

1

您的问题,更可能在于这两条线,但有可能在代码中的其他问题:

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

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

你基本上添加一个参数的命令然后再通过新的方式清除它。交换两行,你应该通过这个错误。

看看对样品的文档:

MSDN: SqlCommand.Parameters Property

1

错误!互换这两行:

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

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

非常感谢你! – taty

相关问题