title: SQL Server: invalid object name in query execution
body: <p>I'm trying to execute an Insert statement, but keep getting a <code>Invalid object name</code> error.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>public string addNewComment(int userID, int pageID, string title, string comment)
  {
      string query = "INSERT INTO dbo.nokernok_kommentarer (userID, pageID, commentTitle, comment) " +
      "VALUES ("+ userID +", "+ pageID +", '"+ title +"', '"+ comment +"')";
  
      adapter.InsertCommand = new SqlCommand(query, connection);
  
      //ExecuteNonQuery retuens number of rows affected
      int numRows = adapter.InsertCommand.ExecuteNonQuery();
      return numRows.ToString();
  }
  </code></pre>
  
  <p>And here is my error message:  </p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException:
    Invalid object name
    'dbo.nokernok_kommentarer'. at
         System.Data.SqlClient.SqlConnection.OnError(SqlException
    exception, Boolean breakConnection) at
         System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
    exception, Boolean breakConnection) at
         System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
    stateObj) at
         System.Data.SqlClient.TdsParser.Run(RunBehavior
    runBehavior, SqlCommand cmdHandler,
    SqlDataReader dataStream,
    BulkCopySimpleResultSet
    bulkCopyHandler, TdsParserStateObject
    stateObj) at
         System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String
    methodName, Boolean async) at
         System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult
    result, String methodName, Boolean
    sendToPipe) at
    System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
    at
         development.DAL.nokernokDAL.addNewComment(Int32
    userID, Int32 pageID, String title,
    String comment) in 
         C:\Inetpub\wwwroot\naaf\DAL\nokernok.cs:line
    49</p>
  </blockquote>
  
  <p>Can anyone help me figure out why I get this error?</p>
  
  <p><strong>UPDATE</strong>  </p>
  
  <p>I should be using the correct database, because the following query works:</p>
  
  <pre><code>    public DataSet getSchools(string countyCode)
      {
          DataSet ds = new DataSet();
          string query = "SELECT * FROM nokernok_skoler WHERE kommunekode LIKE '" + countyCode.Substring(0, 2) + "%' ORDER BY enhetsnavn";
          adapter.SelectCommand = new SqlCommand(query, connection);
          adapter.Fill(ds);
          return ds;
      }
  </code></pre>
  
  <p>My connection string looks like this:</p>
  
  <pre><code>SqlConnection connection = new SqlConnection();
  SqlDataAdapter adapter = new SqlDataAdapter();
  
  // class constructor
  public nokernokDAL()
  {
      connection.ConnectionString = EPiServer.Global.EPConfig["EPsConnection"].ToString();
      connection.Open();
  }
  </code></pre>