SQL Server – Fixing Orphaned Users after restore

I was tasked with migrating a SQL 7 box (yes still heavily used) to SQL 2008.  Really wasn’t that difficult, perhaps I’ll make another post about the process and issues I ran into.  But for now let me talk about something real quick. 

When restoring a database to a new server you may not have your security setup properly.  For example, the database will have users that don’t exist on the server.  Or perhaps they do but they are not linked properly.  This is the issue I ran into. 

Sometimes it’s just as easy to delete the users from the database and recreate them on the server to relink.  This is fine if you have simple permissions.  But if you have a complex set of permissions on a large number of tables, this get’s difficult to recreate properly and of course you always have the potential to create errors in multiple applications that may connect as that user.

Then I found sp_change_users_login, this has saved the day.  This allowed me to fix the orphaned users after the restore.

 

EXEC sp_change_users_login 'Auto_Fix', 'orphaned-user-name'

How do we quickly determine if we have this issue?

EXEC sp_change_users_login 'Report'

This will give us a list of orphaned users.  Very handy indeed.

New Beginning, New Blog

Having recently moved away from my company, Malachi Computer consultants and taking a Director position with Three30 Group, I’ve decided to move my .NET blog away from the malachi site and on to here.  I hope to post more programming related items here and hope to make it a useful resource for myself, my team and others.  I’ll be bringing my posts over slowly (at least my development related ones)so that everything will be on here.  Looking forward to making new friends on here and reconnecting with old.

Review of 2010

This almost seems to be a running joke around my house.  Just about the only time I update my company blog is this end of year review I do.  To me this is at least useful.  I’ve found that putting my thoughts for the previous year down on the proverbial paper and expressing my hopes for the new year help me get focused, at least in the short term, to start my year off right.  So here we go, I’m sure everyone (at least all two of you) are anxiously awaiting to read what I have to write.

Recap of 2010 (last years post)

  • I wanted to honor God each and every day.  While some days are better than others, I don’t think I’ve forgotten where my blessings flow from. 
  • I wanted to become a better father and husband.  Again, I struggle at times to be patient with my children and to honor and love my wife like she deserves, but I can say that they are the most important thing that God has given to me.  This world can take my house, my car and my dog away, but nothing can never take my family from me.  I love each one of my four children more each and every day.  My wife has become my anchor and her strength and love for me gives me the strength and will to move forward each and every day.
  • I mentioned last year that I had a tool idea that I wanted to develop.  To be honest, I don’t even remember what that tool was.  However, we have in development now a  new product that we plan to market towards our manufacturing clients (at least initially) and then expand beyond that down the road.  I hope to have something to announce on that within the next month or so.
  • Last year I read over 40 books.  This year I hit 50 so I’m quite proud of that.  Not as many work related ones as I would have liked, but nonetheless I’m happy with my achievement. 
  • At the beginning of 2010 (as I’ve done every year), I vowed to blog more.  Well that failed miserable as you can tell. 
  • I wanted to become more reliant and move towards becoming debt-free.  Don’t think I’ve made any progress there unfortunately, however circumstances have dictated that I need to focus on that more and more and my wife and I have made a commitment to strive towards those goals and have begun to make progress on that in the last quarter of 2010.
  • Finally, I wanted to work towards expanding and growing Malachi Computer.  While the first half of 2010 moved slow in this regards, we’ve really begun to make great strides in this area and have picked up a few new clients and a number of new projects in the last quarter of 2010.  We have also begun a heavy push in marketing the business and being more proactive in communicating with our existing clients and reaching out to new ones.

