Dienstag, 27. November 2012

rsAccessedDenied on Windows 8


rsAccessedDenied - The permissions granted to user 'mydomain\myAccount' are insufficient for performing this operation.(rsAccessDenied) (ReportingServicesLibrary)

Do you know this error?

I spent ~2 hours trying to fix it, only to find out in the end, that it was really easy.

I'm running Windows 8 and have SQL Server Express with Reporting Services installed and configured. So unless you have the same configuration the origin of your error might be different.

Anyway - after setting up everything and changing the website to port 8080 instead of the default port (80), because that one didn't work, I finally wanted to log on with my user but nothing seemed to work. For a friend on Windows 7 the solution was to start IE 9 with Administrator privileges, but unfortunately didn't work for me.

My user account is a local administrator so I figured it should have worked, but I am not THE local administrator.  The built-in administrator account.

And this one is disabled by default in Windows 8 so here is how you get it:

  1. Open good old Administrative Tools (you can find it via Start -> Search -> <search for it> -> Settings)
  2. Open good old Computer Management
  3. Computer Management (Local) -> System Tools -> Local Users and Groups -> Users
  4. Set a password for the Administrator
  5. Enable the Administrator

Now you can log on with Administrator and the password you have defined. 

I strongly define the necessary permissions for your normal user account and afterwards disable the Administrator account again.

Hope this saved you a lot of time :)

Donnerstag, 22. November 2012

Hyper-V error "IDE/ATAPI Account does not have sufficient privilege to open attachment..."


Hi,

