Thursday, March 14, 2013

How to design Windows Forms with Abstract Inheritance?

It happened few days ago when I got to this nice error:

The designer must create an instance of type '... .BaseClass.TabBaseControl' but it cannot because the type is declared as abstract.


The scenario was like this: I had a Base class where a defined abstract members that where implemented in a form. When wanted to see the design of the page this warning appeared even though the application runs just fine. So how to get rid of this error:  "The designer must create an instance of type 'SchedeMaterialiDaTaglio.BaseClass.TabBaseControl' but it cannot because the type is declared as abstract. " ?
Well, searching around I got to this post http://www.platinumbay.com/blogs/dotneticated/archive/2008/01/05/designing-windows-forms-with-abstract-inheritance.aspx where I found part of the solution. 
Here it is:

 public class AbstractCommunicatorProvider : TypeDescriptionProvider
    {
        public AbstractCommunicatorProvider()
            : base(TypeDescriptor.GetProvider(typeof(UserControl)))
        {
        }
        public override Type GetReflectionType(Type objectType, object instance)
        {
            return typeof(UserControl);
        }
        public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
        {
            objectType = typeof(UserControl);
            return base.CreateInstance(provider, objectType, argTypes, args);
        }
    }
You define this class in inherited class(it only worked for me in this way) and you must also put this for base class and inherited class:

    [TypeDescriptionProvider(typeof(AbstractCommunicatorProvider))]
    public partial class TabUC : TabBaseControl{}

    [TypeDescriptionProvider(typeof(AbstractCommunicatorProvider))]
    public abstract class TabBaseControl : UserControl, ITabBase{}
Of course that you will have to include System.ComponentModel namespace and others just as needed.
Hope it helped. Ciao.

Friday, March 8, 2013

How to solve "Object reference not set to an instance of an object" in Design Time, Visual Studio, Winforms?

If you ever encounter this nice message then here is what you should do in order to fix "Object reference not set to an instance of an object" showing in Design Time even if code runs just fine.

I will tell you how I encountered it. I created a user control that has a property. In a page form I included this user control among others. In control's Load event I had to use that defined property. And I did not understood why it needs to know the value in design time, however it shows this nice error. It is obvious that is not set. It is only set when running.
So. Ready with long talk. The only think you need to get away of this "Object reference not set to an instance of an object" error you must use DesignMode property of the control. :)

if (!this.DesignMode)
{
   //your code here that generates the error
}
Well, hope that I helped you today with this error. Keep up the good code ;)

Friday, January 25, 2013

How to maintain the current scroll position across postbacks in Asp.Net?

It might sometimes happen that when you submit a page you want that the scroll position of the page remain the same. So how do you do that, in Asp.Net?

First I thought about some javascript code. Well you don't need some extra js functions when Asp.Net has a built in property.

Well, in order to keep scroll position of the page the same after submit you have to set in page declaration MaintainScrollPositionOnPostback  property to "true".

As I said in order to maintain the current scroll position across postbacks you can do this:
<%@ Page MaintainScrollPositionOnPostback="true" %>

Regards.

Friday, December 14, 2012

How to check CheckedListBox item with single click in Windows Forms Application?

If you want that when you check an item in CheckedListBox control in Windows Forms Application the item to be checked from first click there is a trick you need to do it.
As I developed more with Asp.Net, web applications, it was very awkward for me that when I checked the item in checkedListBox control it did not checked it but rather kind of selected it.
Well, in order to answer your question you have to make only one modification: select the control and then press Alt + Enter for Properties and then look for CheckOnClick property. Set it on True.

There you go :)
If this post helped you, leave a comment or share it :)

How to get rid of 'Saving Changes Is Not Permitted On SQL Server 2008 Management Studio'?

Well that is really annoying. Isn't it? This often happens when you want to alter a table: change data type on existing columns or change allow null on existing columns.
Good, so how do you get rid of this warning: Saving changes is not permitted ?

These are the steps:
1.Open Microsoft Sql Server Management Studio 2008 (I guess it is already opened)
2.Go to Tools(in menu) and click Options
3.Click Designers
4. and Uncheck 'Prevent saving changes that require table re-creation' option
5.Go OK and retry alter your table.

Here is a print screen.

There you go. Continue programming.

Monday, November 26, 2012

How to INSERT into a table records extracted from another table?

If you want to insert values from one table into another you can use this sql query.

insert into TBL_TARGET([USERNAME],[PASSWORD],[ACTION])
select [USERNAME] ,[PASSWORD], 'I'
from [TBL_SOURCE]


Don't use any extra 'VALUES' or parenthesis.

How to solve error 15128 in Microsoft Sql Server ?

If you ever meet this nice Sql Error, 15128 in Microsoft Sql Error you must do these next steps. At least they worked for me :)
First I want to explain which is the scenario. You are logged with Windows Authentication and you created a new sql login. Now you want to disable Enforce Password Policy. When you want to do that Sql tells you that you can not save the user, a new password must be set. Strange...so:

  • double click on new login created, leave the check boxes checked(enforce password policy and enforce password expiration) and change user's password temporally to what ever you want, click OK
  • then reopen and uncheck enforce password policy, enforce password expiration; Press OK
  • finally you reopen the user and specify the password you want
  • Test it ;)
Hope this will help you.