Feeds:
Posts
Comments

Custom validation in MVC can be achieved in two ways by implementing :

  • ValidationAttribute class
  • IValidatableObject interface

ValidationAttribute

I have created a sample blog application which has a model blog class. Here i will create a validation for no of words in blogger name called MaxWord.

Below steps will demonstrate how to implement it:

  1. Create  class MaxWordAttribute which inherits from System.ComponentModel.DataAnnotations.ValidationAttribute 
  2. Override the IsValid() and add your validation logic here.
  3. Add the Attribute MaxWord(1) on the property (Blogger Name) in Model class
  1. Code1

Now run this and you will see the validations working perfectly.

Code2

But there is a catch here . The above validation will happen only at server side. To make it work at client side we need to do little thing extra.

Steps needed to make it work at client side :

  1. Make the MaxWordAttribute class implement IClientValidatable interface
  2. Implement GetClientValidationRule method. You can add custom parameters in this method that you require on client side for validation.MVC1

3. Create a JavaScript file and register your client methods here using jQuery.validator.unobtrusive.add and jQuery.validator.addMethod methods. I have created MyValidation.js and placed it under Scripts folder

.MVC2

4. To enable client-side validation we have to do couple of things. First we have to make sure  the ClientValidationEnabled and UnObtrusiveJavaScriptEnabled are set to true in web.config

Code5

5. The next thing is include the necessary javascript files in the layout page i.e in my case it is create.cshtml

Code4

Now you can have the custom validation at both client and server side 🙂

IValidatableObject

This is the approach you should use when you wanted to do a deep inspection of a model . Here you will be inside the model object and can access all the properties of the model.

Steps to implement it :

  1. Make your model class implement IValidatableObject interface
  2. Write your validation logic in the Validate() method

Code8

Code7

Unlike the simple IClientValidatable implementation required for the ValidationAttribute method, I have not found any way of enabling client-side validation when using IClientValidatable

I have been working on SSIS for quite some time now but today i came to know about the use of dynamic query in LookUp control. Normally when earlier i used to face a similar situation,i normally rely on OLE DB Source control which facilitates the dynamic query(though it required Merge control as well )

I will demonstrate the way i have implemented this in my project.Below are the steps that i have followed:

  1. Create a variable – SqlEvalType of type string to hold the query to be evaluated at run-time (select id,entity_id,eval_date  from hr_evalinfo  where eval_type = ‘” + @[User::EvalType]   + “)                                                                                                   DQ2
  2. Now go to the properties of the DFT which holds the LookUp control and click on the Expression as shown by red arrow on the screenshot below . It will opens the editor window      DQ1 Continue Reading »

Today i came across a very interesting finding regarding one of the most common ssrs  error while using sub reports — One or more parameters were not specified for the subreport which is normally accompanied by actual report error —Error: Subreport could not be shown

There can be various reasons for the error which are listed below:

  1. Problem with the dataset of the subreport– Run the sub report independently n make sure that it is working
  2. Not passing an actual value to one of the parameters of the subreport – Check whether the names n values of parameters passed from main report to sub report are correct.

When i started getting this error at my production server, i first checked the above 2 points which are available at most of the google blogs but the problem was still there.

The problem was not there if i run in my local box with VS 2008 but if i go to the report manager and run the main file, it was throwing the error again.

Then i changed the way i was inserting my sub report in my main report and it solved the problem

Instead of directly inserting sub report by right click n then adding to the main report, i first added a table.

Then removed the extra columns as i needed only one and drag n dropped the subreport rdl to the column.

After adding the parameters, it started working 🙂

Don’t know whether it might solve the problem for you guys but just thought of sharing my experience

Prashant

One day someone asked me — Are you using MVVM in your project with complete no code in code behind files? I said we are using MVVM but there is still code in button click events in code behind files.
Then I realized that as my project was in Silverlight 3.0 the time we developed the application, we were forced to do the way we did. But not anymore now with Silverlight 4.0.Below is the way to use it

Commanding has always been available in WPF, but was only recently added to Silverlight 4 with the addition of the Command property to ButtonBase and Hyperlink. Commanding lets controls know when their actions are available or not. If not, the controls are disabled.
For example, when adding a new customer, you cannot also delete a customer, therefore, while adding, the Delete button needs to be disabled. The Command pattern is designed to help in these kinds of scenarios.
Commanding is based on a simple interface: ICommand. It has 3 members,

interface ICommand

{
void Execute(object parameter);
bool CanExecute(object parameter);
event EventHandler CanExecuteChanged;
}

There are basically 3 steps to use commanding feature in Silverlight:

