views:

218

answers:

5

I have a project I am trying to move to run on IIS7 instead of the Visual Studio Development Server. Everything works on VS Development Server but when moving to IIS7 its seems to not be able to read anything from the database, but doesn't give a database error, instead it gives a NullReferenceException on the information I'm trying to get from the database. The project uses Linq to get information from the DB if that matters. Also, here is my connection string. I have tried messing with it to no avail.

     <connectionStrings>
       <add name="MyProjectConnectionString" connectionString="DataSource=MY-PC\SQLEXPRESS;Initial Catalog=MyProjectsDB;Integrated Security=True" providerName="System.Data.SqlClient"/>
     </connectionStrings>

System Information: IIS7 running on Vista Business, SQL Server 2008 Express, and it works fine with Visual Studio Development Server and SQL Server 2008. Thanks for any input!

A: 

Make sure the user IIS is running as has permission to connect to the DB. Check the Application event log to see if there are any 'unable to connect' events. I suspect your NullReferenceException is actually an attempt to use a failed SQL connection.

n8wrl
Hi, I am thinking that the NullReference is a DB connection error as well. Where is my application's event log? I have not set that up and do not know where the default one is.
Ryan
A: 

It's a security issue. You can try using a SQL server user/password or give the IIS user access to that database. The IIS user probably is NETWORK SERVICE

Cody C
I strongly recommend creating an application username/password however. This will make it much easier. Then your connection string would be DataSource=MY-PC\SQLEXPRESS;Initial Catalog=MyProjectsDB;User id=xxx; Password=yyy
Cody C
Hi, when you say application username/password do you mean for the database? And then use SQL Server Authentication rather than Windows Authentication? Thanks!
Ryan
No - he means creating a new Windows account, run your IIS app as that user (anonymous authentication) and giving that user limited access to your SQL DB. That's what we do and it's a HUGE step in reducing your SQL injection exposure.
n8wrl
A: 

Change the data source so that it points to the server, not your own computer, or simply use ".\SQLEXPRESS" as data source.

To use integrated security, you may need to add the network account as a user in the databse. Alternatively you can use SQL Server security instead, specifying a user name and password for a user that you set up in the database. At connectionstrings.com you can find examples on how to write different connection strings.

The fact that you are getting a null reference exception suggests that you are catching the exception in your code and just ignoring it. If you do, that is a very bad practice. Errors may occur without you even noticing, and you don't get any clue on what's wrong if you notice the error.

Guffa
A: 

You can do one of these:

  1. Change the App Pool identity to a user that has SQL rights
  2. Change to SQL authentication (username + password)
  3. Add the domain\webserver$ machine account to SQL eg BOBSGARAGE\WEBSERVER1$
  4. Enable pass through authentication so the credentials are passed from browser to SQL Server
  5. Host SQL and IIS on the same server (bad idea)
gbn
A: 

From your connection string "connectionString="DataSource=MY-PC\SQLEXPRESS;Initial Catalog=MyProjectsDB;Integrated Security=True" it looks like you're going to have trouble with the integrated Security setting. When you're moving a database from local to some other place the aspnet user will usually not have direct access to the database as it would on a local machine.

On the server you're moving to, make sure that you have mixed mode set on the SQL Server itself, create a user and password on SQL Server for the database that you're attempting to connect to and make sure that they have the apporiate levels of persmissions to add, edit, delete, etc.

Now go back to your web.config's connection string and change the connection string to have the following: connectionString="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;".

Good luck with your project.

Chris