Pages

MVC.Unity Framework - Are you missing a type mapping?

When we implement the Unity Framework for the first time, we may get this error.

The current type, System.Web.Mvc.IControllerFactory, is an interface and cannot be constructed. Are you missing a type mapping?

When we implement the IDependencyResolver of Unity Framework, GetService method will resolve the requested service type and return the actual object. The moment we write our custom resolver by implementing the Unity Framework, MVC framework things that we are going to take care of resolving all the types.

Type resolving happens in the following order .

  • - IControllerFactory
  • - IControllerActivator
  • - HomeController

We resolve for the objects which we create in our application and not for the default objects of MVC. so we need to return null for the objects for which we did not register. That way we can avoid the error because the MVC by default have resolvers. When we return null in our custom resolver, it considers the default resolver and proceed with implementation.

public object GetService(Type serviceType)
{
            return this._container.IsRegistered(serviceType) ? _container.Resolve(serviceType) : null;
}

Please refer this link to get more insight on this.

Unable to create a SQL Job in my local machine

Error while creating a SQL Job in local SQL Server DB Instance.

I got below error when I try to create a SQL job in SQL Server 2005 Instance.

Unable to cast object of type 'Microsoft.SqlServer.Management.Smo.SimpleObjectKey' to type 'Microsoft.SqlServer.Management.Smo.Agent.JobObjectKey'. 

When i check for a fix, i see lot of users asking to uninstall and install the SSIS, but that does not resolve this issue.

This happens on 2 reasons.

1. If we don't have latest service pack installed (SP3) in your machine.
2. If we try to run the job with SQL Agent service account and that does have privilege to execute a SSIS package.

you can check the below code project link, which helps to create a proxy account for you domain account, using that you can run the job. 

Hope this helps.


VS or BITS Toolbox items missing

When i open my Business Intelligence Studio to update my SSIS package, i noticed that all my toolbox items were missing. When i try to add that using choose components, I am able to add all the items which are missing. But the grouping of items as sources, destination and so on are not happening with that manual addition of items.

I Google and many answers mentioned to import tool box settings, but that does not work. Also some people asked to re-install or repair, that does not works. 

One of the solution was to delete the files in the cache folder, that is the only solution which really helped.

Delete the files which starts with toolbox or with extension tdb from the location:

For Windows 7 - C:\Users\bmanohar\AppData\Local\Microsoft\VisualStudio\8.0

Others OS - C:\Documents and Settings \bmanohar\Local settings Application Data\Microsoft\VisualStudio\8.0

I am trying to fix the issue for VS2005, so cleaning the files from location 8.0. for 2008 check 9.0 and 2010 check 10.0

deleting the files does not harm your studio any way, because it as cache file. It will create a fresh copy when you open the studio next time in the same location.

Ignore CA1405: COM visible type base types should be COM visible

CA1405: COM visible type base types should be COM visible : A COM-visible type derives from a type that is not COM-visible.
ComVisible(true) is on by default through the compiler. Ignore this warning. 
If we really need ComVisible(true), then we need to set that in assembly properties. We can remove this from the Microsoft Minimum Ruleset and customize it.

Copy Paste Problem between local and Remote machines

In the Remote machine. open the task manager and in processes tab, select rdpclip.exe and end process. try disconnect and connect, it will start working again. This solution works for Win 2008 R2 OS.

Assigning values to SQL variable using Select Query


Assigning a value to SQL variable:
Declare @date Datetime
set @date = getdate()

We can achieve the same using simple select statement:
select @date = getdate()

If we try to do the same from a result of query, then it will be

select @date = DateCreated from SomeTable where PrimaryKey = SomeValue

We are sure that this query will return only one record, so this statement is valid and always returns expected result. In a scenario, where the result is more than one then which value will be assigned to that variable. Will it be first record or last record or anything in between?

declare @empid char(8)
select top 10 @empid = empid from Empmaster where DeptID = 'A' order by empid
select top 10 * from Empmaster where DeptID = 'A' order by empid
select @empid

