Al Matias
Al Matias

Reputation: 9

Can't insert list data to sql server table using c#

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

Upvotes: 0

Views: 684

Answers (2)

lloydom
lloydom

Reputation: 387

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

Upvotes: 1

BendEg
BendEg

Reputation: 21088

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!

Upvotes: 0

Related Questions