0

I have a method in c# that accepts a list and its purpose is to insert it into a SQL Server table, but it doesn't insert at all, can you please help me here. any insights is very much appreciated.

Here is my code

public void insertToDB(List<PlantillaData> plantillaDataList)
{
   IsqlConnect db = new IsqlConnect();
   string conQuery = db.connectionQuery;
   SqlConnection con = new SqlConnection(conQuery);

   try
   {
      con.Open();

      foreach (PlantillaData plantillaData in plantillaDataList)
      {
         SqlCommand cmd = new SqlCommand("INSERT INTO dbo.t_employee(Telephone_Number_1," +
                    "Employee_Number,Employee_LastName,Employee_FirstName,Employee_MiddleName," +
                    "Cluster,Division,Department,Unit,Supervisor,Email,EWB_Rank,Position," +
                    "Location,Employee_Category)VALUES(@telNum1,@empNum,@empLastName,@empFirstName," +
                    "@empMiddleName,@cluster,@division,@unit,@supervisor,@email,@rank," +
                    "@position,@location,@emp_cat) ", con);

         cmd.Parameters.Add("@telNum1", SqlDbType.VarChar).Value = plantillaData.telOneVal;
         cmd.Parameters.Add("@empNum", SqlDbType.Int).Value = plantillaData.empNumVal;
         cmd.Parameters.Add("@empLastName", SqlDbType.VarChar).Value = plantillaData.lName;
         cmd.Parameters.Add("@empFirstName", SqlDbType.VarChar).Value = plantillaData.fName;
         cmd.Parameters.Add("@empMiddleName", SqlDbType.VarChar).Value = plantillaData.mName;
         cmd.Parameters.Add("@cluster", SqlDbType.VarChar).Value = plantillaData.clusterVal;
         cmd.Parameters.Add("@division", SqlDbType.VarChar).Value = plantillaData.divVal;
         cmd.Parameters.Add("@unit", SqlDbType.VarChar).Value = plantillaData.unitVal;
         cmd.Parameters.Add("@supervisor", SqlDbType.VarChar).Value = plantillaData.supervisorVal;
         cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = plantillaData.emailVal;
         cmd.Parameters.Add("@rank", SqlDbType.VarChar).Value = plantillaData.rankVal;
         cmd.Parameters.Add("@position", SqlDbType.VarChar).Value = plantillaData.posVal;
         cmd.Parameters.Add("@location", SqlDbType.VarChar).Value = plantillaData.locVal;
         cmd.Parameters.Add("@emp_cat", SqlDbType.VarChar).Value = 1;

         try
         {
             con.Open();
             cmd.ExecuteNonQuery();
         }
         catch
         {
         }
         finally
         {
             con.Close();
         }
      }
   }
   catch (Exception e)
   {
      throw new Exception("SQL Error:" + e.Message.ToString());
   }
   finally
   {
      con.Close();
   }
}
2
  • 2
    See if your inner try-catch contains some error Commented Aug 2, 2013 at 10:00
  • 1
    catch {} - never do that again Commented Aug 2, 2013 at 10:02

2 Answers 2

1

You are missing a department value for the VALUES field in the insert query right before the Unit. If you had trapped and displayed the error you would have got the violation

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @lloydom but i have already added that item and still cannot load it to database.
have you also added the department parameter too in the cmd.parameter section?
0

Why do you open the connection twice? Are you sure you have Items in your List? Do you get any Exceptions?

The inner catch will prevent calling the outer catch-block!

2 Comments

Sorry for that, i have changed that too and still cannot load it.
i dont get any exceptions on this, i have modified my code and was able to load at least 12 items only, but my count on my list is 3900+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.