Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. "Invalid object name" SqlException

"Invalid object name" SqlException

Scheduled Pinned Locked Moved Database
databasehelpcsharpsharepointsql-server
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    patzerFish
    wrote on last edited by
    #1

    Here's what I'm trying to do, using C#: 1 - Connect to SQL Server 2 - Check if a database is present. 3 - Create the database if it isn't. 4 - Create a login/user pair for that database. 5 - Store the connection string for future use. I have this done, confirmed by using SQL Server Management Studio Express. The following method throws an SqlException:

        private static SqlDataReader executeQuery(String query) {
            SqlCommand command = new SqlCommand(query, Connection);
            SqlDataReader reader;
            try {
                openConnection();
                reader = command.ExecuteReader();
                return reader;
            } catch {
                throw;
            } finally {
                //connection left open because the reader is worked on elsewhere.
            }
        }
    

    The query: SELECT ReportGroupName FROM dbo.ReportGroups The error message: Invalid object name 'dbo.ReportGroups'. The offending line of code: reader = command.ExecuteReader(); Some lines from the script I used to create the database, tables and user:

    CREATE LOGIN [bim] WITH PASSWORD=N'*******', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
    EXEC sys.sp_addsrvrolemember @loginame = N'bim', @rolename = N'dbcreator'
    CREATE USER [bim] FOR LOGIN [bim] WITH DEFAULT_SCHEMA=[dbo]
    CREATE TABLE [dbo].[ReportGroups]

    I have a feeling that the problem lies with how I setup the default user, since I'm still learning databases, and it's been pretty hit or miss. However, the application worked previously using the above method with the same query string - the exception has only occurred recently, after creating the database from a script rather than in using the visual editors in SQL Server Management Studio. The script I used to create the database was derived from exports of the original database and tables, with only modifications made to create a login and a user. Any assistance on tracking down how to fix this error would be greatly appreciated.

    P 1 Reply Last reply
    0
    • P patzerFish

      Here's what I'm trying to do, using C#: 1 - Connect to SQL Server 2 - Check if a database is present. 3 - Create the database if it isn't. 4 - Create a login/user pair for that database. 5 - Store the connection string for future use. I have this done, confirmed by using SQL Server Management Studio Express. The following method throws an SqlException:

          private static SqlDataReader executeQuery(String query) {
              SqlCommand command = new SqlCommand(query, Connection);
              SqlDataReader reader;
              try {
                  openConnection();
                  reader = command.ExecuteReader();
                  return reader;
              } catch {
                  throw;
              } finally {
                  //connection left open because the reader is worked on elsewhere.
              }
          }
      

      The query: SELECT ReportGroupName FROM dbo.ReportGroups The error message: Invalid object name 'dbo.ReportGroups'. The offending line of code: reader = command.ExecuteReader(); Some lines from the script I used to create the database, tables and user:

      CREATE LOGIN [bim] WITH PASSWORD=N'*******', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
      EXEC sys.sp_addsrvrolemember @loginame = N'bim', @rolename = N'dbcreator'
      CREATE USER [bim] FOR LOGIN [bim] WITH DEFAULT_SCHEMA=[dbo]
      CREATE TABLE [dbo].[ReportGroups]

      I have a feeling that the problem lies with how I setup the default user, since I'm still learning databases, and it's been pretty hit or miss. However, the application worked previously using the above method with the same query string - the exception has only occurred recently, after creating the database from a script rather than in using the visual editors in SQL Server Management Studio. The script I used to create the database was derived from exports of the original database and tables, with only modifications made to create a login and a user. Any assistance on tracking down how to fix this error would be greatly appreciated.

      P Offline
      P Offline
      patzerFish
      wrote on last edited by
      #2

      Hrm. It appears that I wasn't setting InitialCatalog when building the connection string, at least not in the right place. You don't have to have an initial catalog when testing a connection to a database, which is code near what I'm doing, but you do have to have it to access database tables. I was worried that it was something simple - turns out it was. Upon reflection, I'm kind of glad.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups