当我尝试通过单击“保存”按钮将数据从Web Form-Registration上传到SQL Server时,它会引发异常。加载表格没有任何问题。
我在ASP.net Web表单中创建了一个注册,并将其上传到SQL Server。表单加载正确。
Table Schema -
CREATE TABLE [dbo].[EmployeeDetails] (
[EmployeeId] INT NOT NULL,
[FirstName] CHAR (20) NOT NULL,
[LastName] CHAR (20) NOT NULL,
[Dob] DATE NULL,
[Gender] CHAR (10) NOT NULL,
[AddLine1] CHAR (50) NOT NULL,
[AddLine2] CHAR (50) NOT NULL,
[City] CHAR (20) NOT NULL,
[State] CHAR (20) NOT NULL,
[Postal] NUMERIC (6) NOT NULL,
[Country] CHAR (20) NOT NULL,
[Mobile] NUMERIC (10) NOT NULL,
[JobType] CHAR (15) NOT NULL,
[Doj] DATE NOT NULL,
[GImage] VARBINARY (MAX) NULL,
[GaurdianType] CHAR (10) NOT NULL,
[GName] CHAR (30) NOT NULL,
PRIMARY KEY CLUSTERED ([EmployeeId] ASC)
);
在单击“保存”后输入详细信息后,它将引发异常
“ System.Data.SqlClient.SqlException:'必须声明标量变量 “ @EmployeeId”。'“位于insertCommand.ExecuteNonQuery();
namespace stagingVAM
{
public partial class Employees : System.Web.UI.Page
{
SqlConnection con = new SqlConnection (ConfigurationManager.ConnectionStrings["conStringDB"].ConnectionString);
Random rnd = new Random();
public int value = 0;
public string EmployeeId { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string Dob { get; private set; }
public Employees()
{
value = rnd.Next(100000, 999999);
}
protected void Page_Load(object sender, EventArgs e)
{
TextBox13.Text = (value).ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand insertCommand = new SqlCommand("Insert into[dbo].[EmployeeDetails] Values(@EmployeeId, @FirstName, @LastName, @Dob, @Gender, @AddLine1, @AddLine2, @City, @State, @Postal, @Country, @Mobile, @JobType, @Doj, @GImage, @GaurdianType, @GName)", con);
insertCommand.Parameters.AddWithValue(@EmployeeId, TextBox13.Text);
insertCommand.Parameters.AddWithValue(@FirstName, TextBox1.Text);
insertCommand.Parameters.AddWithValue(@LastName, TextBox2.Text);
insertCommand.Parameters.AddWithValue(@Dob, TextBox3.Text);
insertCommand.Parameters.AddWithValue(@Gender, RadioButtonList1.SelectedValue);
insertCommand.Parameters.AddWithValue(@AddLine1, TextBox4.Text);
insertCommand.Parameters.AddWithValue(@AddLine2, TextBox5.Text);
insertCommand.Parameters.AddWithValue(@City, TextBox6.Text);
insertCommand.Parameters.AddWithValue(@State, TextBox7.Text);
insertCommand.Parameters.AddWithValue(@Postal, TextBox8.Text);
insertCommand.Parameters.AddWithValue(@Country, DropDownList1.SelectedItem.Value);
insertCommand.Parameters.AddWithValue(@Mobile, TextBox9.Text);
insertCommand.Parameters.AddWithValue(@JobType, DropDownList2.SelectedItem.Value);
insertCommand.Parameters.AddWithValue(@Doj, TextBox10.Text);
insertCommand.Parameters.AddWithValue(@GImage, TextBox12.Text);
insertCommand.Parameters.AddWithValue(@GaurdianType, DropDownList3.SelectedItem.Value);
insertCommand.Parameters.AddWithValue(@GName, TextBox11.Text);
insertCommand.ExecuteNonQuery();
con.Close();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
TextBox7.Text = "";
TextBox8.Text = "";
TextBox9.Text = "";
TextBox10.Text = "";
TextBox11.Text = "";
TextBox12.Text = "";
RadioButtonList1.SelectedValue = "";
DropDownList1.SelectedItem.Value = "";
DropDownList2.SelectedItem.Value = "";
DropDownList3.SelectedItem.Value = "";
}
}
}
如何解决此问题。异常指向该行insertCommand.ExecuteNonQuery();
答案 0 :(得分:-1)
我用它来解决暂时问题及其工作
SqlCommand cmdnew = new SqlCommand(@"Insert into [dbo].[EmployeeDetails]
(EmployeeId, FirstName, LastName, Dob, Gender, AddLine1, AddLine2, City, State, Postal, Country, Mobile, JobType, Doj, GaurdianType, GName)
Values ('" + TextBox13.Text + "', '" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + RadioButtonList1.SelectedValue + "', '" + TextBox4.Text + "', '" + TextBox5.Text + "', '" + TextBox6.Text + "', '" + TextBox7.Text + "', '" + TextBox8.Text + "', '" + DropDownList1.SelectedItem.Value + "', '" + TextBox9.Text + "', '" + DropDownList2.SelectedItem.Value + "', '" + TextBox10.Text + "', '" + TextBox11.Text + "', '" + DropDownList1.SelectedItem.Value + "')", con);
con.Open();
cmdnew.ExecuteNonQuery();
con.Close();