#1
  1. המבין יבין
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2001
    Location
    Haifa
    Posts
    2,085
    Rep Power
    1486

    Invalid object name


    I have an SQL Express file with a single table People. People has six or seven columns, but no rows. The database is a resource in my Visual Studio 2010 Express project. I am trying to connect with this code:

    Code:
    SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename=C:\\someLocation\\someDatabase.mdf; Integrated Security=True; User Instance=True ");
    conn.Open();
    SqlCommand cmd = new SqlCommand(@"select * from People;", conn);
    However, I am getting the following error:

    Code:
    Invalid object name 'People'.
    Based on some casual googling I tried to precede the table name with dbo (dbo.People) but that did not resolve the issue. What might I be doing wrong?

    Thanks.
    . . . What is Firefox?
    . . . . . . What is Linux?
    . . . . . . . . . . . What is Love?
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    724
    Rep Power
    291
    Looks like you don't have a table named [People].

    Verify that the table does exist and is spelled correctly.
  4. #3
  5. המבין יבין
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2001
    Location
    Haifa
    Posts
    2,085
    Rep Power
    1486
    Thank you. Actually, there is most certainly a table named People, spelling verified by copy-and-paste.
    . . . What is Firefox?
    . . . . . . What is Linux?
    . . . . . . . . . . . What is Love?
  6. #4
  7. המבין יבין
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2001
    Location
    Haifa
    Posts
    2,085
    Rep Power
    1486
    Originally Posted by dotancohen
    Thank you. Actually, there is most certainly a table named People, spelling verified by copy-and-paste.
    It can be seen here that the table does exist, and is populated with data:
    Attached Images
    . . . What is Firefox?
    . . . . . . What is Linux?
    . . . . . . . . . . . What is Love?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Location
    Kansas City
    Posts
    13
    Rep Power
    0
    Most of the time this is because you are looking in the wrong database.

    Are you positive your pointing to the correct database?

    Comments on this post

    • dotancohen agrees
  10. #6
  11. המבין יבין
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2001
    Location
    Haifa
    Posts
    2,085
    Rep Power
    1486
    I want to say that I'm sure, but I'm not! I now see that since my project is on a network drive Visual Studio is creating the database inside the project, then silently making a copy in my profile folder (I'm not running as Admin) and using that one. So the database created in the project is not the same database that is shown in the Database Explorer.

    What a mess! At least I've found the issue. Thanks.
    . . . What is Firefox?
    . . . . . . What is Linux?
    . . . . . . . . . . . What is Love?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Location
    Kansas City
    Posts
    13
    Rep Power
    0
    Yeah no problem. It's very common.

    I don't remember how many times I did that to myself in the management studio. I had master db selected but was trying to query tables from another database.

IMN logo majestic logo threadwatch logo seochat tools logo