views:

76

answers:

2

I'm trying to execute an Insert statement, but keep getting a Invalid object name error.

Here's my 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();
}

And here is my error message:

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

Can anyone help me figure out why I get this error?

UPDATE

I should be using the correct database, because the following query works:

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

My connection string looks like this:

SqlConnection connection = new SqlConnection();
SqlDataAdapter adapter = new SqlDataAdapter();

// class constructor
public nokernokDAL()
{
    connection.ConnectionString = EPiServer.Global.EPConfig["EPsConnection"].ToString();
    connection.Open();
}
+1  A: 

From the error message, it would appear that the table dbo.nokernok_kommentarer doesn't exist in your database, or it isn't a table and is thus not updatable.

Have you checked that:

  • You're connecting to the server you think you're connecting to?
  • You're connecting to the database you think you're connecting to?
  • You're specifiying the correct catalog (or whatever it's currently called =) i.e. Are you sure it should be dbo. and not somethingElse.?
  • The table dbo.nokernok_kommentarer exists?

If you copy the SQL out from your code and run it in something like SQL Server Management Studio, does it work without error there?

Rob
The query executes fine if executed from SQL Server Manager. I should be connected to correct DB, because `SELECT`queries works fine.
Steven
@Steven In your examples, you're INSERTing into `dbo.nokernok_kommentarer`, but SELECTing from `nokernok_skoler`. Can you successfully SELECT from dbo.nokernok_kommentarer?
Matt Gibson
*coff*.... see above comment
Steven
+3  A: 

You're probably in the wrong database. Include an initial catalog in your connection string:

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername; ...
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^

Or specify a three part name:

INSERT INTO myDataBase.dbo.nokernok_kommentarer
            ^^^^^^^^^^
Andomar
Even though I'm using the code above? (See updated code)
Steven
@Steven: What does `select SCHEMA_NAME(schema_id) from sys.tables where name = 'nokernok_kommentarer'` return?
Andomar
It returns `dbo`. Uh oh... hang on... testing something here.
Steven
@Steven: Then the `.dbo.` part is right. Have you tried to use the three part name? To find the database name use `select DB_NAME()`
Andomar
I think I found the problem. I had created the table in an old DB which I'm not using any more. Thus the new table was never in the correct DB.... :-/
Steven
@Steven - sounds like my "you're connecting to the *database* you think you're connecting to?" answer is pretty close then. The number of times I've made exactly the same mistake, aargh! :)
Rob
Well, I was actually **connecting** to the right database. I just had created the table in the wrong database. Oh well, half day gone trying to figure out what was wrong.... now to figure out why my query is triggered twice (and the data is inserted twice).....
Steven
Thanks for your help, by the way :)
Steven