简体   繁体   中英

Invalid object name 'TableName'

I am a newbie in sql server and I was unable to resolve this issue. I have been searching for 2 days and I am completely lost.

This is the error I am getting when the page is loaded.

Invalid object name 'TableName'

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'TableName'.

This is my code

private void BindListView()
{
    string ConString = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    using (SqlConnection SConnection = new SqlConnection(ConString))
    {
        using (SqlCommand SCommand = new SqlCommand())
        {
            SCommand.CommandText = "SELECT * FROM TableName";
            SCommand.Connection = SConnection;
            using (SqlDataAdapter SDAdapter = new SqlDataAdapter(SCommand))
            {
                DataTable DTable = new DataTable();
                SDAdapter.Fill(DTable);      //This is the line where i am getting error
                NewsList.DataSource = DTable;
                NewsList.DataBind();
            }
        }
    }
}

This is my connection string.

<add name="ApplicationServices"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
     providerName="System.Data.SqlClient"/>

The code is working fine on my computer with localhost. But when the website is hosted its not working.

On the host I found this connection string for the database, and I replaced the above with this in web.config file. But I am getting the error above

<add name="ApplicationServices"
     connectionString="Data Source=.\MSSQLSERVER2014;Integrated Security=True;User ID=user;Password=password;Connect Timeout=15;Encrypt=False;Packet Size=4096"
     providerName="System.Data.SqlClient"/>

I think the problem is connecting with the database and I don't know what to do.

Please help

Your local host connection string is pointing to the a file database that you will need to either attach or move to the server so you can utilize that database file.

My suggestion would be to attach that database file to your local sqlexpress server and then you can move the database to the hosting server when you need to publish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM