SqlException过程需要参数Error

时间:2011-05-17 14:09:33

标签: c# sql-server ado.net exception-handling sqlexception

我有一个简单的表,有三个字段:

ID (int)
FormData (xml)
TimeStamp (DateTime).

我创建了一个存储过程来将值插入到成功运行的表中。然而,在我的尝试捕获中,它正在捕捉

  

System.Data.SqlClient.SqlException:过程或函数'spInsertApplication'需要参数'@formDataDecoded',它未提供。

但是,@ formDataDecoded参数正好被插入到数据库中。

有什么想法吗?我不确定从哪里开始?

这是存储过程:

ALTER PROCEDURE [dbo].[spInsertApplication]
    -- Add the parameters for the stored procedure here 
    @formDataDecoded xml,
    @timestamp DateTime
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO Applications (formXML, TimeStamp) VALUES (@formDataDecoded, @timestamp)
END

这是c#:

string con = ConfigurationManager.AppSettings["umbracoDbDSN"];

using (SqlConnection connection = new SqlConnection(con))
{
    String encodedFormData = HttpUtility.UrlDecode(formData);

    SqlCommand command = connection.CreateCommand();
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "spInsertApplication";
    command.Parameters.AddWithValue("@formDataDecoded", encodedFormData);
    command.Parameters.AddWithValue("@timestamp", DateTime.Now);

    try
    {
        connection.Open();
        command.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        String errorSql = "INSERT INTO ErrorLog(ErrorText) VALUES (@errorText)";
        SqlCommand errorCommand = new SqlCommand(errorSql, connection);
        errorCommand.Parameters.AddWithValue("@errorText", ex.ToString());
        errorCommand.ExecuteNonQuery();
        Response.Redirect("/enterprise-superstars/sounds-like-you-are-already-a-star.aspx");
    }
    finally
    {
        connection.Close();
    }
}

我正在获取这样的formData:

String formData = Request.Form["xmlToVar"]; 

我传递给saveApplicationform方法。

提前致谢。

修改

在其上运行SQL Server Profiler跟踪,结果发现sproc正在被调用两次。

事情是一个flash表单调用特定页面我将不得不让某人查看flash代码,看看它是如何发布的,因为我没有写它。

2 个答案:

答案 0 :(得分:1)

尝试明确指定参数:

command.Parameters.Add("@formDataDecoded", SqlDbType.Xml).Value = encodedFormData;
command.Parameters.Add("@timestamp", SqlDbType.DateTime).Value = DateTime.Now;

你也可以尝试:

command.CommandText = "exec dbo.spInsertApplication @formDataDecoded, @timestamp";

答案 1 :(得分:1)

问题是由于Flash表单调用了持有插入过程的页面。

它基本上发布到插入页面然后重定向到导致页面执行两次,并且在第二次执行时显然没有任何POST数据要插入到数据库中,因此SqlException

仅供参考 - Fiddler帮了很多忙!

相关问题