Saturday, 21 September 2013

WPF Interview Questions and Answer for Experience and fresher


WPF Interview Questions – WPF

Level 1

  1. Q: What the first class that is loaded by any new WPF Application project?
    A: App. App.xaml and App.xaml.cs. It is also acceptable if they say App.
  2. Q: What is a DependencyProperty?
    A: Represents a property that can be set through methods such as, styling, data binding, animation, and inheritance. [1]
  3. Q: Name as many layout controls as you can:
    A: Grid, DockPanel, Canvas, WrapPanel, UniformGrid, StackPanel
  4. Q: What tool should you use sketch a mock of your WPF application?
    A: SketchFlow

Level 2

  1. Q: Describe how you would go about writing your own button style?
    A: Make sure the interviewee understands the following:
    - How to copy the default style using Expression Blend.
    - How to change the style
    - How there must be a Style for each state: Enabled, Disabled, Mouseover, pushed, etc…
  2. Q: You need to display a list of items. Which controls are possible options? Of those, which would you use and why?
    A: ItemsControl, ListBox, ListView, DataGrid.
    - Make sure they know that ListBox and ListView can be selected while ItemsControl does not support selection.
    - ListView is more feature rich, as it inherits ListBox, which inherits ItemsControl, and adds features.
    - Make sure they understand what a DataGrid is and how it is different and why they would display a list in a DataGrid.
  3. Q: What can you do in Expression Blend that you cannot do in Visual Studio?
    A: Configure brushes and gradient brushes in the Properties window.
    A: Configure Visual States.
    A: Configure sample data.
    A: Configure Object Timelines
    A: Animation
    A: Many design features…
  4. What can you do in Visual Studio that you cannot do in Expression Blend?
    A: Reformat source code, including XAML, with a hot key.
    A: Include Solution Folders.

Level 3

  1. What are common Localization/Globalization practices for Localizing/Globalizing WPF?
    A: 1) Use Resources.resx, 2) Use BAML, 3) Use a ResourceDictionary to manage localizable strings.
  2. Q: What are some best practices when using WPF if you plan to build a Localized/Globalized application?
    A: Write UI in XAML
    A: Avoid sizing and positionings, but let objects automatically flow.
    A: Enable TextWrapping.
  3. C# applications usually start with this method:
    ?
    1
    2
    3
    public static void Main(string[] args)
    {
    }
    Q1: Where is this method in a WPF Application?
    A: Under the project and autogenerated class file called App.g.i.cs is created as a partial class of the App class. So the Main function is part of App but found in App.g.i.cs.
    Q2: If you needed to add a line into the Main function, how would you do it?
    A: You add a new class, such as Program.cs, and in the project properties you set Program.cs as the Startup Object.

WPF Interview Questions – Binding

Level 1

  1. Q: How do you bind a bool value to Visibility?
    A: Add a BoolToVisibilityConverter to your resources and then use it as the converter in your Binding.
  2. Q: What is IValueConverter used for?
    A: So that a control or control property can be bound to an element of any data type and the conversion happens in the binding process.
  3. Q: Here is a TextBox.  Change it so that it will bind to the ‘Text’ property of any DataContext.
    <TextBox  />
    A: <TextBox Text=”{Binding Path=Text}” />
  4. Q: What happens if the value you are binding to does not exist?
    A: The error is ignored. If running from Visual Studio, a message is logged in the Output window.

Level 2

  1. You want to use a custom Binding but the control does not support binding in the area you want. What are your options? Which would you choose and why?
    A1: Inherit object and add a Dependency Property – If the object is not sealed this is likely an easier option.
    A2: Create an attached property – The object might be sealed.
  2. Q: How do you bind a button’s Command property to a method?
    A: You don’t. You bind to an ICommand. You create a class that implements ICommand and in that object you connect to your method.
  3. Q: Provide an example of when and why you would use MultiBinding.
    A: When you have multiple values that your which to combine.
    A: When you want to use String.Format in XAML.
  4. Q: What is the Binding syntax for binding to a Static value?
    A:  {x:Static s:MyStaticClass.StaticValue2}

Level 3

  1. Q: Provide a situation in which you have or would use a ConverterParameter.
    A: Any time you have a converter that returns something different based on a parameter.
    A: Look for them to share an example of needing to do this.
  2. What is the correct syntax for binding to a property of a Singleton?
    A: {Binding Source={x:Static sing:MySingletonClass.Instance}, Path=SomeProperty}

WPF Interview Questions – MVVM

Level 1

  1. Q: What is INotifyPropertyChanged used for?
    A: For notifying any observer, usually a WPF control, that the property it is bound to has changed.
  2. Q: What is the purpose of MVVM?
    A: To provide a design pattern that allows for separation of concerns. The UI is independent of the data and the data is independent of the UI.
  3. Do you prefer and MVVM framework or just a few MVVM objects?
    A: No real right answer here, just checking how they actually have implemented MVVM.
  4. Q: What object do most ViewModels inherit from:
    A: Either ObservableObject or ViewModelBase.
  5. Q: How do you implement binding a button click to a method?
    A: Create a class that implements ICommand, often called RelayCommand or RoutedCommand.
    Add an ICommand property to your ViewModel and instantiate the property using your ICommand implementation, RelayCommand or RoutedCommand.

