24

I have been working away for the last 7 months on a C# ASP.NET using Visual Studio 2008 and SQL Server 2008.

Today, I was running part of my application which was previously running and I got the following error:

The SELECT permission was denied on the object 'Address', database 'CNET_85731', schema 'dbo'.

I walked through my code and discovered that this error was being caused in the following User Control:

protected void sdsAddressesList_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    if (e.AffectedRows == 0)
    {
        ddlAddresses.Items.Insert((0), new ListItem("No Billing Addresses", "0"));
    }
}

the SQLDataSource is defined as follows:

<asp:SqlDataSource ID="sdsAddressesList" runat="server" OnSelecting="sdsAddressesList_Selecting" OnSelected="sdsAddressesList_Selected"
    SelectCommand="SELECT [AddressId], [ZipPostalCode], ZipPostalCode + '&nbsp;--&nbsp;' + Address1 AS CombinedAddress FROM [Address] WHERE ([CustomerID] = @CustomerID AND [IsBillingAddress] = @IsBillingAddress) ORDER BY [ZipPostalCode]"
    ConnectionString="<%$ ConnectionStrings:eCoSysConnection %>">
    <SelectParameters>
        <asp:Parameter Name="CustomerID" Type="Int32" />
        <asp:Parameter Name="IsBillingAddress" Type="Boolean" />
    </SelectParameters>
</asp:SqlDataSource>

Basically, what the control does is retrieve a list of addresses for the logged on user from the [Address] table and then populate the drop down list ddlAddresses.

The Address table has all the same permissions as the rest of the tables in the database. I have around 60 tables and approximately 200 stored procedures all merrily working away doing SELECTs, etc. No problem. Except for this one issue. What on earth is going on? I haven't made any changes to the database or table permissions.

Can anyone help me please.

Regards

Walter

6
  • Is the connection string pointing to the correct database (one which has proper permissions setup)? Commented Jul 28, 2009 at 21:23
  • Yes. the connection string in web.config is as follows: <add name="eCoSysConnection" connectionString="Server=POSITIVECODE-UW\SQLSERVER2008STD;Integrated Security=True;Database=CNET_85731" providerName="System.Data.SqlClient"/> I use this same connection string to access the database at all times. Commented Jul 28, 2009 at 21:25
  • Has anything changed with your windows login that is tied to sql server? Commented Jul 28, 2009 at 21:36
  • If you login to the SQL box via some for of management studio, as SA and/or this user and run the select query manually, what happens?
    – PostMan
    Commented Jul 28, 2009 at 21:51
  • Not that I am aware of shahkalpesh. All other database access is working fine. Commented Jul 28, 2009 at 21:52

6 Answers 6

62

As problem states, "The SELECT permission was denied on the object 'Address', database 'CNET_85731', schema 'dbo' ".

I wonder you can quite simply solve this way:

  • Open SQL Server Management studio
  • Navigate to the database 'CNET_85731' >> Security >> Users
  • Right click on the one which you are using in your code
  • And finally, just select 'db_datareader' inside "Database Role membership" section.

Now, I hope you should not get this error again.

4
  • 7
    Or instead of selecting 'db_datareader'uncheck 'db_denydatareader' Commented Nov 9, 2012 at 10:01
  • 5
    Thank you. Unchecking 'db_denydatareader' and 'db_denydatawriter' worked for me.
    – jiminy
    Commented Apr 1, 2013 at 19:35
  • 1
    No, this did not help in my case. Needed "Grant select to user", look at EPiddys answer. Commented Apr 16, 2014 at 9:31
  • 1
    @CodingBarfield THANK YOU. Why does MS SQL even have deny role memberships? Looks like a troll to me. To screw with people who check all fields without reading. #guilty
    – Chronozoa
    Commented Oct 2, 2014 at 21:25
15

Well I'm not sure what the root cause was for the SELECT permission denied for your db user but if you run this and then it does indeed work again, then somewhere along the line, your SELECT permission was indeed wiped out.

GRANT SELECT ON [dbo].[Address] TO [your user name here]

The good news is that permissions don't magically disappear; the bad news is something (either tooling or otherwise) did indeed either remove or revoke permissions.

I don't think we have enough information to answer your question as to "why" it happened -- although, none of what you posted appears to be the culprit.

2
  • Hi EPiddy, yes, I agree, permissions don't just disappear. I tried the following as suggested: GRANT SELECT ON [dbo].[Address] TO [POSITIVECODE-UW\ASPNET] where 'POSITIVECODE-UW\ASPNET' is the ASPNET account. The command completed successfully on SQL Server, but unfortunately, I still get the same error: The SELECT permission was denied on the object 'Address', database 'CNET_85731', schema 'dbo'. Thanks for your suggestion. Regards Walter Commented Jul 28, 2009 at 21:45
  • I even tried putting the SELECT command in a stored procedure and calling the stored procedure from the SqlDataSource. Nothing. I'm just desperately clutching at straws. Commented Jul 28, 2009 at 22:09
4

In your SQL Server Management Studio, right click on your database, then click permission, then select the user, then grant select,edit,update and delete. Source http://go4answers.webhost4life.com/Example/select-permission-denied-object-159536.aspx

3

Had a quick google, found this link Link

It suggests running

select object_name(major_id) as object,
 user_name(grantee_principal_id) as grantee,
 user_name(grantor_principal_id) as grantor,
 permission_name,
 state_desc
from sys.database_permissions
 where major_id = object_id('Users')
 and class = 1

On your database to see what permissions exist, as you may have a DENY select

Edit

select object_name(major_id) as object,
 user_name(grantee_principal_id) as grantee,
 user_name(grantor_principal_id) as grantor,
 permission_name,
 state_desc
from sys.database_permissions
 WHERE state_desc = 'DENY'

Managed to find a running SQL 2k8 box, and ran it, this new query will show all the deny's. Also try taking the WHERE clause out, to see all the permissions on all tables in the currently selected Database

7
  • Thanks PostMan. I ran this query and it returned 0 rows. I assume that is correct? Commented Jul 28, 2009 at 23:17
  • There are DENYs on the following objects: sp_helpdiagrams sp_helpdiagramdefinition sp_creatediagram sp_renamediagram sp_alterdiagram sp_dropdiagram fn_diagramobjects Commented Jul 28, 2009 at 23:27
  • That's fine, thats what show up for me. If you remove the WHERE clause, waht permissions are there for the table in question
    – PostMan
    Commented Jul 28, 2009 at 23:34
  • Hmm, that's weird that a drop and create didn't work either. If you login as your user to management studio, and run "SELECT * FROM Address" it definitaly returns rows?? I'm also clutching at straws now
    – PostMan
    Commented Jul 28, 2009 at 23:36
  • None of the objects listed are user Tables. They all appear to be system objects. Commented Jul 28, 2009 at 23:38
0

If it gives you that error within SQL server Management Studio then just run the SQL server Management Studio as administrator that should work.

0

Remove the following part of your connection string;

In‌​tegrated Security=True;

And everything works fine for me.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.