2011-03-26 46 views
0

我得到一個錯誤:錯誤C#的Web應用程序我建立

The variable name '@GCSSalesPerson' has already been declared. Variable names must be unique within a query batch or stored procedure.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Configuration; 

public partial class Customer : System.Web.UI.Page 
{ 
    protected void 
Page_Load(object sender, System.EventArgs e) 
    { 
     lblErrMsg.Visible = true; 
    } 
    protected void btnInsert_Click1(object sender, EventArgs e) 
    { 
     SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["GSI2ConnectionString"].ConnectionString); 

     //Create Command object 
     SqlCommand nonqueryCommand = thisConnection.CreateCommand(); 

     try 
     { 
      // Open Connection 
      thisConnection.Open(); 

      // Create INSERT statement with named parameters 
      nonqueryCommand.CommandText = "INSERT INTO Customers 
(ChurchName,ContactName,ContactPhoneNumber,ContactCellNumber,ContactEmail, 
FaxNumber,ChurchAddress,ChurchAddress2,ChurchCity,ChurchState,ChurchZipCode, 
Notes,GCSSalesPerson) 
VALUES 
(@ChurchName,@ContactName,@ContactPhoneNumber,@ContactCellNumber,@ContactEmail, 
@FaxNumber,@ChurchAddress,@ChurchAddress2,@ChurchCity,@ChurchState,@ChurchZipCode, 
@Notes,@GCSSalesPerson)"; 


      // Add Parameters to Command Parameters collection 
      nonqueryCommand.Parameters.Add("@ChurchName", System.Data.SqlDbType.VarChar, 70); 
      nonqueryCommand.Parameters.Add("@ContactName", System.Data.SqlDbType.VarChar, 70); 
      nonqueryCommand.Parameters.Add("@ContactPhoneNumber", System.Data.SqlDbType.VarChar, 12); 
      nonqueryCommand.Parameters.Add("@ContactCellNumber", System.Data.SqlDbType.VarChar, 12); 
      nonqueryCommand.Parameters.Add("@ContactEmail", System.Data.SqlDbType.VarChar, 50); 
      nonqueryCommand.Parameters.Add("@FaxNumber", System.Data.SqlDbType.VarChar, 12); 
      nonqueryCommand.Parameters.Add("@ChurchAddress", System.Data.SqlDbType.VarChar, 70); 
      nonqueryCommand.Parameters.Add("@ChurchAddress2", System.Data.SqlDbType.VarChar, 50); 
      nonqueryCommand.Parameters.Add("@ChurchCity", System.Data.SqlDbType.VarChar, 50); 
      nonqueryCommand.Parameters.Add("@ChurchState", System.Data.SqlDbType.VarChar, 3); 
      nonqueryCommand.Parameters.Add("@ChurchZipCode", System.Data.SqlDbType.VarChar, 6); 
      nonqueryCommand.Parameters.Add("@Notes", System.Data.SqlDbType.VarChar, 7800); 
      nonqueryCommand.Parameters.Add("@ContactEmail", System.Data.SqlDbType.VarChar, 50); 
      nonqueryCommand.Parameters.Add("@GCSSalesPerson", System.Data.SqlDbType.VarChar, 50); 

      nonqueryCommand.Parameters["@ChurchName"].Value = TextBox1.Text; 
      nonqueryCommand.Parameters["@ContactName"].Value = TextBox2.Text; 
      nonqueryCommand.Parameters["@ContactPhoneNumber"].Value = TextBox3.Text; 
      nonqueryCommand.Parameters["@ContactCellNumber"].Value = TextBox4.Text; 
      nonqueryCommand.Parameters["@ContactEmail"].Value = TextBox5.Text; 
      nonqueryCommand.Parameters["@FaxNumber"].Value = TextBox6.Text; 
      nonqueryCommand.Parameters["@ChurchAddress"].Value = TextBox7.Text; 
      nonqueryCommand.Parameters["@ChurchAddress2"].Value = TextBox8.Text; 
      nonqueryCommand.Parameters["@ChurchCity"].Value = TextBox9.Text; 
      nonqueryCommand.Parameters["@ChurchState"].Value = DropDownList1.Text; 
      nonqueryCommand.Parameters["@ChurchZipCode"].Value = TextBox11.Text; 
      // nonqueryCommand.Parameters["@Notes"].Value = TextArea1.Text; 
      nonqueryCommand.Parameters["@GCSSalesPerson"].Value = DropDownList2.Text; 


      nonqueryCommand.ExecuteNonQuery(); 
     } 

     catch (SqlException ex) 
     { 
      // Display error 
      lblErrMsg.Text = ex.ToString(); 
      lblErrMsg.Visible = true; 
     } 

     finally 
     { 
      // Close Connection 
      TextBox1.Text = ""; 
      TextBox2.Text = ""; 
      TextBox3.Text = ""; 
      TextBox4.Text = ""; 
      TextBox5.Text = ""; 
      TextBox6.Text = ""; 
      TextBox7.Text = ""; 
      TextBox8.Text = ""; 
      TextBox9.Text = ""; 
      TextBox11.Text = ""; 

      thisConnection.Close(); 

     } 
    } 
} 
+0

你能提供堆棧跟蹤兩次在您的查詢,同時指定@ContactEmail。 – 2011-03-26 19:38:20

+0

存儲的proc定義如何?是否有可能將@GCSSalesPerson聲明爲參數和變量? – RQDQ 2011-03-26 20:15:04

回答

5

確定該錯誤消息不指定@ContactEmail

你加入@ContactEmail兩倍您nonqueryCommand的一部分:

nonqueryCommand.Parameters.Add("@ContactEmail", System.Data.SqlDbType.VarChar, 50); 

這將完全匹配的錯誤消息。

+0

+1。接得好。 :) – 2011-03-26 19:52:31

0

您在添加命令

nonqueryCommand.Parameters["@ContactEmail"].Value = TextBox5.Text;