Level 2

  1. What gotchas have you experience while using the MVVM pattern?
    A: No real answer here…just that they have experienced problems proves their experience. Example issues:
    - The ViewModel just becomes a place for all the code-behind.
    - Determining whether the View can, should, or should not reference the ViewModel.
    - Determining if View objects can, should, or should not be in the ViewModel.
    - When breaking Views down, how small to go.
    - When breaking ViewModels down, how small to go.
    - Should an object in the model implement ObservableObject?
  2. Q: Why is it better to use an IValueConverter instead of  performing the conversion in the ViewModel?
    A: This follows the Don’t Repeat Yourself (DRY) principle. It also follows the Single Responsibility Principle (SRP).
    Because you may have to perform the same conversion between multiple View and ViewModel combinations. If each ViewModel has the code, the code is duplicated. If the code is in an IValueConverter, it exists in one place and is resusable.

Level 3

  1. Q: What does this command do and when would you use it? CommandManager.InvalidateRequerySuggested()
    A: It causes the any commands, such as a binding to Button.Command to check again if it can execute.
    It is used when the command runs and the button should be enabled when the command completes, but the button stays disabled. This often occurs when running the command on a thread or by using a BackgroundWorker.
  2. Q: When should you use “code-behind” in MVVM.
    Note: Don’t except ‘never’ as an answer.
    A: When the code only involves the View or WPF Controls in the View.
    A: When you need to implement events that do not support binding to ICommand.

Monday, 24 June 2013

Excel file fetch data from sql server in 1 second refresh

1.      
2.       Connect with sql server

3.      

4.       Refresh connection property



5.        Go to developer tool à click on visual stio
Click on this work book code

Private Sub Workbook_Open()

Application.OnTime Now + TimeValue("00:00:01"), "SaveThis"

End Sub






6.       Add new module code id
Sub SaveThis()
Application.DisplayAlerts = False
ThisWorkbook.RefreshAll
Application.DisplayAlerts = True
Application.OnTime Now + TimeValue("00:00:01"), "SaveThis"
End Sub

Run use it

Wednesday, 19 June 2013

sql server Find Days between date


SET DATEFIRST 1

DECLARE
    @start_date DATETIME,
    @end_date DATETIME

SET @start_date = DATEADD(MONTH,-1,CONVERT(DATETIME,CONVERT(VARCHAR(4),YEAR(GETDATE()))+'-'+CONVERT(VARCHAR(4),MONTH(GETDATE()))+'-24'))
SET @end_date = CONVERT(DATETIME,CONVERT(VARCHAR(4),YEAR(GETDATE()))+'-'+CONVERT(VARCHAR(4),MONTH(GETDATE()))+'-23')

;WITH Days_Of_The_Week AS (
    SELECT 1 AS day_number, 'Monday' AS day_name UNION ALL
    SELECT 2 AS day_number, 'Tuesday' AS day_name UNION ALL
    SELECT 3 AS day_number, 'Wednesday' AS day_name UNION ALL
    SELECT 4 AS day_number, 'Thursday' AS day_name UNION ALL
    SELECT 5 AS day_number, 'Friday' AS day_name UNION ALL
    SELECT 6 AS day_number, 'Saturday' AS day_name UNION ALL
    SELECT 7 AS day_number, 'Sunday' AS day_name
)
SELECT
    day_name,
    DATEDIFF(wk, @start_date, @end_date) -
        CASE WHEN DATEPART(weekday, @start_date) > day_number THEN 1 ELSE 0 END -
        CASE WHEN DATEPART(weekday, @end_date)   < day_number THEN 1 ELSE 0 END
FROM
    Days_Of_The_Week

Saturday, 25 May 2013

C# TextBox Numeric Value with one decimal point


