Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用存储过程需要@Id。请尽可能指定它_C#_Asp.net_Sql Server_Stored Procedures - Fatal编程技术网

C# 调用存储过程需要@Id。请尽可能指定它

C# 调用存储过程需要@Id。请尽可能指定它,c#,asp.net,sql-server,stored-procedures,C#,Asp.net,Sql Server,Stored Procedures,从aspx页面 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ID.aspx.cs" Inherits="_ID" %> <body> <form id="form1" runat="server"> <asp:TextBox ID="TextBoxChildID" runat="server" Enabled="false"></asp:TextBox> <

从aspx页面

<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="ID.aspx.cs" Inherits="_ID" %>

<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBoxChildID"  runat="server" Enabled="false"></asp:TextBox>
<div>
<asp:Button ID="Button1" runat="server" Text="Submit Return" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
还有讨厌的调试页面

“/”应用程序中出现服务器错误。程序或功能“WebCallID” 需要未提供的参数'@Id'。描述:安 执行当前网站时发生未处理的异常 要求请查看堆栈跟踪以了解有关堆栈的更多信息 错误及其在代码中的起源

异常详细信息:System.Data.SqlClient.SqlException:过程或 函数“WebCallID”需要未提供的参数“@Id”


在创建
SqlCommand
之后,您需要定义并添加参数——否则您将删除所有已设置的内容

// here, you're adding the parameter to "cmd" .... which exists from before
cmd.Parameters.Add("@Id", SqlDbType.BigInt);
cmd.Parameters["@Id"].Value = TextBoxChildID.Text;

// and here you're creating a **NEW** "cmd" and thus WIPING OUT all work done before!
cmd = new SqlCommand("WebCallID", con);
cmd.CommandType = CommandType.StoredProcedure;
您需要按以下顺序编写代码:

// Create the command, define a stored procedure
cmd = new SqlCommand("WebCallID", con);
cmd.CommandType = CommandType.StoredProcedure;

// **THEN** add the parameter - and don't call another "new SqlCommand()" again after this!!
cmd.Parameters.Add("@Id", SqlDbType.BigInt);
cmd.Parameters["@Id"].Value = TextBoxChildID.Text;

首先需要在
cmd=newsqlcommand(“WebCallID”,con)之后添加参数
在这里创建
SqlCommand
对象非常感谢。我真不敢相信我错过了。
// Create the command, define a stored procedure
cmd = new SqlCommand("WebCallID", con);
cmd.CommandType = CommandType.StoredProcedure;

// **THEN** add the parameter - and don't call another "new SqlCommand()" again after this!!
cmd.Parameters.Add("@Id", SqlDbType.BigInt);
cmd.Parameters["@Id"].Value = TextBoxChildID.Text;