1) Create a command class that implements the ICommand interface.
2) Create a property of type ICommand in the View/Model class.
3) Bind the Command property of a Button or Hyperlink to the property created in 2 above.

We will create a simple application to append last name with first name on a button click.

Creating a Command Class
All ICommand interfaces are wrapped in a custom RelayCommand class which makes it easy to enable and disable commands from the ViewModel itself. The code for the RelayCommand is shown below:

public class RelayComand :ICommand
{
private Action handler;

public RelayComand(Action handler1)
{
handler = handler1;
}

           private bool isEnabled;

           public bool IsEnabled
{
get { return isEnabled; }
set
{
if (value != isEnabled)
{
isEnabled = value;
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}

}
}

public bool CanExecute(object parameter)
{
return IsEnabled;
}

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)
{
handler();
}
}

Creating a Property in the View/Mode
private readonly ICommand calculateCommand;

public ICommand CalculateCommand
{
get
{
return calculateCommand;
}
}

Binding to the Command

<Button Grid.Row=”1″ Grid.ColumnSpan=”5″ Margin=”0,5,0,0″

Content=”Calculate” x:Name=”btnCalculate”

Command=”{Binding CalculateCommand}” />

The complete code is shown below:
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
calculateCommand = new RelayComand(Calculate) { IsEnabled = true };
}
private string firstName;
public string FirstName
{
get { return firstName; }
set
{
firstName = value;
OnPropertyChanged(“FirstName”);
}
}
private string lastName;
public string LastName
{
get { return lastName; }
set
{
lastName =  value;
OnPropertyCh anged(“LastName”);
}
}
private string fullName;
public string FullName
{
get { return fullName; }
set
{
fullName = value;
OnPropertyChanged(“FullName”);
}
}
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

public event PropertyChangedEventHandler PropertyChanged;
private readonly ICommand calculateCommand;
public ICommand CalculateCommand
{
get
{
return calculateCommand;
}

}

private void Calculate()
{
FullName = FirstName + ” ” + LastName;
}

}

In the normal case when we want to consume a WCF service, we need to create proxy at client side. To create proxy, service must expose metadata endpoint.

Let us assume we want to call the service using channel without creating proxy or adding the service reference. We need to follow the below steps

Step 1

Put all the DataContract or ServiceContract in a separate DLL or class library. Add the reference of System.ServiceModel in class library. And create the service contract as below,

ServiceWithoutProxy.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
 
 
namespace ServiceContractDll

  [ServiceContract] 
    public  interface ServiceWithoutProxy 
  { 
     [OperationContract] 
      string GetData(int value); 
   }
}
Assume we have created a service library called ServiceContractDll

Step 2

Create a WCF Service application and implement the service contract created in step 1. For that add the reference of project created in step 1 in WCF service application.

Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using ServiceContractDll; 
 
 namespace WcfService1

   public class Service1 : ServiceWithoutProxy  
   { 
      public string GetData(int value) 
        { 
            return string.Format(“You entered: {0}”, value); 
        } 
   }
}
Add the EndPoint in config file.

<services>
  
<service name =”WcfService1.Service1″ >
    
<endpoint address =”” binding =”basicHttpBinding” contract =”ServiceContractDll.ServiceWithoutProxy”/>        
           <host> 
               <baseAddresses> 
                   <add baseAddress =”http://localhost:8181/Service1.svc”/
                </baseAddresses> 
         </host> 
   </service>      
</services>

In above config file

ServiceContractDll.ServiceWithoutProxy is name of the service contract exposed.
There is no metadata exchange endpoint in the config file.

Step 3

Create the client. We are creating a console client to consume the service with channel or without creating proxy. So follow the below steps

Do not add the service reference.
Add the reference of System.ServiceModel.
Add the reference of class library created in step1.

Now we need to Create a ChannelFactory

ChannelFactory<ServiceWithoutProxy> factory = null;

Create a binding of the type exposed by service

BasicHttpBinding binding = new BasicHttpBinding();

Create EndPoint address

