变量名称' @ OrderNo'已经宣布。变量名在查询批处理或存储过程中必须是唯一的

时间:2017-06-28 06:45:09

标签: c# sql

变量名称' @ OrderNo'已经宣布。变量名在查询批处理或存储过程中必须是唯一的。

每当我执行下面的代码时,第二个@OrderNo上的sql参数出现错误

 protected void lvProjects_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    Literal ltOrderNo = (Literal)e.Item.FindControl("ltOrderNo");

    if (e.CommandName == "startproj") 
    {

        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "UPDATE Project " +
            "SET Status='On Going' , StartDate=@StartDate WHERE OrderNo=@OrderNo";
        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@OrderNo", ltOrderNo.Text);
        cmd.Parameters.AddWithValue("@StartDate", DateTime.Now);
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Redirect("Default.aspx");
    }

    if (e.CommandName == "endproj")
    {
        #region

        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "UPDATE Project " +
            "SET Status='Finished' , EndDate=@EndDate WHERE OrderNo=@OrderNo";
        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@OrderNo", ltOrderNo.Text);
        cmd.Parameters.AddWithValue("@EndDate", DateTime.Now);
        cmd.ExecuteNonQuery();
        con.Close();
        #endregion

        #region
        con.Open();
        cmd.CommandText = "INSERT INTO Deliveries VALUES (@OrderNo, @Status)";
        cmd.Parameters.AddWithValue("@OrderNo", ltOrderNo.Text);
        cmd.Parameters.AddWithValue("@Status", "Pending");
        cmd.ExecuteNonQuery();
        con.Close();
        #endregion
    }
}

2 个答案:

答案 0 :(得分:0)

尝试创建第二个if (e.CommandName == "endproj") { #region con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "UPDATE Project " + "SET Status='Finished' , EndDate=@EndDate WHERE OrderNo=@OrderNo"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@OrderNo", ltOrderNo.Text); cmd.Parameters.AddWithValue("@EndDate", DateTime.Now); cmd.ExecuteNonQuery(); cmd.Dispose(); con.Close(); #endregion #region con.Open(); SqlCommand cmd1 = new SqlCommand(); cmd1.CommandText = "INSERT INTO Deliveries VALUES (@OrderNo, @Status)"; cmd1.Parameters.AddWithValue("@OrderNo", ltOrderNo.Text); cmd1.Parameters.AddWithValue("@Status", "Pending"); cmd1.ExecuteNonQuery(); cmd1.Dispose(); con.Close(); #endregion } 作为新的<b:if cond='data:blog.pageType == &quot;index&quot;'> <b:if cond='data:post.thumbnailUrl'> <div class='block-image'><div class='thumb'> &lt;a href=&#39;<data:post.url/>&#39; style=&#39;background:url(<data:post.thumbnailUrl/>) no-repeat center center;background-size:cover&#39;&gt;&lt;/a&gt; </div> </div> <b:else/> <b:if cond='data:post.firstImageUrl'> <div class='block-image'><div class='thumb'> &lt;a href=&#39;<data:post.url/>&#39; style=&#39;background:url(<data:post.firstImageUrl/>) no-repeat center center;background-size:cover&#39;&gt;&lt;/a&gt; </div> </div> <b:else/> <div class='block-image'><div class='thumb'> &lt;a href=&#39;<data:post.url/>&#39; style=&#39;background:url(http://1.bp.blogspot.com/-eAeO-DYJDws/Vkqtj4HFBFI/AAAAAAAAB0o/Q5OLsyONXM0/s1600-r/nth.png) no-repeat center center;background-size:cover&#39;&gt;&lt;/a&gt; </div> </div> </b:if></b:if> </b:if> <b:if cond='data:blog.pageType == &quot;archive&quot;'> <b:if cond='data:post.thumbnailUrl'> <div class='block-image'><div class='thumb'> &lt;a href=&#39;<data:post.url/>&#39; style=&#39;background:url(<data:post.thumbnailUrl/>) no-repeat center center;background-size:cover&#39;&gt;&lt;/a&gt; </div> </div> <b:else/> <b:if cond='data:post.firstImageUrl'> <div class='block-image'><div class='thumb'> &lt;a href=&#39;<data:post.url/>&#39; style=&#39;background:url(<data:post.firstImageUrl/>) no-repeat center center;background-size:cover&#39;&gt;&lt;/a&gt; </div> </div> <b:else/> <div class='block-image'><div class='thumb'> &lt;a href=&#39;<data:post.url/>&#39; style=&#39;background:url(http://1.bp.blogspot.com/-eAeO-DYJDws/Vkqtj4HFBFI/AAAAAAAAB0o/Q5OLsyONXM0/s1600-r/nth.png) no-repeat center center;background-size:cover&#39;&gt;&lt;/a&gt; </div> </div> </b:if></b:if> </b:if> 而不是尝试使用第一个。{/ p>

cordova platform update ios

答案 1 :(得分:0)

您正在重用{{1}} SqlCommand对象,在第二个if语句中,您添加了两次名为OrderNo的参数。在运行命令后通过clearing out the parameters解决此问题(或者更好:为第二个查询创建一个新的SqlCommand)。

{{1}}