Blurryface213
Blurryface213

Reputation: 93

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

I'm a beginner in using Asp.net MVC5, I am trying to update the records to the database and this always show up.

Here is the 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

Upvotes: 3

Views: 1341

Answers (1)

Cristian Szpisjak
Cristian Szpisjak

Reputation: 2479

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();

Upvotes: 1

Related Questions