asp.net标量变量出错

时间:2011-07-06 22:00:24

标签: c# asp.net sql

如果你能帮助我,我有一个错误,我正在努力了解错误是什么,所以我可以解决它,因为我是一个初学者。

我的错误是:

Must declare the scalar variable "@Details"

我的功能是这样的:

public static void CreateReview(string paperId, string grate, string criteriaId, string Details)
{
    var sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

             // GET CONFERENCE ROLE ID
             SqlCommand cmd = new SqlCommand();
             cmd.Connection = sqlCon;
             cmd.CommandText = "select Conference_Role_ID from AuthorPaper where Paper_ID = @PaperId";
             cmd.Parameters.AddWithValue("@PaperId", paperId);
             cmd.Connection.Open();
             string ConferenceRoleId = cmd.ExecuteScalar().ToString();
             cmd.Connection.Close();
             cmd.Dispose();

             string query2 = @"insert into ReviewPaper(Overall_Rating,Paper_id,Conference_role_id,Details)
             values(0,@paperId,@ConferenceRoleId,@Details); select
 SCOPE_IDENTITY() as RPID";

             cmd = new SqlCommand(query2, sqlCon);
                 cmd.Parameters.AddWithValue("@paperId",
 paperId);
                 cmd.Parameters.AddWithValue("@ConferenceRoleId",
 ConferenceRoleId);

             string ReviewPaperId;

                 try
                 {
                     cmd.Connection.Open();
                     ReviewPaperId = cmd.ExecuteScalar().ToString();
                     cmd.Connection.Close();
                 }
                 catch (Exception ee) { throw ee; }
                 finally { cmd.Dispose(); } }

如果我删除细节我的功能完美。提前感谢你。

2 个答案:

答案 0 :(得分:1)

你需要一条线:

cmd.Parameters.AddWithValue("@Details", Details);

您正在执行带有3个参数的语句,但您只提供两个参数。

答案 1 :(得分:1)

您需要在代表@Details

的SqlCommand中添加一个新参数
cmd = new SqlCommand(query2, sqlCon);
cmd.Parameters.AddWithValue("@paperId",paperId);
cmd.Parameters.AddWithValue("@ConferenceRoleId", ConferenceRoleId);

// Add the details parameter
cmd.Parameters.AddWithValue("@Details", Details);