Where do we headed in 2011? In the past I’ve mixed personal goals and business goals here on this list.  My personal goals change little from year to year, so aside from the first and primary goal of my life, my personal goals will go unstated here.

  • Again, as every year, my primary goal in life and in business is to honor God in all we do.  Conduct our business and life as if we were working for Jesus Christ.  Make my family a priority in my life, for without them, I want little else.
  • I won’t promise to blog more, but look for a new look to the Malachi Computer website and making the blog an important part of our marketing strategy.
  • I’ve also created a new blog that I’m really enjoying writing for.  Not business or computer related, but a topic of importance to me.  I look to move it from it’s current host and make an effort to have it to reach more people.
  • Continue to strive to market Malachi Computer in a number of ways.  We are solidifying the services we offer and moving to be more proactive in marketing those services to new and existing clients.  Our focus this year (and really the last quarter of 2010) was to become the solution provider for our customers.  Our customers have problems or inefficiencies and we want to be there to provide a solution for them.  We are really excited about some of things we are seeing that we can do for our clients and looking forward to expanding that out to our entire customer base and beyond.

2010 was not a great year, but I look and see that I still have my health, my family and I’m still working.  Perhaps business has been slow and things have been tight (but really who hasn’t been in the boat this year?), but we are still blessed beyond comprehension.  I look for 2011, with the grace of God, to be a productive and exciting year.  God bless everyone of you that may read this post and to all my friends, colleagues and clients.  May everyone of you be blessed in 2011!

Setting MDIParent results in “Error creating window handle” exception

This was a tricky bug I ran into recently.  First a bit of back history.  I’m working on a largish win-forms MDI application.  Early on in the development I received the  exception “Error creating window handle”  when trying to show the MDI child.  I’m using a 3rd party tab control and I originally thought it was a bug in there.  I found a work-around that if I switched back to the first tab (tab index 0) before showing the new child, the error seemed to go away.  For many months, this worked fine.  Recently I went through a bit of refactoring of the application and suddenly this bug reappeared despite my hack.   Well to make  a long story short some more research ensued and I found the fix (hopefully the true fix now)…actually it is a bit of workaround as well, but we’ll just have to live with it for now.  It appears that .NET MDI applications hates a maximized child window.  To bring another tab to the front, it has to restore the active child window from the maximized state, which triggers the forms layout method to re-layout the form.  This causes a series of events (not interested in digging too deep) that basically seems to create your form twice causing a NullReference exception at some point.  More details here.

Some code that might help me in the future:

    public void DisplayChild(Form child) {
bool max = false;
if (this.ActiveMdiChild != null && this.ActiveMdiChild.WindowState == FormWindowState.Maximized) {
this.ActiveMdiChild.WindowState = FormWindowState.Normal;
max = true;
}
child.MdiParent = this;
child.Show();
if (max) child.WindowState = FormWindowState.Maximized;
}

ActiveControl to tell me the control gaining focus.

I ran into a situation where I needed to know what control was gaining focus in the Leave() event of another control.  The situation I was working with is as follows;  I have a multi-line text box that when it gains focus I want a listview of potential values to popup below it.  When the textbox loses focus, the listview should be hidden.   The user should be able to click on the listview to select items to input into the textbox.  The problem was when the user clicked on the listview, the textbox lost focus and was hiding the listview.

Enter the ActiveControl property on the form.  This handy property allows me to check, in the Leave() event, what is the new active control with focus.  Thus I can check to see if the textbox lost the focus to the listview control and if so, skip the hiding of it.

Here we have the textbox named txtComplaint, when it gets the focus we make listView visible.

private void txtComplaint_Enter(object sender, System.EventArgs e)
{
  listView.Visible = true;
}

When the textbox loses focus we see if the ActiveControl is the listView, if not we hide it.

private void txtComplaint_Leave(object sender, System.EventArgs e)
{
  if (ActiveControl.Name != "listView")
    treeComplaint.Visible = false;
}

Delimited String to Generic List<>

This is something that I tend to forget even though it’s pretty basic and probably obvious to most.  I have a comma delimited string which I wanted to put into a List<>. Here’s a one-liner to get the job done.

List<string> aList = new List<string>();
aList.AddRange(myString.Split(‘,’));

Where myString is my comma-delimited string.  Pretty basic, but very handy.

Follow

Get every new post delivered to your Inbox.

Join 466 other followers