Invalid object name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Curious

    Invalid object name

    I got an exception when executing the SQL statement in my code below.
    The error is "Invalid object name
    'db_dynamic_tra ding.dbo.TrdRpt _broker_list'".

    I know for a fact that 'db_dynamic_tra ding.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 sConnectionStri ng = "Integrated
    Security=SSPI;I nitial Catalog=db_dyna mic_trading;Dat a
    Source=PANSQLDE V";
    SqlConnection objConn = new
    SqlConnection(s ConnectionStrin g);
    objConn.Open();
    string sSQL = "SELECT DISTINCT symbol_requeste d " +
    "FROM
    db_dynamic_trad ing.dbo.TrdRpt_ broker_list " +
    "WHERE symbol_requeste d LIKE '%SCC%'";
    SqlCommand objCmd = new SqlCommand(sSQL , objConn);

    try
    {
    objCmd.ExecuteN onQuery();
    }
    catch (System.Excepti on e)
    {
    Console.WriteLi ne(e.Message);
    }
    Console.WriteLi ne("Records selected");
    }
  • rhaazy

    #2
    Re: Invalid object name

    On Aug 21, 10:58 am, Curious <fir5tsi...@yah oo.comwrote:
    I got an exception when executing the SQL statement in my code below.
    The error is "Invalid object name
    'db_dynamic_tra ding.dbo.TrdRpt _broker_list'".
    >
    I know for a fact that 'db_dynamic_tra ding.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 sConnectionStri ng = "Integrated
    Security=SSPI;I nitial Catalog=db_dyna mic_trading;Dat a
    Source=PANSQLDE V";
    SqlConnection objConn = new
    SqlConnection(s ConnectionStrin g);
    objConn.Open();
    string sSQL = "SELECT DISTINCT symbol_requeste d " +
    "FROM
    db_dynamic_trad ing.dbo.TrdRpt_ broker_list " +
    "WHERE symbol_requeste d LIKE '%SCC%'";
    SqlCommand objCmd = new SqlCommand(sSQL , objConn);
    >
    try
    {
    objCmd.ExecuteN onQuery();
    }
    catch (System.Excepti on e)
    {
    Console.WriteLi ne(e.Message);
    }
    Console.WriteLi ne("Records selected");
    }
    'db_dynamic_tra ding.dbo.TrdRpt _broker_list'

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

    also objCmd.ExecuteN onQuery(); 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("som e SELECT statement here", con);
    cmd.CommandType = CommandType.Tex t;
    SqlDataAdapter adp = new SqlDataAdapter( cmd);
    DataSet ds = new DataSet();
    adp.Fill(ds);

    Comment

    • Curious

      #3
      Re: Invalid object name

      On Aug 21, 3:20 pm, rhaazy <rha...@gmail.c omwrote:
      On Aug 21, 10:58 am, Curious <fir5tsi...@yah oo.comwrote:
      >
      >
      >
      >
      >
      I got an exception when executing the SQL statement in my code below.
      The error is "Invalid object name
      'db_dynamic_tra ding.dbo.TrdRpt _broker_list'".
      >
      I know for a fact that 'db_dynamic_tra ding.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 sConnectionStri ng = "Integrated
      Security=SSPI;I nitial Catalog=db_dyna mic_trading;Dat a
      Source=PANSQLDE V";
                  SqlConnection objConn = new
      SqlConnection(s ConnectionStrin g);
                  objConn.Open();
                  string sSQL = "SELECT DISTINCT symbol_requeste d " +
                                "FROM
      db_dynamic_trad ing.dbo.TrdRpt_ broker_list " +
                                "WHERE symbol_requeste d LIKE '%SCC%'";
                  SqlCommand objCmd = new SqlCommand(sSQL , objConn);
      >
                  try
                  {
                      objCmd.ExecuteN onQuery();
                  }
                  catch (System.Excepti on e)
                  {
                      Console.WriteLi ne(e.Message);
                  }
                  Console.WriteLi ne("Records selected");
              }
      >
      'db_dynamic_tra ding.dbo.TrdRpt _broker_list'
      >
      this table name looks funny, are you sure its correct?
      >
      also objCmd.ExecuteN onQuery(); 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("som e SELECT statement here", con);
                      cmd.CommandType = CommandType.Tex t;
                      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_tr ading.dbo.TrdRp t_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.

      Comment

      • rhaazy

        #4
        Re: Invalid object name

        On Aug 21, 5:15 pm, Curious <fir5tsi...@yah oo.comwrote:
        On Aug 21, 3:20 pm, rhaazy <rha...@gmail.c omwrote:
        >
        >
        >
        >
        >
        On Aug 21, 10:58 am, Curious <fir5tsi...@yah oo.comwrote:
        >
        I got an exception when executing the SQL statement in my code below.
        The error is "Invalid object name
        'db_dynamic_tra ding.dbo.TrdRpt _broker_list'".
        >
        I know for a fact that 'db_dynamic_tra ding.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 sConnectionStri ng = "Integrated
        Security=SSPI;I nitial Catalog=db_dyna mic_trading;Dat a
        Source=PANSQLDE V";
                    SqlConnection objConn = new
        SqlConnection(s ConnectionStrin g);
                    objConn.Open();
                    string sSQL = "SELECT DISTINCT symbol_requeste d " +
                                  "FROM
        db_dynamic_trad ing.dbo.TrdRpt_ broker_list " +
                                  "WHERE symbol_requeste d LIKE '%SCC%'";
                    SqlCommand objCmd = new SqlCommand(sSQL , objConn);
        >
                    try
                    {
                        objCmd.ExecuteN onQuery();
                    }
                    catch (System.Excepti on e)
                    {
                        Console.WriteLi ne(e.Message);
                    }
                    Console.WriteLi ne("Records selected");
                }
        >
        'db_dynamic_tra ding.dbo.TrdRpt _broker_list'
        >
        this table name looks funny, are you sure its correct?
        >
        also objCmd.ExecuteN onQuery(); 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("som e SELECT statement here", con);
                        cmd.CommandType = CommandType.Tex t;
                        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_tr ading.dbo.TrdRp t_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_requeste d " +
        "FROM
        dbo.TrdRpt_brok er_list " +
        "WHERE symbol_requeste d LIKE '%SCC%'";

        Comment

        • Curious

          #5
          Re: Invalid object name

          Got it!

          Comment

          Working...