"Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection"

I wrote a SQL connection program. It works in connecting to the SQL server in the console application program. But when I copied the same code to a web form under button_click event handling, system reported:
"Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection"

Why it works in C# console application while not working in the asp.net web form? They use the same SqlConnection:
new SqlConnection("Initial Catalog=DataMasters; Data Source=Abcdefg ;Integrated Security=SSPI");

And the security has been set up as: Authentication: SQL Server and Winows.
On the startup service account (start and run sql server in the following account) (in security configuration), system administrator selected an account name and a password already though (instead of using system account).

thanks a million,
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

do you have Identity Ipersonate=true in your web.config?

<identity impersonate="true" />
what account is it running under in IIS?  
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

That is the account that will need to have a sql server account to access the db
Is there an ASPNET account set up for the database?

You may either need to set up an account, or configure credentials to be sent properly accross the network (I believe the ASP.NET worker process only has anonymous access to network resources by default).

try this

new SqlConnection("Initial Catalog=DataMasters;uid=sa;pwd=; Data Source=Abcdefg ;Integrated Security=SSPI");

heyday2004Author Commented:
Thanks. Here are some parts that I have doubt with in my web.config:
<authentication mode="Windows" />
<sessionState mode="InProc" stateConnectionString="tcpip=" sqlConnectionString="data source=;Trusted_Connection=yes" cookieless="false" timeout="20" />

When I added <identity impersonate="true" />, there was another error:
Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
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: Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.

Also, how to check what account is running under IIS?

ok you need to create an Active Directory Windows account for the application to run under or use an existing one.
Go to your IIS manager and click on the site properites and then look under the directory security tab.  There it wil let you choose
the account the application runs under set it to your desired account with the sql server account and all will be well.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
don't worry about that top stuff its fine.
currently its running with anonymous access as the default process.  That is why the username is null, and you are being denied.
1. Create a SQL Server user using SQL Server Enterprise Manager. Make sure that your SQL server supports both the Windows and SQL Authentication.

2. Use the follwoing connection string:

Data Source=,1433;Network Library=DBMSSOCN;Initial Catalog=northwind;User ID=SQL_USER;Password=SQL_PASSWORD;"

In your console application, you didnt have any problem as the application was accessing the SQL server under the privileges of user account that you are logged in. However, in asp.net web application, ASPNET user account do not have high privileges and if you do not provide the user account info in the connection string, it tries to access the server using ASPNET account that by default do not have permission to access the server.

HTH, Nauman.
Hi heyday2004,

the connection is open under the account it is executable. when you create a web site the defaul settings are - enabled for the anonimous user
when you use Windows Authentication you can not retrieve ( .net also :) ) which is the current user that is logged into the machine
so when you try to connect to the SQL Server .net pass (null ) user

to remove that you have to remove anonymus access for your web site
1. Open Internet Information Service console
2. RIght click on your virtual directory/web site
3. go to Directory Security Tab
4. Click on Edit button in the  Authentication and access control
5. uncheck the checkbox Enable Anonymous Access

heyday2004Author Commented:
Thanks  a lot, all! I created a account and solved the problem. Thanks again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.