【发布时间】:2011-05-17 14:09:33
【问题描述】:
我有一个包含三个字段的简单表格:
ID (int)
FormData (xml)
TimeStamp (DateTime).
我已经创建了一个存储过程来将值插入到工作成功的表中。但是,在我的 try catch 中,它正在捕获一个
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 跟踪,结果发现存储过程被调用了两次。
问题是一个 flash 表单调用特定页面,我将不得不让某人查看 flash 代码并查看它是如何发布的,因为我没有编写它。
【问题讨论】:
-
如何填充您的表格?使用
SqlDataAdapter还是什么?发布更多 C# 代码 -
能否附上调用代码?
-
您确定参数填充了格式正确的 XML 吗?如果将参数更改为
varchar(max)并在插入语句中执行convert(xml, @formdatadecoded)会怎样? -
@HansKesting 是的,xml 格式正确。
-
如果您已经在使用
using(){}块,则不需要connection.Close();
标签: c# sql-server ado.net exception-handling sqlexception