Reputation:
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
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
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
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
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
Reputation: 16693
Very simple - You do not have a table called UserAccounts
in your database.
Please try dbo.UserAccounts
Upvotes: 1