Monday, September 16, 2013

How to parameterize the value of a Top in a SQL query?

Supposing that you have a query like 'Select TOP x from table' and you want to parameterize x param value(in a C# method) then there is a trick which you must have in consideration if you want the query to run.
So, in Sql server 2005 and above you can use parenthesis before and after you param.
Here you go:
 

SELECT TOP (@elements) * from tableCities


It is pretty easy, isn't it?

How to calculate the next business days in C#?

There are cases when according to a business day(Monday to Friday) and number of days you have to return the computed business day. Of course the result can not be on week-end.
So if you ever encounter this scenario when you have a certain date and you want to add x days to it and then return the dateTime found you could use the next extension.
I admit that this extension is a very basic solution. I refer to the fact that I don't take into consideration holidays. If you want a more complex solution then you could go to Dynamic-Holiday-Date-Calculator. Or you could create a list of holidays and then make another method - IsHoliday() that should check if a certain date is part of your holiday calendar.

The idea of this algorithm is simple. In a while block we add one day to our original date time and as long as the result date is not in weekend we decrease number of bussiness days to add.
 
        ///
        /// Adds business days to a date
        /// 
        /// 
        /// 
        /// 
        public static DateTime AddBusinessDays(this DateTime dateTime, int businessDays)
        {
            DateTime resultDate = dateTime;
            while (businessDays > 0)
            {
                resultDate = resultDate.AddDays(1);
                if (resultDate.DayOfWeek != DayOfWeek.Saturday &&
                    resultDate.DayOfWeek != DayOfWeek.Sunday)
                    businessDays--;
            }
            return resultDate;
        }

Hope that helped you ;)

Monday, June 10, 2013

How to set the tab order for controls in a form, Microsoft Visual Studio 2010?

It might happen to you, as it happened to me to have a form with many controls. When you run the application and use only keyboard for moving from one control to another you notice that the order is random, in fact is according to the order of creation of your controls. Maybe you had to change the order after your first sketch.
My first tryout was to change the order manually. I choose from Properties the Tab Index and changed according to my plans. The problem was that I received a nice error that was saying( i don't remember exactly) that the index is already occupied, so it could not change order.

Finally, I searched a while around to see how to set the tab order index for many controls in a form of Microsoft Visual Studio. I found a nice and easy solution which tested and works for Windows Forms in Visual Studio 2010.

First step: as you are in Design View of your form in menu go to View and select Tab Order.


Second step: on your controls will appear a number that represents the Tab Order of your control. For re - setting tab order press a click in ascending order the controls that want to succeed, including Labels(even they are not selectable). I hope it is very intuitive this step for you too.You may see the image bellow.

When you have finished press Escape. Retest your application :)

Like and Share - very much appreciated ;)

Wednesday, May 29, 2013

How to set the background color of a grid column header panel, DevExpress?

If you want to set the background color of a XtraGrid control from DevExpress controls you might get into some problems. Why? Because you can indeed find property Appearance.HeaderPanel.BackColor, then you set it and however no influence on grid. Why? Because you have to make another set :) But is not specified anywhere in info places.
So how do solve this?
In order to set background color of a GridControl, winforms you have to:
-go to gridControl and set Style to Flat;
-set UseDefaultLookAndFeel to False


-go to gridview and set Appearance.HeaderPanel.BackColor - your color (this will automatically set useBackColor to true, also)


Otherwise if you want to set it by code you may use this:



  gridControl1.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat;
  gridControl1.LookAndFeel.UseDefaultLookAndFeel = false; 

  gridView1.Appearance.HeaderPanel.Options.UseBackColor = true;
  gridView1.Appearance.HeaderPanel.BackColor = System.Drawing.Color.Red;
If this helped you, go ahead and share :)

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.