It will always get assigned to the last result data. So to be on the safe side we need to put the order by clause and use the appropriate order as ascending or desending considering the fact it always pull the last record. Otherwise we need to use the “Top 1” so it always use the value of first record in the result with proper sorting.

Handling Ajax Asynchronous Post Back Error

I read this solution in one of the article, just want to share the same. 


1. We can handle ajax errors from client side by adding a custom handler to end_request event of PageRequestManager object. 
2. We can handle this by binding a error handler method to property OnAsyncPostBackError of Script manager object.


Client side handling:
<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
  if (args.get_error() != undefined) {
    var errorMessage;
    if (args.get_response().get_statusCode() == '200') {
      errorMessage = args.get_error().message;
    }
    else {                
      // Error occurred somewhere other than the server page.
      errorMessage = 'An unspecified error occurred. ';                                }
    args.set_errorHandled(true);                
  }
}
</script>

Server side handling:
<asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError"></asp:ScriptManager>
protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e){
  if (e.Exception.Data["ExtraInfo"] != null){
    ScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message + e.Exception.Data["ExtraInfo"].ToString();
  }
  else{
    ScriptManager1.AsyncPostBackErrorMessage = "An unspecified error occurred."
  }
}

Invoking mouse events through JavaScript

Handling Mouse Events through Java script

In some scenarios, we may need to invoke a button click through script. We can write element.click(), but that will not work in all the browsers. We may need to do this in another way, please check the below code snippet.


function invokeSaveClick(saveElementId) {
    var e = document.createEvent('MouseEvents');
    e.initEvent('click', true, true);
    document.getElementById(saveElementId).dispatchEvent(e);
}

Request.ApplicationPath is not working in some cases

Request.ApplicationPath will work only IE. When we want to support different browsers like Chrome, Safari and Firefox, we should use the below property to get the correct relative URL.
this.Request.Url.GetLeftPart(UriPartial.Authority)
Example:

If we use Request.ApplicationPath 
window.open('" + Request.ApplicationPath + "/abc/testpage.aspx?x=1"','test page', 'toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes ,modal=yes'); }", true); 
It will be rendered as http://abc/testpage.aspx?x=1

If we use Request.Url.GetLeftPart(UriPartial.Authority), then it will be rendered as 
http://ServerName/abc/testpage.aspx?x=1



View State Issues in ASP.Net


We are getting some of the view state related errors in our application.
  • Invalid view state.
  • Unable to read beyond the end of the stream.
  • The serialized data is invalid.
  • The client disconnected.
The reason for this errors are one of the one mentioned below:

1. The application may be deployed across farms and the encryption/Decryption keys may vary in the servers, so it may throw errors intermittently.
2. The View state may be very large or bloated, so it may reduce the application performance and may cause issue while serialization or deserialization of View state.

Possible solution for this issue are,

1. We can set the encryption validation key at the application level in the web.config file. It looks like the one mentioned below.

<machinekey decryption="AES" decryptionkey="4C05D6B56FA0A057F519884A1C3B5B42B14491ED56CB44F942EE3A0CA2848D29" validation="SHA1" validationkey="ABFD56FE84823AE69E9A613CE998803F4246FE0D96DC48E7BB9E715B0A57352CEFB35264EFB8C0F710E154F42B7460B7CDE9A99F225DA796DEE5E5F94F8F7188"></machinekey>


We can generate keys online from this location. http://aspnetresources.com/tools/machineKey

<pages enableviewstatemac="true">

We can also set the enableViewStateMac to true, but we have some issues when we implement the both these fixes. It may throw user a run time error or compiler warning. It will not allow you to debug sometimes.

3. Other simple solution to overcome this issue is to break the view state in to multiple chunks so that we can avoid the serialization error. This can be achieved by setting a simple property in the pages element of config file.

<pages maxpagestatefieldlength="20">


4. We can also compress and decompress the view state using a simple logic and have that code in the Base Page of the application. We can use compression at the page level by setting a property. Please check the code project link for a detailed explanation on this.

http://www.codeproject.com/KB/viewstate/ViewStateCompression.aspx

These are some of the things I found during my search, i put all the points in one stop for your reference. It worked for me. Hope the same for you all.