user7688277
user7688277

Reputation:

Invalid object name even if it exists

Here is my code:

   InitializeComponent();
   SqlConnection myConnection = new SqlConnection("data source = DESKTOP-77FA1JE;" +
             "user id = DESKTOP-77FA1JE\\Chanloi" +
             "initial catalog = TransactionProcessingSystem;" +
             "integrated security = SSPI");

   myConnection.Open();

    SqlCommand myCommand = new SqlCommand("select * from UserAccounts", myConnection);
   SqlDataReader myReader = myCommand.ExecuteReader();

   BindingSource mySource = new BindingSource();
   mySource.DataSource = myReader;

   dataGridView1.DataSource = mySource;

   myConnection.Close();

I get this error:

System.Data.SqlClient.SqlException: 'Invalid object name 'UserAccounts'.'

Upvotes: 0

Views: 3682

Answers (6)

Flame alchemist
Flame alchemist

Reputation: 127

if the table is created do a local refresh cahce from Edit section and then intellisense and then refresh lcoal cache

Upvotes: 0

Mostafa Mohamed Ahmed
Mostafa Mohamed Ahmed

Reputation: 649

In Such a Case What I Usually Do Is Run This Statment

 select name from sys.tables

sys.Tables is a System View That Has info About The Existing Tables In Your Db , This Way You Can Check If The Table Already Exists or U Misspelled It's Name or Existed By Another Name , and Even But an if Condition To Execute Your Logic Only If This Table Exists

Also as Other Have Said it's A Good Practice To Put Column Names Between a [] Brackets , and Call It By Schema Name like dbo.MyTable

Upvotes: 1

Muhammad Qasim
Muhammad Qasim

Reputation: 1712

The above exception says that the table UserAccounts does not exist in your database you are connected through your connection string

Upvotes: 0

mindOfAi
mindOfAi

Reputation: 4632

It's pretty straightforward. It's telling you that there is no UserAccounts table on your database. You can try dbo.UserAccounts, but if it fails, check your database first and also make sure you're connecting to right database.

Hope it helps!

Upvotes: 0

Sergey
Sergey

Reputation: 11

try this:

select * from dbo.[UserAccounts]

Upvotes: 0

Koby Douek
Koby Douek

Reputation: 16693

Very simple - You do not have a table called UserAccounts in your database. Please try dbo.UserAccounts

Upvotes: 1

Related Questions