A network-related or instance-specific error occurred while establishing a connection to SQL Server. Error Locating Server/Instance Specified)

Moving a web project from test enviroment to the web server made this error occur whenever tehre’s an attempt to open a connection to the sql server.

We can insert and read data from sql server management studio.

We’re suspecting that the error is created from a bad connection string or from the sql server using integrated security, but haven’t been able to confirm that.

the connection string.

<connectionStrings>
<add name="DSVUShort" connectionString="data source=localhostSQLEXPRESS;database=NS_Survey; integrated security=true;multipleactiveresultsets=True;App=EntityFramework" />
</connectionStrings>

where it is used

SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DSVUShort"].ToString());
Connection.Open() // pops the error

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

causes this error is connectionString in webConfig
Set connectionString in webConfig.
name of connectionStrin in webConfig and code behind must be same.

in web.config:

<configuration>
<connectionStrings>
    <add name="DSVUShort" connectionString="Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
    <compilation debug="true" targetFramework="4.0"/>
</system.web>

in code:

SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DSVUShort"].ToString());  
Connection.Open();

enter image description here


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x