EndPointAddress address = new EndPointAddress(“http://localhost:4684/Service1.svc“);

Pass Binding and EndPoint address to ChannelFactory

factory = new ChannelFactory<ServiceWithoutProxy>(binding,address)

Now create the channel as below ,

ServiceWithoutProxy channel = factory.CreateChannel();

Call the service method on this channel as below ,

string returnmessage = channel.GetData(9);

 Thanks,

Prashant

When i first started using WCF RIA services,  I had a very limited knowledge of WCF . So for many days i didn’t bother whats happening behind the scene, how RIA is using WCF  internally etc. Then one day when i learned WCF, it forced me to explore how it is getting used in WCF RIA 🙂

To help you better understand how RIA Services uses WCF, in the following section I walkthrough what happens under the covers when a user creates a simple DomainService and then communicated with it.

1. ‘Add new DomainService Class’

Lets assume an application developer opens up the ‘Add new Domain Service Class’ item template and adds a new Domain Service.

The item template, besides producing a skeletal Domain Service Class, adds the right assembly references and registers an Http modules in the Web.Config. By default it registers an httpModule for Cassini (for Visual Studio F5 experience) and one for IIS, as shown below.

<?xml version="1.0"?>
<configuration>

  <system.web>
    <httpModules>
      <add name="DomainServiceModule" 
type="System.Web.Ria.Services.DomainServiceHttpModule, 
System.Web.Ria" />
    </httpModules>
    <compilation debug="true" targetFramework="4.0" /> 
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="DomainServiceModule" preCondition="managedHandler"
type="System.Web.Ria.Services.DomainServiceHttpModule, 
System.Web.Ria" />
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
  </system.webServer>
 
 

2. Domain Service Code

The developer then goes ahead and adds Business logic to his DomainService.The methods exposed via the DomainService can be broken into two broad categories –

CRUD operations – Query, Update, Named Update and Delete operations. These operations follow the RIA Services prescriptive guideline and rely on the RIA Services framework. This is added functionality that RIA Services introduces on top of WCF and is not available to Core WCF Services.

Service Operation/ Invoke Operations – These are [Invoke] operation in the RIA Services terminology and Service Operations in WCF terminology. These methods are independent of the RIA Services concept of ChangeSet (ChangeSet applies only to the CRUD operations above) and are ‘Online/Direct’ methods that communicate with the Server immediately when invoked.

Below is the code for the OrganizationService DomainService we use in our canonical RIA Services walkthrough.

namespace HRApp.Web
{
    [EnableClientAccess()]
    public class OrganizationService : 
        LinqToEntitiesDomainService<AdventureWorks_DataEntities>
    {
        #region CRUD    
        public IQueryable<Employee> GetEmployee(){…}        
        public void InsertEmployee(Employee employee) {…}        
        public void UpdateEmployee(Employee currentEmployee) {…}        
        public void DeleteEmployee(Employee employee) {…}
        public IQueryable<Employee> GetSalariedEmployee(){…}
        [RequiresAuthentication()]
        public void ApproveSabbatical(Employee current) {…}   
        #endregion

       #region ServiceOperations
        public string Echo(string msg) {…}
        public DateTime GetServerDateTime(){…}
       #endregion
    }
}

3. WCF Channel on Client

The RIA Services Framework on the Client contains a WebDomainClient:DomainClient whose purpose is to help the SL client communicate with a WCF service represention of the Domain Service. The WebomainClient uses a WCF Client Chanel for this cummunication.

The Channel is created by using WCF’s ChannelFactory and by passing to it a WCF Service Contract that was generated from the Domain Service (more on the contract creation later). The ChanelFactory creates a WCF client proxy based on the supplied contract. The generated proxy takes care of communication/(de)serialization between Client and Server.

The RIA Services DomainContext utilizes the WebDomainClient for Client-Server communication and the context itself is WCF agnostic.

4. Dynamic .SVC generation

By default DomainServices do not have a physical .SVC file generated for them at Design Time.

However each DomainService has a virtual .SVC associated with it. The .SVC represents the WCF Service that services requests for that particular DomainService. For a given DomainService the path to its .SVC can be determined using the following convention:

[SilverlightApplicationBaseURI] + [DomainServiceFullName].svc (With all “.” replaced by ““)

So HRApp.Web.OrganizationService is exposed as – http://[ApplicationBaseURI]/HRApp-Web-OrganizationService.svc

At RunTime when the first request (within a particular Application Domain) is made for a DomainService’s .SVC file , the registered httpModules intercept the call and RIA Services writes out an in memory .SVC file on the fly.

Below is what the dynamically generated svc file looks like for the OrganizationService defined above –

<%@ ServiceHost Service=”HRApp.Web.OrganizationService” 
Factory=”System.Web.Ria.DomainServiceHostFactory”
%>
 

The .SVC refers to the DomainService Type and a ServiceHostFactory. The default RIA Services  HostFactory instantiates the default RIA Services ServiceHost, which in turn is responsible for extracting the WCF Service Contract from the DomainService (See the ‘Generating a WCF Contract’ paragraph below for more details) and also for hosting the service.

A request for the DomainService .SVC in any folder under the Web Application root is redirected to [WebAppRoot]/Services/[DomainService].svc using ASP.net URL rewriting. The service is thus accessible under any folder in the WebApp. Hence even if a .XAP is moved around under the hosting Web App Root, the Silverlight App’s relative reference to the DomainService is not broken. 

NOTE – If a physical .svc file with the right file name (as per convention) is present in the ~/Services folder, that is used to define the Service Contract and no virtual .SVC file is generated.

5. Generating the WCF Contract:

Each WCF service needs to have a ServiceDescription and one or more ContractDescriptions for it.

ServiceHosts are responsible for extracting descriptions from a service and hosting the service. The standard ServiceHost in WCF produces descriptions based on WCF attributes such as [ServiceContract] and [OperationContract]. In RIA Services we provide a custom ServiceHost which does this based on a RIA Service Attributes and Conventions.

Here is how DomainService operations are mapped to the WCF Contract:

Query operations –

Each Query operation shows up as a ServiceOperation in the WCF Contract, but with its signature modified.

The ServiceHost creates an operation description for each query operation. The return type is changed to QueryResult<T> such that it can return additional information such as count. It also adds a QueryOperationBehavior which through a custom operation Invoker takes care of applying cache policies, validating parameters and composing queries.

Insert, Update, Delete operations –

For all CUD operations the ServiceHost generates one top-level SubmitChanges operation description. Again, it adds a custom behavior which injects a custom operation invoker. The signature of SubmitChanges is ChangeSet SubmitChanges(ChangeSet changeSet). The returned ChangeSet contains auto-generated values from the server.

Invoke Operations –

Invoke Operations are the RIA Services equivalent of WCF ServiceOperations. All Invoke Operations show up in the WCF Contract as Service Operations.

For the OrganizationService Domain Service we had defined earlier, below is what the WCF Contract looks like to the “Add Service Reference” dialog –

image

Attached to this post is also the full generated WCF Contract for the OrganizationService (the file was produced by doing an Add Service Reference to the Domain Service’s WCF endpoint)

6. Default Endpoints:

The RIA Services ServiceHost creates the following endpoints by default – 

a) For Silverlight Client: SOAP w/binary endpoint. Address = “binary”, Binding = CustomBinding consisting of HttpTransportBindingElement and BinaryMessageEncodingBindingElement.

