Invalid object name

C

Curious

I got an exception when executing the SQL statement in my code below.
The error is "Invalid object name
'db_dynamic_trading.dbo.TrdRpt_broker_list'".

I know for a fact that 'db_dynamic_trading.dbo.TrdRpt_broker_list' is
valid, because I can run the same SQL statement in Management Studio
and it works.

Any advice on this? Is there anything wrong with my connection string
(although there is no complaint about the connection string in my
debugger)?


static void Main(string[] args)
{
string sConnectionString = "Integrated
Security=SSPI;Initial Catalog=db_dynamic_trading;Data
Source=PANSQLDEV";
SqlConnection objConn = new
SqlConnection(sConnectionString);
objConn.Open();
string sSQL = "SELECT DISTINCT symbol_requested " +
"FROM
db_dynamic_trading.dbo.TrdRpt_broker_list " +
"WHERE symbol_requested LIKE '%SCC%'";
SqlCommand objCmd = new SqlCommand(sSQL, objConn);

try
{
objCmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Records selected");
}
 
R

rhaazy

I got an exception when executing the SQL statement in my code below.
The error is "Invalid object name
'db_dynamic_trading.dbo.TrdRpt_broker_list'".

I know for a fact that 'db_dynamic_trading.dbo.TrdRpt_broker_list' is
valid, because I can run the same SQL statement in Management Studio
and it works.

Any advice on this? Is there anything wrong with my connection string
(although there is no complaint about the connection string in my
debugger)?

static void Main(string[] args)
{
string sConnectionString = "Integrated
Security=SSPI;Initial Catalog=db_dynamic_trading;Data
Source=PANSQLDEV";
SqlConnection objConn = new
SqlConnection(sConnectionString);
objConn.Open();
string sSQL = "SELECT DISTINCT symbol_requested " +
"FROM
db_dynamic_trading.dbo.TrdRpt_broker_list " +
"WHERE symbol_requested LIKE '%SCC%'";
SqlCommand objCmd = new SqlCommand(sSQL, objConn);

try
{
objCmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Records selected");
}

'db_dynamic_trading.dbo.TrdRpt_broker_list'

this table name looks funny, are you sure its correct?

also objCmd.ExecuteNonQuery(); is wrong.
Execute NON query is usually used for update or delete commands.
where is the result of your select statement going to be saved? you
need to have some object that will hold the result of your statement.
See below for example.

SqlCommand cmd = new SqlCommand("some SELECT statement here", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
 
C

Curious

I got an exception when executing the SQL statement in my code below.
The error is "Invalid object name
'db_dynamic_trading.dbo.TrdRpt_broker_list'".
I know for a fact that 'db_dynamic_trading.dbo.TrdRpt_broker_list' is
valid, because I can run the same SQL statement in Management Studio
and it works.
Any advice on this? Is there anything wrong with my connection string
(although there is no complaint about the connection string in my
debugger)?
       static void Main(string[] args)
        {
            string sConnectionString = "Integrated
Security=SSPI;Initial Catalog=db_dynamic_trading;Data
Source=PANSQLDEV";
            SqlConnection objConn = new
SqlConnection(sConnectionString);
            objConn.Open();
            string sSQL = "SELECT DISTINCT symbol_requested " +
                          "FROM
db_dynamic_trading.dbo.TrdRpt_broker_list " +
                          "WHERE symbol_requested LIKE '%SCC%'";
            SqlCommand objCmd = new SqlCommand(sSQL, objConn);
            try
            {
                objCmd.ExecuteNonQuery();
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("Records selected");
        }

'db_dynamic_trading.dbo.TrdRpt_broker_list'

this table name looks funny, are you sure its correct?

also objCmd.ExecuteNonQuery(); is wrong.
Execute NON query is usually used for update or delete commands.
where is the result of your select statement going to be saved?  you
need to have some object that will hold the result of your statement.
See below for example.

SqlCommand cmd = new SqlCommand("some SELECT statement here", con);
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adp.Fill(ds);- Hide quoted text -

- Show quoted text -

The table name is 'TrdRpt_broker_list'. However, I must add the
database name plus 'dbo' before it
('db_dynamic_trading.dbo.TrdRpt_broker_list'); otherwise, I'll get an
error about table not found.

I do need to return the records that return from the SELECT statement.
Thanks for the sample code. I'll give it a try.
 
R

rhaazy

I got an exception when executing the SQL statement in my code below.
The error is "Invalid object name
'db_dynamic_trading.dbo.TrdRpt_broker_list'".
I know for a fact that 'db_dynamic_trading.dbo.TrdRpt_broker_list' is
valid, because I can run the same SQL statement in Management Studio
and it works.
Any advice on this? Is there anything wrong with my connection string
(although there is no complaint about the connection string in my
debugger)?
       static void Main(string[] args)
        {
            string sConnectionString = "Integrated
Security=SSPI;Initial Catalog=db_dynamic_trading;Data
Source=PANSQLDEV";
            SqlConnection objConn = new
SqlConnection(sConnectionString);
            objConn.Open();
            string sSQL = "SELECT DISTINCT symbol_requested " +
                          "FROM
db_dynamic_trading.dbo.TrdRpt_broker_list " +
                          "WHERE symbol_requested LIKE '%SCC%'";
            SqlCommand objCmd = new SqlCommand(sSQL, objConn);
            try
            {
                objCmd.ExecuteNonQuery();
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("Records selected");
        }
'db_dynamic_trading.dbo.TrdRpt_broker_list'

this table name looks funny, are you sure its correct?
also objCmd.ExecuteNonQuery(); is wrong.
Execute NON query is usually used for update or delete commands.
where is the result of your select statement going to be saved?  you
need to have some object that will hold the result of your statement.
See below for example.
SqlCommand cmd = new SqlCommand("some SELECT statement here", con);
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adp.Fill(ds);- Hide quoted text -
- Show quoted text -

The table name is  'TrdRpt_broker_list'. However, I must add the
database name plus 'dbo' before it
('db_dynamic_trading.dbo.TrdRpt_broker_list'); otherwise, I'll get an
error about table not found.

I do need to return the records that return from the SELECT statement.
Thanks for the sample code. I'll give it a try.- Hide quoted text -

- Show quoted text -

you shouldn't need to include the database name as part of your
object. when you create your connection string the database should be
specified there.

string sSQL = "SELECT DISTINCT symbol_requested " +
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top