Drop orphan users – internal.object.permissions

I did a database restore from development to staging. Couldn’t connect to database using user login, Adam. Selected General tab on properties by clicking on Database->Security->User->Adam. The User type is “SQL user without login”. It seems restore process didn’t connect to the logins.

I try to drop the user;

DROP USER [qsadb]
GO

I keep getting this error;

  • Msg 208, Level 16, State 1, Procedure ddl_cleanup_object_permissions, Line 8 [Batch Start Line 2]
  • Invalid object name ‘internal.object_permissions’.

So, this is an orphan user and issue is with permissions. This needs to be fixed. But how?

Here are the steps that I have used;

The problem is that the user in the database is an “orphan”. This means that there is no login id or password associated with the user. This is true even if there is a login id that matches the user, since there is a GUID (called a SID in Microsoft-language) that has to match as well.

This used to be a pain to fix, but currently (SQL Server 2000, SP3 and up) there is a stored procedure that does the heavy lifting.

All of these instructions should be done as a database admin, with the restored database selected.

First, make sure that this is the problem. This will lists the orphaned users:

EXEC sp_change_users_login 'Report'

If you already have a login id and password for this user, fix it by doing:

EXEC sp_change_users_login 'AUTO_FIX', 'user'

If you want to create a new login id and password for this user, fix it by doing:

EXEC sp_change_users_login 'AUTO_FIX', 'user', 'login', 'password'

jQuery Ajax – tips

jQuery Ajax is async by nature. We use to set a flag “async:false” if we need to make Ajax call sync (non-blocking). This feature has been deprecated. Here is a compile list of Ajax using jQuery;

Alternative to “async: false” for successive AJAX calls

Handling Sequential AJAX Calls using jQuery

Multiple Simultaneous Ajax Requests (with one callback) in jQuery

jQuery callback for multiple ajax calls

jQuery.when understanding

jQuery Promises – Taking action .when() multiple ajax calls are complete

Dapper – ORM example list

I am using EF for most of my ORM and data access. Recently I have tries Dapper and started liking it because of its simplicity and small footprint. Here is a list of example;

Using Dapper to fill a dataset;

Dapper returns a IDataReader when we use the ExecuteReaderAsync method. More information on this addition can be found here and here.

Use Nuget package to add Dapper. Add this to your Main class for quick demo;

static IDbConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString);

Here you go with DataSet example using Dapper;

public async Task<DataSet> GetUserInformationOnUserId(int UserId)
{
    var storedprocedure = "usp_getUserInformation";
    var param = new DynamicParameters();
    param.Add("@userId", UserId);
    var list = await SqlMapper.ExecuteReaderAsync(dbConn, storedprocedure, param, commandType: CommandType.StoredProcedure);
    var dataset = ConvertDataReaderToDataSet(list);
    return dataset;
}

Here is conversion to dataset method;

public DataSet ConvertDataReaderToDataSet(IDataReader data)
{
    DataSet ds = new DataSet();
    int i = 0;
    while (!data.IsClosed)
    {
        ds.Tables.Add("Table" + (i + 1));
        ds.EnforceConstraints = false;
        ds.Tables[i].Load(data);
        i++;
    }                    
    return ds;
}

How to map class names to class properties with dapper

Manually map column name with class properties

Does Dapper supports .NET DataSets

Get base url in javascript

If you are constructing your URL like this;

message += '<br><a href=/Project/GetFileData?documentId=' + response.Data.vm.DocumentId + '>' + response.Data.vm.FileName + '</a>';

You will get results like this;

https://www.foo.com/Project/GetFileData?1234

This will not work on a web site that has following URL;

https://www.foo.com/demo/Project/GetFileData?1234

To fix this problem in ASP.NET Core, use this;

var url = '@Url.Content("~");
message += '<br><a href=' + url + '/Project/GetFileData?documentId=' + response.Data.vm.DocumentId + '>' + response.Data.vm.FileName + '</a>';

Other useful function to find URL are;

var url = window.location.origin;
var host = window.location.host;
var pathArray = window.location.pathname.split( '/' );