b) For AJAX Client: JSON REST endpoint. Address = “”, Binding = WebHttpBinding w/JSON as the format.

c) For other clients : SOAP w/XML endpoint. Address = “soap”, Binding = BasicHttpBinding consisting of HttpTransportBindingElement and TextMessageEncodingBindingElement.

For more information refer to http://blogs.msdn.com/b/saurabh/archive/2009/11/23/understanding-the-wcf-in-wcf-ria-services.aspx

There might be some breaking changes that can be found here –code.msdn.microsoft.com/…/ProjectReleases.aspx

Also, here is a sample that demos the use of WCF extensibility by RIA services and runs on the latest bits –

code.msdn.microsoft.com/…/ProjectReleases.aspx

 Thanks,

Prashant

You can avoid SCR component in SSIS for formatting if SQL functions are used while fetching source data.

Below is the example where I need to fetch decimal 3428.00 and format required for submission is 00342800.

 select ISNULL(right(REPLICATE(0,8) + replace(convert(nvarchar(8),NULL),’.’,”),8),’00000000′)

 Also SSIS best practice suggest that fetch as much data as possible while retriving from source and perform  operation on T-SQL statement

Thanks,

Prashant

I came across an interesting  finding while working with recordset in SSIS Script Component. Here it goes :

Populating a data table from SSIS variable (recordset) within a script component/Task works fine first time but produces no result subsequently.

Below code works fine while we are loading data table from objCDHArrays for the first time.

DataTable dtCDHArrays = new DataTable();

OleDbDataAdapter oleCDHArraysDA = new OleDbDataAdapter();

oleCDHArraysDA.Fill(dtCDHArrays, Variables.objCDHArrays);

If we load again from the same variable , then it returns nothing :

DataTable dtCDHArrays1 = new DataTable();

OleDbDataAdapter oleCDHArraysDA1 = new OleDbDataAdapter();

oleCDHArraysDA1.Fill(dtCDHArrays1, Variables.objCDHArrays);

Solution:

Cast the SSIS recordset to ADODB.Recordset object

Recordset rsPayBase = new Recordset();

rsEECON = (Recordset)Variables.objCDHArrays;

refer to the below links for more info :

http://social.msdn.microsoft.com/forums/en-US/sqlintegrationservices/thread/7b7e6c2f-ca84-4d56-9751-97c8c2becaed/

http://social.msdn.microsoft.com/Forums/en-US/sqlintegrationservices/thread/2bb13fe7-6241-4344-b96f-ca6053072e2c

Happy Reading

Prashant