Error on C# Web App I'm building
I'm getting an error:
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,@Cont开发者_Python百科actPhoneNumber,@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();
}
}
}
Are you sure the error message doesn't specify @ContactEmail
?
You are adding @ContactEmail
twice as part of your nonqueryCommand
:
nonqueryCommand.Parameters.Add("@ContactEmail", System.Data.SqlDbType.VarChar, 50);
This would exactly match that error message.
you are specifying @ContactEmail twice in your query and also in adding commands
nonqueryCommand.Parameters["@ContactEmail"].Value = TextBox5.Text;
精彩评论