title: 'SqlException was unhandled' error when trying to connect to SQL Server database
body: <p>I have a program that takes information passed to it, and puts the information in a database. The program is being created in VS2008 for a Windows Ce 6.0 device, and I am having trouble connecting to the database. I want to connect over the local network, but not entirely too sure how to do so. Here is my code for the function that calls the database.</p>
  
  <pre><code>public void callSQL(string partNumber, int total, int numOfPacks, string dunsNumber, string serialNumber, string laneNumber)
  {
      //establish connection
      string conString = "Data Source=10.97.2.67,1433;Initial Catalog=testing.mdf;User ID=**********;Persist Security Info=false";
  
      using (SqlConnection con = new System.Data.SqlClient.SqlConnection(conString))
      {
          con.Open();
  
          try
          {
              //open the server
              //Insert values passed into the method
              SqlCommand myCommand = new SqlCommand("INSERT INTO test (Part_Number, total, number_of_packs, dunsNumber, serialNumber, truck_number) VALUES (@Part_Number,@total,@number_of_packs,@dunsNumber,@serialNumber,@truck_number)", con);
              myCommand.Parameters.AddWithValue("@Part_Number", partNumber);
              myCommand.Parameters.AddWithValue("@total", total);
              myCommand.Parameters.AddWithValue("@number_of_packs", numOfPacks);
              myCommand.Parameters.AddWithValue("@dunsNumber", dunsNumber);
              myCommand.Parameters.AddWithValue("@serialNumber", serialNumber);
              myCommand.Parameters.AddWithValue("@truck_number", laneNumber);
  
              //execute the query
              myCommand.ExecuteNonQuery();
              con.Close();
          }
          catch (SqlException e)
          {
             string error = e.ToString();
             error = error.ToLower();
  
             if (error.Contains("duplicate") == true)
             {
                Console.WriteLine("You have already scanned this barcode.");
             }
          }
      }
  }
  </code></pre>
  
  <p>When I run the code, the code stops at con.Open(); and I get an error which says </p>
  
  <blockquote>
    <p>SqlException was unhandled</p>
  </blockquote>
  
  <p>Here is the information from the error:</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException {"SqlException"}<br>
    [System.Data.SqlClient.SqlException] {"SqlException"}<br>
    {Systen.Data.SqlClient.SqlErrorCOllection}<br>
    LineNumber  0<br>
    Message     "SqlException"<br>
    Number      17<br>
    Procedure   "ConnectionOpen(Connect())."<br>
    Server      "10.97.2.67,1433"<br>
    Source      ".Net SqlClient Data Provider"<br>
    StackTrace<br>
    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, TdsParserState state)<br>
    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, TdsParserState state)<br>
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()<br>
    at System.Data.SqlClient.TdsParser.Connect(String host, SqlInternalConnection connHandler, Int32 timeout)<br>
    at System.Data.SqlClient.SqlInternalConnection.OpenAndLogin()<br>
    at System.Data.SqlClient.SqlInternalConnection..ctor(SqlConnection connection, Hashtable connectionOptions)<br>
    at System.Data.SqlClient.SqlConnection.Open()<br>
    at scan.partObject.callSQL(String partNumber, Int32 total, Int32 numOfPacks, String dunsNumber, String serialNumber, String laneNumber)<br>
    at scan.Program.Main(String[] args)  </p>
  </blockquote>
  
  <p>Thanks</p>