4

I'm kind of new to C# and am trying out the convenient looking adaper-dataset combo with local database. while I was amazed at how easy and useful it was I stumbled upon an issue:

Whenever I try to fill the Item table with the adapter I get Invalid object name SQLexception, I use the same Dataset for all my tables, dsOns.Users / dsOns.Items. The problem is, the users adapter does recognize and successfully works with Users database(dbo.Users) while the items adapter cannot find the table(dbo.Item).

Heres some sniplets:

    UsersTableAdapter uAdapter = new UsersTableAdapter();
    ItemTableAdapter iAdapter = new ItemTableAdapter();

Users select:

SELECT Id, name, password, sold, isLogged, lastLog FROM dbo.Users

Item select:

SELECT Id, name, sale_price, min_price, cog FROM dbo.Item

Both use the same connection string:

Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ons_data.mdf;Integrated Security=True;Connect Timeout=30

Also note that in Dataset designer, the adapter works fine, it selects successfully. The error only occurs in code.

Image of the schema:

enter image description here

What could possibly cause this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Tim
  • 165
  • 3
  • 9
  • You're 100% sure that the `Item` table is in the `dbo` schema? You didn't add it to another schema accidentally? – Simon Whitehead May 19 '14 at 03:07
  • Yes I double-checked, and tried to upload image here to show the schema but I need 10 rep for it, or so it says. – Tim May 19 '14 at 03:09
  • Okay that was weird, I solved it by simply changing the view of the database explorer, to find that the app uses two databases, in UI(dataset designer) it uses my original db, while in code it uses the same db(but outdated since I changed it via dataset designer) that was copied to debug folder, hence containing only Users table alone and resulting in the error. Is there a more practical way to have it synchronized than shutting down visual studios and copying manually every time I change something in the Database? – Tim May 19 '14 at 10:38
  • Tim, answer you own question below and mark it as solved for reference. – kurast May 20 '14 at 18:04

3 Answers3

1

Okay that was weird, I solved it by simply changing the view of the database explorer, to find that the app uses two databases, in UI(dataset designer) it uses my original db, while in code it uses the same db(but outdated since I changed it via dataset designer) that was copied to debug folder, hence containing only Users table alone and resulting in the error

Tim
  • 165
  • 3
  • 9
0

I am unsure if item may be considered a reserved word in some cases perhaps in the context of your connection. Might want to refer to is at [dbo].[item].

0

Try changing the query which you are passing to the SqlDataReader in my case it was "Select [Name],[Location],[Email] FROM [dbo].[CreateDB]" but I tried "Select Name,Location,Email FROM YourTableName" and it worked, you can also use "Select * FROM YourTableName".

Laxman Gite
  • 2,248
  • 2
  • 15
  • 23
Ghost
  • 21
  • 4