2016-10-11 120 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

相關問題