title: How to handle SqlException when trying to insert (and row already exists)
body: <p>I have a C# MVC app that adds things to a database and that part works.  I have a Primary Key called <code>CRN</code>.  When I try to insert a row that contains a CRN that already exists I get <code>An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code</code> as expected.  However, the program stops there.  I'm trying to figure out how to get a result (something like -1 if it failed or a number of rows added if it succeeded) but continue the program and let me know.  I'm fairly new to this but I'm thinking I need to use <code>try</code>  and <code>catch</code> maybe (not sure how)?</p>
  
  <p>C#:</p>
  
  <pre><code>using (SqlConnection connect = new SqlConnection(connection))
          {
              int result = 0;
              string insertQuery = "Insert Into Classes (OpenClosed, Section, CRN, CreditHours, PartTerm, Capacity, Enrolled, SeatsAvailable, WaitlistCapacity, WaitlistCount, WaitlistAvailability, Days, Campus, Method, Location, Time, StartDate, EndDate, Instructor) VALUES (@OpenClosed, @Section, @CRN, @CreditHours, @PartTerm, @Capacity, @Enrolled, @SeatsAvailable, @WaitlistCapacity, @WaitlistCount, @WaitlistAvailability, @Days, @Campus, @Method, @Location, @Time, @StartDate, @EndDate, @Instructor)";
              SqlCommand command = new SqlCommand(insertQuery, connect);
              command.Parameters.AddWithValue("OpenClosed", selectedClasses.OpenClosed);
              command.Parameters.AddWithValue("Section", selectedClasses.Section);
              command.Parameters.AddWithValue("CRN", selectedClasses.CRN);
              command.Parameters.AddWithValue("CreditHours", selectedClasses.CreditHours);
              command.Parameters.AddWithValue("PartTerm", selectedClasses.PartTerm);
              command.Parameters.AddWithValue("Capacity", selectedClasses.Capacity);
              command.Parameters.AddWithValue("Enrolled", selectedClasses.Enrolled);
              command.Parameters.AddWithValue("SeatsAvailable", selectedClasses.SeatsAvailable);
              command.Parameters.AddWithValue("WaitlistCapacity", selectedClasses.WaitlistCapacity);
              command.Parameters.AddWithValue("WaitlistCount", selectedClasses.WaitlistCount);
              command.Parameters.AddWithValue("WaitlistAvailability", selectedClasses.WaitlistAvailability);
              command.Parameters.AddWithValue("Days", selectedClasses.Days);
              command.Parameters.AddWithValue("Campus", selectedClasses.Campus);
              command.Parameters.AddWithValue("Method", selectedClasses.Method);
              command.Parameters.AddWithValue("Location", selectedClasses.Location);
              command.Parameters.AddWithValue("Time", selectedClasses.Time);
              command.Parameters.AddWithValue("StartDate", selectedClasses.StartDate);
              command.Parameters.AddWithValue("EndDate", selectedClasses.EndDate);
              command.Parameters.AddWithValue("Instructor", selectedClasses.Instructor);
              connect.Open();
              result = command.ExecuteNonQuery();
  
          }
  </code></pre>