I have Microsoft Hyper-V running on my laptop (Windows 8). After the installation and configuration was a little bit of a pain (getting an internet connection wasn't that easy for me) it works very well.
Unfortunately I did another major mistake. I wanted to use my VM for classes for testing iptables, and still use it afterwards. But instead of making a snapshot, I did what I was used to do on VirtualBox - copied the file. And once I deleted the destroyed VM and copied my file back, I couldn't start the machine any more. The error displayed was:

"IDE/ATAPI Account does not have sufficient privilege to open attachment..."

I've found quite fast an article on MSDN (http://support.microsoft.com/kb/2249906/en-us?fr=1) but the fix just didn't work for me. What worked was simply remove the Hard Disc which has the "broken" VHD loaded, create a new Hard Disc and load the VHD there inside. Hyper-V will set all necessary permissions on it's own.

Samstag, 1. September 2012

How To Use Generalisation With Generics

Hi, did you also have problems using generics with too specific parameters?

Example:
public abstract class Person 
{ 
  public void DoSomething(IEnumerable<Person> persons) { }
}

public class Teacher : Person { }
public class Student : Person { }

The question - why won't it work?

Exactly, if I want to call this method with IEnumerable<Student> I'd need to cast it. And not just normally cast, but with the extension method .Cast<Person>()
There are several more or less valid reasons for this need, which doesn't change the fact that it's annoying and doesn't serve the purpose of what I want to allow.

A very easy workaround which allows us to do everything we need:

public abstract class Person 
{ 
  public void DoSomething<T>(IEnumerable<T> persons) where T : Person { }
}

public class Teacher : Person { }
public class Student : Person { }

Ok, but what if I want my DoSomething of Teacher only be used with Teacher?

public abstract class Person<T> where T : Person 
{ 
  public void DoSomething(IEnumerable<T> persons) { }
}

public class Teacher : Person<Teacher> { }
public class Student : Person<Person> { }

And this was only the starter - what if you want to have a List of a List, but don't really care if it's an array, IEnumerable, Collection or an actual List?

public abstract class Person 
{ 
  // Limits you to IEnumerable<Person> as inner generic.
  // Example IEnumerable<Person>[] or List<IEnumerable<Person>> ...
  public void DoSomethingMore(IEnumerable<IEnumerable<Person>>) { }

  // No limitations
  public void DoSomethingBetter<T>(IEnumerable<T> persons) 
      where T : IEnumerable<Person> { }
}

public class Teacher : Person<Teacher> { }
public class Student : Person<Person> { }

As you can see you can call DoSomethingBetter any way of combining collections as you want. Even with Person[][] or List<HashSet<Person>>.

Happy Coding!

Peter

Sonntag, 19. August 2012

Namespace Problems in C#

Hallo, just stumbled over two problems with namespaces in C#

First things first:

The type name 'IO' does not exist in the type 'Microsoft.Office.Interop.Word.System' when you have the following usings defined:
    using System.IO;
    using Microsoft.Office.Interop.Word;
The problem is that there exists a namespace Microsoft.Office.Interop.Word.System and Visual Studio or MSBuild somehow apply the System.*-namespaces (even they're defined before) to Micorosft.Office.Interop.Word and make it Microsoft.Office.Interop.Word.System.*

There is also a StackOverflow question about it, but unfortunately without any answer. My solution to this was more of a workaround, I named the namespace Microsoft.Office.Interop.Word.
    using System.IO;
    using word = Microsoft.Office.Interop.Word;
The downside of this workaround is that you need to use "word" in front of every type from the namespace. But at least the code compiles.

The second problem...

was a bit my fault but also not very clear in the beginning.
I have 2 Projects:
  • Common (for common functions, ExtensionMethods, ... which I may use also in the future)
  • Editor - the actual project
in Editor I had another folder "Common" which lead to the namespace Editor.Common

Now, the 1 Million $ question - how do you use in a class which has the namespace Editor.Common a class from the project and namespace Common? - The answer is simple: you can't.

And the moral of this story - don't mix your namespaces and give them names which are as unique as possible.


Happy Coding!
Peter

Sonntag, 12. August 2012

Simple Zoom for WPF Controls

Hallo again,
recently, I had to implement a zoom – in / out – functionality into a WPF application. After some research I found out that there is a RenderTransform on the Grid-Control. Here you can define a ScaleTransform which is able to scale everything in the grid by a value of type double (so 0.5 makes everything half the size, 2 makes everything double the size). One more great but probably not often needed feature is, that you can define different values for the horizontal and the vertical scale.To get an easy and quick return, just use the following XAML-Code:
<Grid x:Name="LayoutRoot">
  <Grid.RenderTransform>
    <ScaleTransform>
      <ScaleTransform.ScaleX>
        <Binding Path="ScaleFactor" ElementName="Window"/>
      </ScaleTransform.ScaleX>
      <ScaleTransform.ScaleY>
        <Binding Path="ScaleFactor" ElementName="Window"/>
      </ScaleTransform.ScaleY>
    </ScaleTransform>
  </Grid.RenderTransform>

  <Grid.RowDefinitions>
    <RowDefinition Height="66"/>
    <RowDefinition Height="*"/>
    <RowDefinition Height="30"/>
  </Grid.RowDefinitions>

  <!-- enter your controls here -->
</Grid>

As you see there is a binding to ScaleFactor, which is Dependency-Property on the Window element. Now you only have to set the ScaleFactor to change the zoom-factor, this was easy.
When you want to implement this on your main-window, which doesn’t have any scroll-bars you won’t be very. You get the zoom-functionality but when you zoom-in, you won’t see everything anymore, and when you zoom-out, there will be unused space.
I can’t help you with the unused space, because this can only be filled, when you actually have scroll bars, because your control ist too large for it’s container. So the best thing is, just ignore the zoom-out and set the minimum zoom value to 1 (100%)
But for the zoom-in, there is a very simple trick, just add 2 more RowDefinitions. Why 2? Because the RowDefinitions with static values (pixel, auto) change by a slightly other factor then the dynamic ones (star). So now, our definition should look something like this:
<Grid x:Name="LayoutRoot">
  <Grid.RenderTransform>
    <ScaleTransform>
      <ScaleTransform.ScaleX>
        <Binding Path="ScaleFactor" ElementName="Window"/>
      </ScaleTransform.ScaleX>
      <ScaleTransform.ScaleY>
        <Binding Path="ScaleFactor" ElementName="Window"/>
      </ScaleTransform.ScaleY>
    </ScaleTransform>
  </Grid.RenderTransform>

  <Grid.RowDefinitions>
    <RowDefinition Height="66"/>
    <RowDefinition Height="*"/>
    <RowDefinition Height="30"/>
    <!– zooming row definitions –>
    <RowDefinition Height="0"/>
    <RowDefinition Height="0"/>
  </Grid.RowDefinitions>
  
  <!-- enter your controls here -->
</Grid>
As you can see, I added 2 more RowDefinitions with Height=”0″ but this will change, in the code, this is the definition of the ScaleFactor and what happens, when it changes:
public double ScaleFactor
{
  get { return (double)GetValue(ScaleFactorProperty); }
  set { SetValue(ScaleFactorProperty, value); }
}

// Using a DependencyProperty as the backing store for ScaleFactor. 
// This enables animation, styling, binding, etc…
public static readonly DependencyProperty ScaleFactorProperty =
  DependencyProperty.Register("ScaleFactor", typeof(double), 
    typeof(MainWindow), new FrameworkPropertyMetadata(1.0,
      FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
      ScaleFactorPropertyChangedCallback));

private static void ScaleFactorPropertyChangedCallback(
  DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  MainWindow me = d as MainWindow;

  if (me != null)
  {
    double starSize = 0, staticSize = 0;

    for (int i = 0; i < me.LayoutRoot.RowDefinitions.Count – 2; i++)
    {
      if (me.LayoutRoot.RowDefinitions[i].Height.IsStar)
        starSize += me.LayoutRoot.RowDefinitions[i].Height.Value;
      else if (me.LayoutRoot.RowDefinitions[i].Height.IsAuto)
        staticSize += me.LayoutRoot.RowDefinitions[i].MinHeight;
      else
        staticSize += me.LayoutRoot.RowDefinitions[i].Height.Value;
    }

    me.LayoutRoot.RowDefinitions
      [me.LayoutRoot.RowDefinitions.Count - 2].Height = 
        new GridLength(staticSize * (me.ScaleFactor – 1), GridUnitType.Pixel);
    me.LayoutRoot.RowDefinitions
      [me.LayoutRoot.RowDefinitions.Count - 1].Height = 
        new GridLength(starSize * (me.ScaleFactor – 1), GridUnitType.Star);
  }
}
Now, you can see, that I dynamically sum up the dynamic and static heights of all rows.
In the end, I set the height of the row before last to “Sum(height of all RowDefinitions with a static size) * (ScaleFactor – 1)” and I do the same with the last row, just for all dynamic RowDefinitions.
But why ScaleFactor – 1 ?
The answer is easy, so I can assure, that everything else my other rows are still fully visible, how?
Because the ScaleFactor changed, everything got bigger, so for example it changed to 1,5 so now everything is 150% of the normal size. But in the window only fits 100% so I can only see 2/3 of my grid. By resizing the 2 rows to the half of the grid (1,5 – 1 = 0,5) the rest still fits into the 100% and the 2 dummy rows are hidden in an unknown space.

Happy Coding!

Spring DAO Exceptions List


Hi all,
I’m coding a global exception wrapping class, which should be automatically create a user-friendly message for every DataAccessException thrown by spring. Although I wasn’t able to find a list of all possible exceptions, so I checked out the spring-tx-<version>.jar and found following exceptions with derived exceptions and because I’m using Spring-JDBC for DatabaseConnection, I also added the Spring-JDBC-Exceptions in red.
  • DataAccessExcpeption
    • NonTransientDataAccessException
      • CleanupFailureDataAccessException
      • DataAccessResourceFailureException
        • CannotGetJdbcConnectionException
      • DataIntegrityViolationException
        • DuplicateKeyException
      • DataRetrievalFailureException
        • IncorrectResultSizeDataAccessException
          • EmptyResultDataAccessException
        • IncorrectResultSetColumnCountException
        • LobRetrievalFailureException
      • InvalidDataAccessResourceUsageException
        • IncorrectUpdateSemanticsDataAccessException
          • JdbcUpdateAffectedIncorrectNumberOfRowsException
        • TypeMismatchDataAccessException
        • BadSqlGrammarException
        • InvalidResultSetAccessException
      • InvalidDataAccessApiUsageException
      • NonTransientDataAccessResourceException
      • PermissionDeniedDataAccessException
      • UncategorizedDataAccessException
        • SQLWarningException
        • UncategorizedSQLException
    • RecoverableDataAccessException
    • TransientDataAccessException
      • ConcurrencyFailureException
        • OptimisticLockingFailureException
        • PessimisticLockingFailureException
          • CannotAcquireLockException
          • CannotSerializeTransactionException
          • DeadlockLoserDataAccessException
      • TransientDataAccessResourceException
Additionally to the DAO and JDBC Exceptions, here are the Transaction-Exceptions:


  • TransactionException
    • CannotCreateTransactionException
      • NestedTransactionNotSupportedException
      • TransactionSuspensionNotSupportedException
    • HeuristicCompletionException
    • TransactionSystemException
    • TransactionTimeOutException
    • TransactionUsageException
      • IlegalTransactionStateException
      • InvalidIsolationLevelException
      • InvalidTimeoutException
      • NoTransactionException
    • UnexpectedRollbackException

Happy Coding

How To Stop a Storyboard


I have an application with a storyboard, which turns a icon that indicates that the application is currently busy. Because I don’t have any chance to know, how long the application will be busy, the RepeatBehavior on the application is set to Forever and I have to stop it manually.
OK, seams to be easy, the storyboard has everything needed to stop it, a Pause-, Stop- and Remove-method, but somehow, nothing works. After some time I detected one line in the Output-Window of Visual Studio:
System.Windows.Media.Animation Warning: 6 :
Unable to perform action because the specified Storyboard was never applied to
this object for interactive control.; Action='Remove'; ...
After some research the answer was very easy and very Microsoft-like, when you start the storyboard with the Begin-method, there is a second parameter of type Boolean, isControllableThis has to be set to true, afterwards all the methods mentioned above will work.
Happy Coding!