Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Experts..!

I am trying to execute a query using ado.net, but I am getting "Invalid Object name " exception. This exception tells that Table is not present in the database.
but while I am executing the same query directly to the Sql Server management studio it is working fine. In my connection string all the entries are correct, I have checked it several time.
Following is the code I am using:
C#
SqlConnection thisConnection1 = new SqlConnection(SSR_CONN.ToString());
string word="";
string fstring = "select count(d.ID) as reccnt from [dbo].[V01_PR_RAILDOCS] d, [dbo].[V01_PG_RAILDOCS] s where s.ID=d.ID and d.ID > 104770 AND s.PRONO='ABKET'";

                thisConnection1.Open();
         try{
                using (SqlCommand com = new SqlCommand(fstring, thisConnection1))
                {

                    using (SqlDataReader reader = com.ExecuteReader())
                    {

                        while (reader.Read())
                        {

                            word = reader["reccnt"].ToString();

                            checkrec2 = Convert.ToDouble(word);

                        }
                    }
                }

                thisConnection1.Close();
            }
          catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


Note: I have tried many ways to solve this problem:
Schema name I kept correctly like dbo.tablename, Connection string is correct it is working for other tables to the same database.
I did, Edit->intellisense->Referesh

What could be the problem??
Please suggest me..!
Thanks
Posted
Updated 10-Oct-19 20:48pm
Comments
F-ES Sitecore 24-Sep-15 4:19am    
Have you tried dropping the [dbo]?

select count(d.ID) as reccnt from [V01_PR_RAILDOCS] ...
[no name] 24-Sep-15 4:21am    
You really don't need a while loop here. You are returning a single value and check for the table name in the same database whether it exists or not. Similarly the columnname as well that you are using here are having proper name or not.
Mukesh Pr@sad 24-Sep-15 4:25am    
Hi sisir...I replaced the while loop with If condition..and The same query is working fine directly to the management studio so there is no case of wrong table name or column name, also the same connection string is working fine for other tables.
Mukesh Pr@sad 24-Sep-15 4:26am    
Hi F-ES..!
Do u mean that I should drop the table and recreate it.??
It contains many records.
Awadhendra Tripathi 24-Sep-15 8:22am    
His means not to drop table just check after removing "dbo"

Start by looking at your tables: Do you have a table called "V01_PR_RAILDOCS"? Then make sure that your tables contains the columns "ID" and "PRONO" - should the second one be "PROMO"?

I'd suggest that probably you need a JOIN rather than refer to two tables in teh same SELECT:
C#
string fstring = "SELECT COUNT(d.ID) AS reccnt FROM [dbo].[V01_PR_RAILDOCS] d JOIN [dbo].[V01_PG_RAILDOCS] s ON s.ID=d.ID WHERE d.ID > 104770 AND s.PRONO='ABKET'";

And why are you looping? You will only return one record (since it's a count) so your code should reflect that with an if rather than a while
 
Share this answer
 
Comments
Mukesh Pr@sad 24-Sep-15 4:16am    
both the queries are returning same value, when I am executing it directly to sql server management studio.
but through program same exception I am getting at line:-

using (SqlDataReader reader = com.ExecuteReader())

All the column name and table name are correct.
I ran into the problem with :
ODBC and SQL-Server-Authentication in ODBC
and
Firedac-Connection

Solution :
I had to set the Param MetaDefSchema to sqlserver username :
FDConnection1.Params.AddPair('MetaDefSchema', self.FDConnection1.Params.UserName);


The wikidoc sais :
MetaDefSchema=Default schema name. The Design time code >>excludes<< !! the schema name from the object SQL-Server-Authenticatoinname if it is equal to MetaDefSchema.

without setting, the automatic coder creates :
dbname.username.tablename -> invalid object name

With setting MetaDefSchema to sqlserver-username :
dbname.tablename -> works !

See also the embarcadero-doc at :
http://docwiki.embarcadero.com/RADStudio/Rio/en/Connect_to_Microsoft_SQL_Server_(FireDAC)


Hope, it helps someone else..

regards, Lutz
 
Share this answer
 
Comments
Richard Deeming 11-Oct-19 14:00pm    
The question is tagged as SQL Server 2012, and is using the built-in SqlClient classes. What makes you think FireDAC was involved?
Member 14619353 14-Oct-19 4:45am    
I simply looked for the errormessage and added my comment as a tipp for someone with same problem to have a chance to find a solution. We are all thankful for a bunch of possible solutions, are'nt we ? Anyway I'm sorry, if its too OT.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900