Search code examples
c#asp.netsql-servervisual-studioaspxgridview

Able to connect to SQLEXPRESS2014, but getting Invalid object name error from Visual Studio


I am trying to connect to connect to SQLEXPRESS2014 from Visual Studio webforms application. Looks like the connection is being made, but i am getting Invalid object name 'HumanResources.Employee'.

System.Data.SqlClient.SqlException: Invalid object name 'HumanResources.Employee'.

When i query the databse in Management Studio, it returns the result.

I am using AdventureWorks2012 database. I use this datasource to bind to a Gridview.

aspx page

<asp:sqldatasource id="CustomersSource"
    selectcommand="Select [BusinessEntityID], [NationalIDNumber], [LoginID] 
    From [HumanResources.Employee]"
    connectionstring="<%$ ConnectionStrings:AdvWorksConnection%>" 
    runat="server"/>

Connectionstring

    <connectionStrings>
    <add name="AdvWorksConnection" connectionString="data source=localhost\SQLEXPRESS2014;
   initial catalog=AdventureWorks2012;persist security info=True; 
   Integrated Security=True;" 
   providerName="System.Data.SqlClient" />
  </connectionStrings>

Can you tell me where i am doing wrong. Is something wrong with the connection string.


Solution

  • I believe you want this:

    <asp:sqldatasource id="CustomersSource"
        selectcommand="Select [BusinessEntityID], [NationalIDNumber], [LoginID] 
        From [AdventureWorks2012].[HumanResources].[Employee]"
        connectionstring="<%$ ConnectionStrings:AdvWorksConnection%>" 
        runat="server"/>
    

    Whereas, AdventureWorks2012 is the database, HumanResources is the schema, and Employee is the table.

    NB: You could remove [AdventureWorks2012]. and the query should still execute, as your connection string explicitly says to use the AdventureWorks2012 database.