开发者

SqlException Procedure expects parameter Error

I have a simple table which has three fields:

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

I have created a stored procedure to insert values into the table which is working successfully. However, in my try catch, it is catching a

System.Data.SqlClient.SqlException: Procedure or function 'spInsertApplication' expects parameter '@formDataDecoded', which was not supplied.

However, the @formDataDecoded parameter is being inserted into the database fine.

Any ideas? I'm not sure where to go from here?

Here's the stored proc:

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

Here's the c#:

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

using (SqlConnection connection = new SqlConnection(con))
{
    String encodedFormData = HttpUtility.UrlDecode(开发者_JAVA百科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();
    }
}

And I am getting the formData like this:

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

which I pass to the saveApplicationform method.

Thanks in advance.

EDIT

Ran SQL Server Profiler trace on it and it turns out the sproc is being called twice.

The thing is a flash form calls the particular page i will have to get someone to look at the flash code and see how its being posted as I didn't write it.


Try to specify the parameters explicitly:

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

Also you can try:

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


The problem was due to the Flash Form that was calling the page which held the insert procedures.

It basically posted to the insert page and then redirected there causing the page to execute twice, and on the second execution there obviously wasn't any POST data to insert into the database, hence the SqlException

FYI - Fiddler helped a lot!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