Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

c# - An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Incorrect syntax near the keyword 'WHERE'.

I dont know where is the wrong in the syntax !!

using( var command1 = new SqlCommand("INSERT INTO Employee(EmpPhone,Password,OfficeNo,Floor, Building) VALUES (@EmpPhone,@Password,@OfficeNo,@Floor, @Building) WHERE EmpID ='" + id.Text + "'", con))
{
      command1.Parameters.AddWithValue("@EmpPhone", Convert.ToInt32(phone.Text));
      command1.Parameters.AddWithValue("@Password", password.Text);
      command1.Parameters.AddWithValue("@OfficeNo", officeNo.Text);
      command1.Parameters.AddWithValue("@Floor", Convert.ToInt32(floor.Text));
      command1.Parameters.AddWithValue("@Building", Convert.ToInt32(building.Text));

     command1.ExecuteNonQuery();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Problem is still unidentified. What actually you want to achieve, a new row or update an existing row.

In case of insert:

using( var command1 = new SqlCommand("INSERT INTO Employee(EmpPhone,Password,OfficeNo,Floor, Building) VALUES (@EmpPhone,@Password,@OfficeNo,@Floor, @Building)", con))
                {
                command1.Parameters.AddWithValue("@EmpPhone", Convert.ToInt32(phone.Text));
                command1.Parameters.AddWithValue("@Password", password.Text);
                command1.Parameters.AddWithValue("@OfficeNo", officeNo.Text);
                command1.Parameters.AddWithValue("@Floor", Convert.ToInt32(floor.Text));
                command1.Parameters.AddWithValue("@Building", Convert.ToInt32(building.Text));


  int i=         command1.ExecuteNonQuery();
}

Update:

using( var command1 = new SqlCommand("Update Employee Set EmpPhone=@EmpPhone,Password=@Password,OfficeNo=@OfficeNo,Floor=@Floor, Building = @Building Where EmployeeId =@Id", con))
 {
   command1.Parameters.AddWithValue("@EmpPhone", Convert.ToInt32(phone.Text));
   command1.Parameters.AddWithValue("@Password", password.Text);
   command1.Parameters.AddWithValue("@OfficeNo", officeNo.Text);
   command1.Parameters.AddWithValue("@Floor", Convert.ToInt32(floor.Text));
   command1.Parameters.AddWithValue("@Building", Convert.ToInt32(building.Text));
 command1.Parameters.AddWithValue("@Id", Convert.ToInt32(Id.Text));


  int i= command1.ExecuteNonQuery();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...