datadirectory incorrect in nested application - asp.net

I have used the |DataDirectory| value in a connection string in web.config. It works fine on my local machine.
However it fails when the site is uploaded. The uploaded version of my site is a sub-directory / sub-application of another site. The |datadirectory| value does not reference my App_Data folder, but a folder on the hosting computer.
Is there a way around this? I want to avoid having 2 different web.config files.

Did you check that SQL Express is supported at your host? If not ask their support team. It could also be that you need to attach your .mdf file, after uploading it to the server, to a full blown SQL Server. Again ask your hosting company. Also you should receive from them the correct connectionstring. Also find out more about Connection strings.
Grz, Kris.

Related

Can not access SQL Server database information

I am new to SQL Server databases. I used a SQL Server database in my website (I add it to my project by add new Item and now it is located in a App_Data folder). I can show the needed information of it in a gridview on my localhost but when I upload it on the web hosting server, I can not access it.
What is wrong with it? You can see it here: http://www.kaj.somee.com/SL.aspx
I'm using asp.net 2010 and vb.net
There is the error. See your excerpt (per comment). Do you really have your app_data folder in path
AttachDbFilename=C:\Users\Javad\Desktop\Sharj\App_Data\ShDB.‌​mdf
Does this path really exist in your hosting machine. I believe NO and in that case Change that path accordingly.

Access to the path 'C:\Sites\content\ServerIpAddress.txt' is denied

I create a web site that in Default web page of it i try to access to ServerIpAddress.txt file that is on server . now when i run my project on local host it work correctly , but when I upload it on Server(free host) i face with
error. can some one help me ?
You most likely do not have permissions access files on the shared server due to security issues. Consider moving data from that file to web.config app key.
or access the file as a relative path to your site. The remote sever will most likely not have the same folder structure as your local machine.
The account that is being used for the worker process of the web site has no (write)access to the file.
Configure the file (or folder) so that the ASP.NET process has access.
Another thing to look at is whether or not the path as stated in the code is the actual path on the server.
give full permission to ServerIpAddress.txt file. it will work.
Right click on content folder. Click Properties. Then from General tab uncheck the Read-only checkbox.
Then go to Security tab and Allow Write permission for your ASP.NET process.
Are you using a database? In that case how did you set up permissions for app_data folder? You have to set permissions in the same way.
If you are not able to do that, try moving the file inside of app_data folder and then check.
Right click on .txt file and select security and add network service .restart iis and try.It will work.

asp.net/vb.nrt select local web.config file only

I am deploying an asp.net solution written in vb.net, the solution works fine on the localhost server and on my development server. On the live server the solution keeps ignoring the local web.config file and is using one belonging to another persons project which I have no control over.
This is the list of files that are in the folder I uploaded to the live server:
imageFolder
Default.htm
FormDelete.aspx
FormDelete.aspx.vb
thankyou.htm
web.config
I want my solution to only look at the web.config located in the same folder as it, and not inherit any values from another web.config file. Can this be done?
Thank you in advanced
This happened to me some days ago; as I was the owner of them both, I deleted the app I didn't need, and restarted IIS.
Maybe there are some similarities between your app and the other one, or there is some bad redirection, I don't know. In my case, the other app just linked to a test database, that's how I knew there was an issue.

"An attempt to attach an auto-named database for file *FileName* failed." error after moving from ASP.NET Dev Server to IIS

My ASP.NET project has an MDF database in App_Data folder.
The connection string is:
Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;Integrated
Security=True;User Instance=True
It worked fine until I changed the project parameters to use "Local IIS Web server" instead of "Visual Studio Development Server".
The project now produces the following SqlException:
An attempt to attach an auto-named database for file
C:\Users\Admin\documents\visual studio
2010\Projects\SL\SL\App_Data\MainDatabase.mdf
failed. A database with the same name exists, or specified file cannot
be opened, or it is located on UNC share.
How to modify the connection string to make it work with IIS?
You'll probably find this is file permissions (the error does state: "or specified file cannot be opened"). IIS runs as a limited-user by default, and it's unlikely to have access to the Administrators home directory.
Your three options are:
Change the identity of the application pool in IIS to run as a user that
does have access to that folder
Move the MDF file to a location that
IIS can access (and then check the permissions of the file)
Change the permissions of the file (and possibly the directories above it) so that IIS can access the file.
Of these, number 2 is the most desirable. The other two reduce security somewhat - the first, by giving IIS free reign over a lot of the file system (and potentially system resources as well); whilst the third has the potential to open up a user's home directory to IIS.
Since you are using Integrated Security, the user connecting has to map to a SQL Server login. When you changed to IIS, you switched the user and it may not map to a login in the DB. You could test this by specifying a username and password that are valid in your database.
For example:
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;User Id=admin;Password=password;User Instance=True
Make sure the user account your IIS process is running as has correct permissions to that file.

Get db path outside of web app folder on server using Asp.Net

I published a web app to a server and I would like to access a database file (.mdb) that is also stored on the server in a different location outside of mine. How can I do this in the web.config file of my Asp.Net app?
<add name="MyOleDbConnection" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source= **{?}** \inventory.mdb"/>
I don't believe you can do it from code, using anything like Server.MapPath() - and for good reason. This would allow developers to write apps that take over host operating systems on cloud servers. You're restricted from determining the path of files/folders outside of the control of IIS to prevent directory traversal attacks.
You'll need to ask a SysAdmin, or someone who has access what the path is on that machine. Once you have the full path, you can set the full path in the .config
c:\path\inventory.mdb
instead of
\inventory.mdb
You will also need to make sure that the SysAdmin grants you permissions to access the .mdb.

Resources