title: C# Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
body: <p>Apologies in advance if I missed an answer to this somewhere but I wasn't quite finding it anywhere. So I'm building an application that scans PDF's of service orders our company gets, parses it, and inserts it into a SQL DB. The problem is at the end of this code. It successfully :</p>
  
  <ul>
  <li>saves the original pdf in the proper folder</li>
  <li>scans the pdf and parses it</li>
  <li>inserts the correct data into the service order table</li>
  <li>grabs PK of service order just created as we need that for the next batch of inserts</li>
  </ul>
  
  <p>Here is where it gets hung up with a Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll</p>
  
  <p>I foreach through all the instruments as there are multiples per Service Order, but it is erroring on this somewhere. to be clear I put a break point on the insert statement and all of the data is good and in the proper format ("string" int) </p>
  
  <p>I feel like its in my connection maybe?</p>
  
  <p>Anyways, thanks in advance for the help.</p>
  
  <pre><code>string filename = Path.GetFileName(FileUpload1.FileName);
  FileUpload1.SaveAs(Server.MapPath("~/PDF/") + filename);
  
  // Now we parse the PDF by creating a new ServiceOrder object and parsing from it.
  ServiceOrder so = new ServiceOrder();
  
  // Make sure we load the PDF from the correct path on the server
  so.LoadPDF(Server.MapPath("~/PDF/") + filename);
  
  String strConnString = "Data Source=127.0.0.0;Initial Catalog=SOMECATALOG;User ID=SOMEUSER;Password=SOMEPASSWORD";
  
  // Insert Into Service Orders Table
  string defaultdate = DateTime.Now.ToString("yyyy-MM-dd");
  String strQuery = "insert into TServiceOrders (strServiceOrderNo, intStatusCodeID, strCustomerName, strCustomerNo, strCustomerAddress1, strCustomerAddress2, strCustomerAddress3, intRepID, strServiceDescription, strServiceRequestDate, strServiceOrderDate, strNotes) values ('" 
      + so.ServiceOrderNumber.ToString() + "', 2, '" 
      + so.CustomerContactName.ToString() + "', '" 
      + so.CustomerNumber.ToString() + "', '" 
      + so.CustomerContactAddress1.ToString() + "', '" 
      + so.CustomerContactAddress2.ToString() + "', '" 
      + so.CustomerContactAddress3.ToString() + "', 1, '', '" 
      + defaultdate + "', '" + defaultdate + "', '')";
  
  SqlConnection conn = new SqlConnection(strConnString);
  SqlCommand cmd = new SqlCommand(strQuery, conn);
  conn.Open();
  cmd.ExecuteNonQuery();
  conn.Close();
  
  // Grabbing latest primary key od service order just added for next instrument inserts
  int lastid = 999999;
  String strPKquery = "select top 1 intServiceOrderID from TServiceOrders order by intServiceOrderID desc";
  SqlDataReader rdr = null;
  SqlConnection conn2 = new SqlConnection(strConnString);
  SqlCommand cmd2 = new SqlCommand(strPKquery, conn2);
  try
  {
      conn2.Open();
      rdr = cmd2.ExecuteReader();
      while (rdr.Read())
      {
          lastid = (int)rdr["intServiceOrderID"];
      }
  
  }
  finally
  {
      if (rdr != null)
      {
          rdr.Close();
      }
  
      if (conn2 != null)
      {
          conn2.Close();
      }
  }
  
  // Insert Into Service Instruments Tables           
  SqlConnection conn3 = new SqlConnection(strConnString);
  conn3.Open();
  foreach (ServiceInstrument sin in so.ServiceInstruments)
  {
      string sim = "";
      sim = sin.ServiceInstrumentModel;
      if (String.IsNullOrEmpty(sim))
      {
          sim = "";
      }
      else
      {
          sim = sin.ServiceInstrumentModel.ToString();
      }
      string sid = "";
      sid = sin.ServiceInstrumentDescription;
      if (String.IsNullOrEmpty(sid))
      {
          sid = "";
      }
      else
      {
          sid = sin.ServiceInstrumentDescription.ToString();
      }
      string sis = "";
      sis = sin.ServiceInstrumentSerial;
      if (String.IsNullOrEmpty(sis))
      {
          sis = "";
      }
      else
      {
          sis = sin.ServiceInstrumentSerial.ToString();
      }
      string sih = "";
      sih = sin.ServiceInstrumentHandle;
      if (String.IsNullOrEmpty(sih))
      {
          sih = "";
      }
      else
      {
          sih = sin.ServiceInstrumentHandle.ToString();
      }
      string sip = "";
      sip = sin.ServiceInstrumentParentAsset;
      if (String.IsNullOrEmpty(sip))
      {
          sip = "";
      }
      else
      {
          sip = sin.ServiceInstrumentParentAsset.ToString();
      }                        
      String strQuery3 = "insert into TServiceInstruments values ('" + sim.ToString() + "', '" + sid.ToString() + "', '" + sis.ToString() + "', '" + sih.ToString() + "', " + sip.ToString() + ", " + lastid + ")";
      SqlCommand cmd3 = new SqlCommand(strQuery3, conn3);                       
      cmd3.ExecuteNonQuery();                        
  }
  conn3.Close();
  </code></pre>