Friday, January 4, 2013

RelayCommand Class for MVVM - C# and VB

Publishing both C# and VB version of the RelayCommand by Josh Smith


Namespace ApplicationClass

''' <summary>

''' A command whose sole purpose is to

''' relay its functionality to other

''' objects by invoking delegates. The

''' default return value for the CanExecute

''' method is 'true'.

''' </summary>

''' <remarks>Class taken from online example. http://msdn.microsoft.com/en-us/magazine/dd419663.aspx Josh Smith</remarks>

Public Class RelayCommand

Implements ICommand

#Region "Fields"

Private ReadOnly _execute As Action(Of Object)

Private ReadOnly _canExecute As Predicate(Of Object)

#End Region

#Region "Constructors"

''' <summary>

''' Creates a new command that can always execute.

''' </summary>

''' <param name="execute">The execution logic.</param>

Public Sub New(ByVal execute As Action(Of Object))

Me.New(execute, Nothing)

End Sub

''' <summary>

''' Creates a new command.

''' </summary>

''' <param name="execute">The execution logic.</param>

''' <param name="canExecute">The execution status logic.</param>

Public Sub New(ByVal execute As Action(Of Object), ByVal canExecute As Predicate(Of Object))

If execute Is Nothing Then

Throw New ArgumentNullException("execute")

End If

_execute = execute

_canExecute = canExecute

End Sub

#End Region

#Region "ICommand Members"

'<DebuggerStepThrough()> _

Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute

Return If(_canExecute Is Nothing, True, _canExecute(parameter))

End Function

Public Custom Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged

AddHandler(ByVal value As EventHandler)

AddHandler CommandManager.RequerySuggested, value

End AddHandler

RemoveHandler(ByVal value As EventHandler)

RemoveHandler CommandManager.RequerySuggested, value

End RemoveHandler

RaiseEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)

End RaiseEvent

End Event

Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute

_execute(parameter)

End Sub

#End Region

End Class

End Namespace











using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Input;

namespace Xpert_Lync.ViewModel

{

public class RelayCommand : ICommand

{

#region Fields

readonly Action<object> _execute;

readonly Predicate<object> _canExecute;

#endregion // Fields

#region Constructors

public RelayCommand(Action<object> execute)

: this(execute, null)

{

}

public RelayCommand(Action<object> execute, Predicate<object> canExecute)

{

if (execute == null)

throw new ArgumentNullException("execute");

_execute = execute;

_canExecute = canExecute;

}

#endregion // Constructors

#region ICommand Members

 

public bool CanExecute(object parameter)

{

return _canExecute == null ? true : _canExecute(parameter);

}

public event EventHandler CanExecuteChanged

{

add { CommandManager.RequerySuggested += value; }

remove { CommandManager.RequerySuggested -= value; }

}

public void Execute(object parameter)

{

_execute(parameter);

}

#endregion // ICommand Members

}

}

Friday, June 1, 2012

Search for SQL Servers on the network

Two ways to search for available database instances

System.Data.Sql
dtDatabaseServer =
SqlDataSourceEnumerator.Instance.GetDataSources()


Microsoft.SqlServer.Management.Smo
dtDatabaseServer =
SmoApplication.EnumAvailableSqlServers(False)


The second alternative allows you to get the full instance name and if the database server is on the local machine. The only catch is that you have to locate the Microsoft.SqlServer.Smo.dll in the C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies, and copy it to your directory.


Here is the output of each data table:

 System.Data.Sql

Microsoft.SqlServer.Management.Smo

Friday, May 11, 2012

Configure Microsoft TMG 2010 and Polycom HDX 6000

In your Polycom, you have to create all the settings for static IP which should be an IP address from you LAN assuming now that you are behind the TMG firewall. Set the H.323 settings in the IP Networks menu including the NAT Public (WAN) Address to be the external IP address.

If you are using a Gatekeeper, ensure that it can connect. If you have restricted access out from your firewall to your internal clients, you need to allow access through Access Rules in TMG for the below mentioned protocols, and reverse the directions for TCP (Inbound to Outbound) and UDP (Receive/Send to Send/Receive).

You need to create Protocols for the following ports:
1719 Receive/Send (if you are using a Gatekeeper with external provider)
1720 TCP Inbound
3230-3243 TCP Inbound
3230-3341 UPD Receive/Send


You need to create Non-Web Server Protocols, one for each of the above Protocols:
Polycom 1719:
- External to Polycom IP address
- Request appear to come from the original client

Polycom 1720:
- External to Polycom IP address
- Request appear to come from the Forefront TMG computer

Polycom 3230-3243 TCP:
- External to Polycom IP address
- Request appear to come from the original client

Polycom 3230-3341 UPD:
- External to Polycom IP address
- Request appear to come from the original client


As you can see in the above picture, the Denied Connections by Default rule was triggered becasue of wrong direction set in TCP Protocol.
Note: If you set the wrong direction on the UDP protocols such as setting it to ‘Send/Receive’ instead of ‘Receive/Send’, the protocol will not appear in the wizard when you publish your non-web server protocol. The same applies to the TCP protocols if you set them to Outbound.

‘Send/Receive’ of UDP is the equivalent of TCP outbound and vice versa.

If you look in your firewall log, and you see the H.323 protocol being triggered, that is because you probably have not setup your rules correctly. If you look at the rule which triggered it and the port, it should be blank.

The reason for this is because TMG identifies this protocol and applies the system protocol to the rule, as TMG is not a passive firewalls where you just open and close ports.

I believe that the H.323 protocol is probably very powerful in protecting your environment, but blogs and even on Microsoft actually recommend disabling the H.323 filter in TMG. http://support.microsoft.com/kb/556039/en-us

To avoid TMG to use the H.323 Protocol (not the filter), creating your user-defined protocol for this port and setting the non-web server rule, will eliminate this being used.

Please feel free to comment on this blog…





Forefront TMG includes an H.323 filter that allows H.323 compliant applications, such as Microsoft Windows NetMeeting® 3, to pass through Forefront TMG. This enables rich multimedia and real-time collaboration capabilities between enterprises using the Internet. Organizations that deploy interdepartmental firewalls can also use this technology to enhance communications between their employees over their intranets.

Additionally, the H.323 filter protects communication between internal clients and the Internet, hiding client IP addresses and restricting access, as needed.

H.323 Protocol

The H.323 protocol is a set of standards enabling real-time multimedia conferencing and communications over packet-based networks that do not guarantee Quality of Service (QoS). The standards were developed to accommodate varying usages. Due to the inadequate quality of voice over the Internet, it was proposed that improvements could be made if communications were carried partly on the Internet and partly on the public switched telephone network (PSTN). The H.323 standards would also provide for communications between a standard PSTN phone and a computer-based client.

H.323 defines how compliant components (terminals, gateways, gatekeepers, and multipoint control units) engage in audio, video, and multipoint conference communications. The H.323 standards define the mandatory and optional services supplied by a gatekeeper. The H.323 protocol standard contends with call control and management for both point-to-point and multipoint conferences. The standard also defines the gateway operability that allows calls to be connected between H.323 terminals as well as between LAN and PSTN devices.

By default, the H.323 filter is applied to the H.323 protocol.


Monday, March 26, 2012

Nasty Exceptions with Threaded MVVM Application

Consider to work with the private variable of a property versus the public property itself if you are implementing INotifyPropertyChanged. More specifically, if you have your OnPropertyChanged event in the Set block of your property as this makes a call to the Main Thread of your application to update the view (which you probably already knew).

Here is an example…

This property calls the OnPropertyChanged event every time the property is set.

        Private _salesOrderItemView As DataView = Nothing

        Public Property SalesOrderItemView As DataView

            Get

                Return _salesOrderItemView

            End Get

            Set(ByVal value As DataView)

                _salesOrderItemView = value

                OnPropertyChanged("SalesOrderItemView")

            End Set

        End Property

                                                         

And if you populate the property

SalesOrderItemView = dsTemp.Tables(0).DefaultView



Then add a column

SalesOrderItemView.Table.Columns.Add("NewColumn")



And do a For Each loop

For Each row As DataRow In SalesOrderItemView.Table.Rows

       ‘’Do something

Next



If it is quick enough, you will get a nasty exception.

Why? I am not really sure, but I think it has to do with adding a row and be in the iteration of the For Each loop. It seems to me that if you are adding the row, which will call the OnPropertyChanged event and update the view in the main thread, it will continue to execute the background thread. If it is fast enough to get into the For Each loop while the view is being updated, it will take away the object reference to the view.

Though, if I put a couple of lines of code between the adding a column statement and the For Each loop, the view will have enough time to get updated and no exception will be thrown.

Commenting out the OnPropertyChanged event will also avoid an exception being thrown.
Any comments on this?

Tuesday, March 20, 2012

Short fix to oledb blank values when importing excel file

If you are having trouble importing data from excel file and are receiving blank values, it is due to that the excel drive scans (by default) 8 rows and determines which is the predominant data value. This does not take into consideration that you have set your cell type to General or Numeric.

Also, adding the IMAX=1 in the connection string does not always work.



The upside to this is that you can change the default scan vaule from 8 rows to be between 1 and 16. This is done by adding MaxScanRows=1 to the connection string. Note that you can set it to 0 where it SHOULD scan all the rows, but there is a bug in the ODBC driver and the setting has no effect.




The simple solutions is to insert a blank row under the header and add a blank space as the first character.


Rows to Scan: Excel does not provide ADO with detailed schema information about the data it contains, as a relational database would. Therefore, the driver must scan through at least a few rows of the existing data in order to make an educated guess at the data type of each column. The default for "Rows to Scan" is eight (8) rows. You can specify an integer value from one (1) to sixteen (16) rows, or you can specify zero (0) to scan all existing rows. This is done by adding the optional MaxScanRows= setting to the connection string, or by changing the Rows to Scan setting in the DSN configuration dialog box.

However, due to a bug in the ODBC driver, specifying the Rows to Scan (MaxScanRows) setting currently has no effect.



For example:


· In your eight (8) scanned rows, if the column contains five (5) numeric values and three (3) text values, the provider returns five (5) numbers and three (3) null values.


· In your eight (8) scanned rows, if the column contains three (3) numeric values and five (5) text values, the provider returns three (3) null values and five (5) text values.


· In your eight (8) scanned rows, if the column contains four (4) numeric values and four (4) text values, the provider returns four (4) numbers and four (4) null values.


About IMAX=1
http://support.microsoft.com/kb/194124 

Insure that the data in Excel is entered as text. Just reformatting the Excel column to Text will not accomplish this. You must re-enter the existing values after reformatting the Excel column. In Excel, you can use F5 to re-enter existing values in the selected cell.

NOTE: Setting IMEX=1 tells the driver to use Import mode. In this state, the registry setting ImportMixedTypes=Text will be noticed. This forces mixed data to be converted to text. For this to work reliably, you may also have to modify the registry setting, TypeGuessRows=8. The ISAM driver by default looks at the first eight rows and from that sampling determines the datatype. If this eight row sampling is all numeric, then setting IMEX=1 will not convert the default datatype to Text; it will remain numeric.

You must be careful that IMEX=1 not be used indiscriminately. This is IMPORT mode, so the results may be unpredictable if you try to do appends or updates of data in this mode.