0

I added database file to my built in sql server of Visual Studio and i have also connection string of it but i am confuse how to add this connection string into web.config here is my try to add connection string to code below :

<connectionStrings>
   <add name="myFirstConnectionString" connectionString="Data Source= .\SQLEXPRESS;AttachDbFilename="C:\Users\Ahdus\Desktop\First Task\First Task\MyFirstTask.mdf"; Integrated Security=True;Connect Timeout=30;User Instance=TrueproviderName="System.Data.SqlClient" />
  </connectionStrings>

I am much confused that how i can add connection string of my visual studio built in database file to web.config. Please help me as i am new in asp.net and ignore my way of asking.

2 Answers 2

1

To make your connection string work with the path you've given, it should look somewhat like this:

Visual Studio 2012 (SQL Server 2012):

<connectionStrings>
  <add name="myFirstConnectionString" 
       connectionString="Data Source=(LocalDB)\v11.0;Integrated Security=True;AttachDBFilename=&quot;C:\Users\Ahdus\Desktop\First Task\First Task\MyFirstTask.mdf&quot;;Integrated Security=True" 
       providerName="System.Data.SqlClient"/>
</connectionStrings>

Visual Studio 2010 (SQL Server 2010):

<connectionStrings>
  <add name="myFirstConnectionString"
       connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=&quot;C:\Users\Ahdus\Desktop\First Task\First Task\MyFirstTask.mdf&quot;;User Instance=true"
       providerName="System.Data.SqlClient" />
</connectionStrings>

But you should consider moving the database file to the App_Data folder in your project, and then use AttachDBFilename=|DataDirectory|MyFirstTask.mdf instead.

Sign up to request clarification or add additional context in comments.

6 Comments

after setting up according to you i faced this error message A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
@AmmarRaja I have updated the answer, maybe it works better for you now... I'm assuming you are using Visual Studio 2012, is that correct?
I am using VS2010 an now my code is <add name="myFirstConnectionString" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|MyFirstTask.mdf&quot;;" providerName="System.Data.SqlClient" /> but still error while debugging the code because dataadapeter is null...Please help
if you have skype please access my machine and provide help ammar.asjad is my skype name
@AmmarRaja Then it should work if the MyFirstTask.mdf file is in the App_Data folder of the project where the web.config with the cnnectionstring is.
|
0

You should place you .mdf in App_Data folder, then you can use this

<connectionStrings>

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

</connectionStrings>

With C:\Users\Ahdus\Desktop\First Task\First Task\MyFirstTask.mdf, if you move your application to another machine, that path might not exist

|DataDirectory| maps to your application App_Data folder

2 Comments

some one suggest me to add your database file under Data Connection Folder and i did but facing problem... so you suggest me to remove file from there and place it under App_Data ??
Data should be stored in App_Data folder as by default it is protected and web users cannot view files in there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.