private void txtBasicSal_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)&& e.KeyChar != '.')
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if (e.KeyChar == '.'&& (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

SQL Server Database BackUp using C#


SQL Server Database BackUp using C#

There are various ways to take the SQL Server database backup. You can take the database backup using SQL Server backup wizard or using SQL Server BackUp Database statement. Here I am going to describe how to take the SQL Server database backup programatically using C# and SQL Server Management Objects (SMO).

In my previous posts, I explained Partial Methods,Contextual Keyword, C# Static Methods and some other articles related to C#, ASP.Net and SQL Server .

SQL Server Management Objects (SMO) is a collection of objects that are designed for programming all aspects of managing Microsoft SQL Server.


For taking the database backup using C#, you have to add the following references in your application-

Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoExtended
Microsoft.SqlServer.SqlEnum

In your .CS file you will have to use the following namespaces-

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

After using above namespaces, write the following code to take the database backup-

public void BackupDatabase(string databaseName, string userName, string password, string serverName, string destinationPath)
{
//Define a Backup object variable.
Backup sqlBackup = new Backup();

//Specify the type of backup, the description, the name, and the database to be backed up.
sqlBackup.Action = BackupActionType.Database;
sqlBackup.BackupSetDescription = "BackUp of:" + databaseName + "on" + DateTime.Now.ToShortDateString();
sqlBackup.BackupSetName = "FullBackUp";
sqlBackup.Database = databaseName;

//Declare a BackupDeviceItem
BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath + "FullBackUp.bak", DeviceType.File);
//Define Server connection
ServerConnection connection = new ServerConnection(serverName, userName, password);
//To Avoid TimeOut Exception
Server sqlServer = new Server(connection);
sqlServer.ConnectionContext.StatementTimeout = 60 * 60;
Database db = sqlServer.Databases[databaseName];

sqlBackup.Initialize = true;
sqlBackup.Checksum = true;
sqlBackup.ContinueAfterError = true;

//Add the device to the Backup object.
sqlBackup.Devices.Add(deviceItem);
//Set the Incremental property to False to specify that this is a full database backup.
sqlBackup.Incremental = false;

sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
//Specify that the log must be truncated after the backup is complete.
sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

sqlBackup.FormatMedia = false;
//Run SqlBackup to perform the full database backup on the instance of SQL Server.
sqlBackup.SqlBackup(sqlServer);
//Remove the backup device from the Backup object.
sqlBackup.Devices.Remove(deviceItem);
}
Mahesh arya,

Friday, 19 April 2013

System.NullReferenceException in Silverlight application designer in Visual Studio 2010


System.NullReferenceException in Silverlight application designer in Visual Studio 2010

This is my Environment:
  1. Windows 7 64 bit
  2. VisualStudio 2010 with SilverLight 4 developer runtime
ProblemAfter upgrading the SilverLight 5 client, I tried to create a SilverLight Application and then I have got the following wired error message on the design tab :(
The text format of the error is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
System.NullReferenceException
Object reference not set to an instance of an object.
at Microsoft.Windows.Design.Platform.SilverlightMetadataContext.SilverlightXamlExtensionImplementations.d__8.MoveNext()
at MS.Internal.Design.Metadata.ReflectionProjectNode.BuildSubsumption()
at MS.Internal.Design.Metadata.ReflectionProjectNode.SubsumingNamespace(Identifier identifier)
at MS.Internal.Design.Markup.XmlElement.BuildScope(PrefixScope parentScope, IParseContext context)
at MS.Internal.Design.Markup.XmlElement.ConvertToXaml(XamlElement parent, PrefixScope parentScope, IParseContext context, IMarkupSourceProvider provider)
at MS.Internal.Design.DocumentModel.DocumentTrees.Markup.XamlSourceDocument.FullParse(Boolean convertToXamlWithErrors)
at MS.Internal.Design.DocumentModel.DocumentTrees.Markup.XamlSourceDocument.get_RootItem()
at Microsoft.Windows.Design.DocumentModel.Trees.ModifiableDocumentTree.get_ModifiableRootItem()
at Microsoft.Windows.Design.DocumentModel.MarkupDocumentManagerBase.get_LoadState()
at MS.Internal.Host.PersistenceSubsystem.Load()
at MS.Internal.Host.Designer.Load()
at MS.Internal.Designer.VSDesigner.Load()
at MS.Internal.Designer.VSIsolatedDesigner.VSIsolatedView.Load()
at MS.Internal.Designer.VSIsolatedDesigner.VSIsolatedDesignerFactory.Load(IsolatedView view)
at MS.Internal.Host.Isolation.IsolatedDesigner.BootstrapProxy.LoadDesigner(IsolatedDesignerFactory factory, IsolatedView view)
at MS.Internal.Host.Isolation.IsolatedDesigner.BootstrapProxy.LoadDesigner(IsolatedDesignerFactory factory, IsolatedView view)
at MS.Internal.Host.Isolation.IsolatedDesigner.Load()
at MS.Internal.Designer.DesignerPane.LoadDesignerView()
Solution: The problem is the conflict of SilverLight versions and I have found the solution in this thread.
Step 1: Uninstall the latest SilverLight client which is SilverLight  5, the 1st one in the below control panel screen shot
Step 2: Uninstall SilverLight 4 developer, the next 1 in the above image
Step 3: Download and Install Microsoft Silverlight 4 Tools for Visual Studio 2010 (Add-on and pre-requisite files for Visual Studio 2010 to develop Silverlight 4 and RIA Services applications)
Installing..!
Installation finished successfully.
Restart the VisualStudio 2010, wow.. it’s working! :)
Happy Silverlighting..! Thanks R./