# ASP.NET question - error accessing SQL Server db from SharePoint

**URL:** https://boards.straightdope.com/t/asp-net-question-error-accessing-sql-server-db-from-sharepoint/525070
**Category:** Factual Questions
**Created:** [January 15, 2010, 2:30pm UTC](https://boards.straightdope.com/t/asp-net-question-error-accessing-sql-server-db-from-sharepoint/525070 "2010-01-15T14:30:48Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Roland\_Orzabal](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@Roland\_Orzabal](https://boards.straightdope.com/u/Roland_Orzabal)
#### Post date: [January 15, 2010, 2:30pm UTC](https://boards.straightdope.com/t/asp-net-question-error-accessing-sql-server-db-from-sharepoint/525070/1 "2010-01-15T14:30:48Z")

</div>

Bit of an overspecific question, I know, but I’m not finding any help anywhere else, so I’m hoping the Dope comes through for me.

I’m developing an [ASP.NET](http://ASP.NET) user control in VS2008 that writes to a SQL Server 2005 database. I’ve published the control to a SharePoint 2007 MOSS page using the SmartPart add-in. The control consists of a few text fields and a submit button that writes the contents of the text fields to the database. When I test the control in the Visual Studio debugger, it works fine. When I try to submit data from the SharePoint page, I get the following error:

> [@](#):
>
> ## Server Error in ‘/’ Application.
> 
> Object reference not set to an instance of an object.  
> 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.NullReferenceException: Object reference not set to an instance of an object.
> 
> Source Error:
> 
> An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
> 
> Stack Trace:
> 
> [NullReferenceException: Object reference not set to an instance of an object.]  
> ConnectionHandler.ClientDataConnect() +103  
> ClientData.ClientData.Page\_Load(Object sender, EventArgs e) +1426  
> System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +24  
> System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +41  
> System.Web.UI.Control.OnLoad(EventArgs e) +131  
> System.Web.UI.Control.LoadRecursive() +65  
> System.Web.UI.Control.LoadRecursive() +190  
> System.Web.UI.Control.LoadRecursive() +190  
> System.Web.UI.Control.LoadRecursive() +190  
> System.Web.UI.Control.LoadRecursive() +190  
> System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2427
> 
> * * *
> 
> Version Information: Microsoft .NET Framework Version:2.0.50727.3053; [ASP.NET](http://ASP.NET) Version:2.0.50727.3053

Last time I saw this, it was a database permissions error, but I’ve granted my site’s default application pool read/write access to my database.

Does anyone have any idea what else the problem might be? Also, how can I double-check the credentials that the SharePoint page is using to access the database, since it looks for all the world like a permissions issue? Any advice is appreciated, thanks.

---

<div class="post-metadata">

### Author: ![Dervorin](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dervorin](https://boards.straightdope.com/u/Dervorin)
#### Post date: [January 15, 2010, 4:00pm UTC](https://boards.straightdope.com/t/asp-net-question-error-accessing-sql-server-db-from-sharepoint/525070/2 "2010-01-15T16:00:07Z")

</div>

I’d check several things:

1. Authentication in IIS
2. Check that the app pool you _think_ is running the SharePoint site is actually running it
3. Check that the correct impersonation (if any) is being used

> [@](#):
>
> Does anyone have any idea what else the problem might be? Also, how can I double-check the credentials that the SharePoint page is using to access the database, since it looks for all the world like a permissions issue?

One way to check what’s actually hitting the database is to run a trace against the server using SQL Server Profiler, which will give you details on queries and stored procs that are being run, including which user is running them. A quick overview of Profiler for 2005 is here: [http://www.codeproject.com/KB/dotnet/SQLServerProfiler.aspx](http://www.codeproject.com/KB/dotnet/SQLServerProfiler.aspx)  
Video tutorial here: [http://sqlserverpedia.com/wiki/Using\_SQL\_Server\_Profiler](http://sqlserverpedia.com/wiki/Using_SQL_Server_Profiler)

---

<div class="post-metadata">

### Author: ![Roland\_Orzabal](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@Roland\_Orzabal](https://boards.straightdope.com/u/Roland_Orzabal)
#### Post date: [January 15, 2010, 4:37pm UTC](https://boards.straightdope.com/t/asp-net-question-error-accessing-sql-server-db-from-sharepoint/525070/3 "2010-01-15T16:37:32Z")

</div>

Well, I’m one step closer, at any rate. I realized that this control is using a connection string in web.config, which isn’t copied over when you implement a control using SmartPart…all you do is copy the published ascx file, and throw the dll in the site’s bin folder. I went back and hard-coded the connection string, and now I’m getting a new error. Progress! 😃

Specifically, I’m getting “Login failed for user ‘NT AUTHORITY\ANONYMOUS LOGON’.”. I thought that might mean that the control wasn’t passing any credentials (app pool or otherwise) to SQL Server, but even when I specify a username and password in my connection string, I get the same error.

---

<div class="post-metadata">

### Author: ![Dervorin](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dervorin](https://boards.straightdope.com/u/Dervorin)
#### Post date: [January 15, 2010, 5:00pm UTC](https://boards.straightdope.com/t/asp-net-question-error-accessing-sql-server-db-from-sharepoint/525070/4 "2010-01-15T17:00:09Z")

</div>

> [@Roland\_Orzabal](#):
>
> Well, I’m one step closer, at any rate. I realized that this control is using a connection string in web.config, which isn’t copied over when you implement a control using SmartPart…all you do is copy the published ascx file, and throw the dll in the site’s bin folder. I went back and hard-coded the connection string, and now I’m getting a new error. Progress! 😃
> 
> Specifically, I’m getting “Login failed for user ‘NT AUTHORITY\ANONYMOUS LOGON’.”. I thought that might mean that the control wasn’t passing any credentials (app pool or otherwise) to SQL Server, but even when I specify a username and password in my connection string, I get the same error.

Can you post the connection string you’re using? (With sensitive info stripped out, obviously).

Actually, if you can copy and paste the whole of the web.config file, that might help eliminate some possibilities.

---

<div class="post-metadata">

### Author: ![Athena](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Athena](https://boards.straightdope.com/u/Athena)
#### Post date: [January 15, 2010, 5:29pm UTC](https://boards.straightdope.com/t/asp-net-question-error-accessing-sql-server-db-from-sharepoint/525070/5 "2010-01-15T17:29:01Z")

</div>

> [@Roland\_Orzabal](#):
>
> Specifically, I’m getting “Login failed for user ‘NT AUTHORITY\ANONYMOUS LOGON’.”. I thought that might mean that the control wasn’t passing any credentials (app pool or otherwise) to SQL Server, but even when I specify a username and password in my connection string, I get the same error.

Are you setting “Trusted\_Connection” to false in the connection string when you add the username/password? The error you post indicates that it’s using Windows Authentication to get to the DB - if you explicitly set the username/password in the connection string, that’s SQL Security, not Windows authentication, but it will ignore it if Trusted\_Connection is set to true.

---

<div class="post-metadata">

### Author: ![Roland\_Orzabal](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@Roland\_Orzabal](https://boards.straightdope.com/u/Roland_Orzabal)
#### Post date: [January 15, 2010, 5:44pm UTC](https://boards.straightdope.com/t/asp-net-question-error-accessing-sql-server-db-from-sharepoint/525070/6 "2010-01-15T17:44:59Z")

</div>

Actually, the web.config isn’t copied over when the control is deployed via SmartPart. That was the initial problem I was having; the control couldn’t find the connection string. Currently, it’s hard-coded into the app in a class I named ConnectionHandler.

Setting Integrated Security to “false” in the connection string fixed the “NT AUTHORITY\ANONYMOUS LOGON” bit, but now I’m getting "Login failed for user ‘domain\username’, when I’m certain that the credentials I’m passing have access to the database.

The connection string itself is: @“Data Source=servername;Initial Catalog=databasename;Integrated Security=False;Username=domain\username;Password=password”

ETA: Now that I think about it, the username and password I’m putting in are for an Active Directory login that has rights within SQL Server. Do I need to somehow recreate the login in SQL Server itself in order to use it without Integrated Security?

---

<div class="post-metadata">

### Author: ![Roland\_Orzabal](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@Roland\_Orzabal](https://boards.straightdope.com/u/Roland_Orzabal)
#### Post date: [January 15, 2010, 5:54pm UTC](https://boards.straightdope.com/t/asp-net-question-error-accessing-sql-server-db-from-sharepoint/525070/7 "2010-01-15T17:54:40Z")

</div>

As an added note, the only thing I really care about here is getting this app access to the database ASAP. At this point, I’m willing to do pretty much anything that gets this app up and running, so I’m open to any left-field suggestions that will accomplish that.

---

<div class="post-metadata">

### Author: ![Roland\_Orzabal](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@Roland\_Orzabal](https://boards.straightdope.com/u/Roland_Orzabal)
#### Post date: [January 15, 2010, 6:39pm UTC](https://boards.straightdope.com/t/asp-net-question-error-accessing-sql-server-db-from-sharepoint/525070/8 "2010-01-15T18:39:37Z")

</div>

Ok, got it. Either AD logins don’t work when you turn off integrated security, or else I don’t know how to pass them properly, but either way, creating a new user with SQL Server authentication did the trick. Thanks to everyone for your responses.

---

<div class="post-metadata">

### Author: ![Athena](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Athena](https://boards.straightdope.com/u/Athena)
#### Post date: [January 15, 2010, 6:41pm UTC](https://boards.straightdope.com/t/asp-net-question-error-accessing-sql-server-db-from-sharepoint/525070/9 "2010-01-15T18:41:02Z")

</div>

> [@Roland\_Orzabal](#):
>
> ETA: Now that I think about it, the username and password I’m putting in are for an Active Directory login that has rights within SQL Server. Do I need to somehow recreate the login in SQL Server itself in order to use it without Integrated Security?

Yes, the username and password in the connection refer to the SQL Server security user, not anything to do with active directory.

You’ll need to create that user within SQL Server. I use the SQL Management Studio to do it. There’s a “Security” tab, you can add users easily enough there. Give it permissions to the appropriate database.

I then usually test the connection by clicking “New Query” in the top left corner of the Management Studio window. A dialog will popup, choose SQL Server Authentication from the Authentication drop down, and type in your username/password. If that works, make sure your .NET connection string username/password is set the same, and you should be good to go.

Edit: Oops, I was typing this while you added your last reply. Glad to hear you got it working!
