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

I am starting to use Asp.net MVC5, I am trying to update records in the database and this always shows up.

Here is a screenshot

enter image description here

Here is the code:

        SqlConnection con = new SqlConnection(DAL.cs);
        con.Open();
        SqlCommand com = new SqlCommand("UPDATE Student SET LastName = @LastName," +
            "FirstName = @FirstName," +
            "MiddleName = @MiddleName," +
            "WHERE ID = @ID", con);

        com.Parameters.AddWithValue("@ID", SqlDbType.Int).Value = s.ID;
        com.Parameters.AddWithValue("@LastName", SqlDbType.VarChar).Value = s.LastName;
        com.Parameters.AddWithValue("@FirstName", SqlDbType.VarChar).Value = s.FirstName;
        com.Parameters.AddWithValue("@MiddleName", SqlDbType.VarChar).Value = s.MiddleName;
        com.ExecuteNonQuery();
        con.Close();

      

strong text

+3


source to share


1 answer


Remove the comma after the last parameter:



SqlConnection con = new SqlConnection(DAL.cs);
con.Open();
SqlCommand com = new SqlCommand("UPDATE Student SET LastName = @LastName," +
        "FirstName = @FirstName," +
        "MiddleName = @MiddleName " +
        "WHERE ID = @ID", con);

com.Parameters.AddWithValue("@ID", SqlDbType.Int).Value = s.ID;
com.Parameters.AddWithValue("@LastName", SqlDbType.VarChar).Value = s.LastName;
com.Parameters.AddWithValue("@FirstName", SqlDbType.VarChar).Value = s.FirstName;
com.Parameters.AddWithValue("@MiddleName", SqlDbType.VarChar).Value = s.MiddleName;
com.ExecuteNonQuery();
con.Close();

      

+1


source







All Articles