Sunday, 30 November 2014

FileLoadException was unhandled by user code(Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040))

I'm getting following error when I'm using NewtonSoft.Json with WebApi client. 

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Solution: To everyone having problems with Newtonsoft.Json v4.5 version try using this in web.config or app.config:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json"
            publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>

  </runtime>

Thursday, 6 November 2014

Skip and Take record from 'N' to 'M' in SQL Sever

Hi Guys! In last weekend I was thinking let's write something on SQL Server database. So I was just thinking which topics should I covered or on which topics should I write...... thinking......thinking...., suddenly I got an idea lets write on some common questions or something which will more efficient during ours interviews or related to our project. So I've decided let's write on  "How to skip N to M records during table record selection?", I think this is very common question., So let's see how to do that in SQL Server database. 

Scenario: Suppose that, I've a table ABC and this table having following records,

Table: ABC
Id          Name       Address
1           Peter          XYZ
2           John           PQR
3           Scott B'u     CBT
4          Allen           123-TPO
5          Belly           986-PYT


So here question is Do we select 2 rows starting from 3rd row?

Solution:  I think the most elegant is to use the ROW_NUMBER function to resolve this problem.

WITH NumberedMyTable AS
(
    SELECT
        Id,
        Value,
        ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber
    FROM
        MyTable
)
SELECT
    Id,
    Value
FROM
    NumberedMyTable
WHERE
    RowNumber BETWEEN @From AND @To
So in this way you can resolve this problem. Please feel free comment for 
any assistance or query.

11 Crazy World Records That Only Indians Could Have Held.

Indians are a competitive breed, which is proven by their obsession of breaking records. According to Guinness Indians rank 3rd in setting world records behind USA and UK and what’s interesting is the bizarre nature of these records. Here are a few world record set by Indians which cross the threshold of conventional.
For more information please follow the following link. http://www.rookiestew.com/11-crazy-world-records-that-only-indians-could-have-held/

Wednesday, 5 November 2014

Exception: ASP.NET Web API - No 'MediaTypeFormatter' is available to read an object of type T

Questions: ASP.NET Web API - No 'MediaTypeFormatter' is available to read an object of type T where T is a primitive type or class type.
Scenarios: I've created a WebAPI as follows:

[RoutePrefix("api/Account")]
    public class AccountController : ApiController
    {
        private AuthRepository _repository;

        public AccountController()
        {
            _repository = new AuthRepository();
        }

        // POST  api/Account/Register
        [AllowAnonymous]
        [Route("Register")]
        [HttpPost]
        public async Task<IHttpActionResult> Register(UserModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            IdentityResult result = await _repository.RegisterUser(model);
            IHttpActionResult errorResult = GetErrorResult(result);
            if (errorResult != null)
            {
                return errorResult;
            }
            return Ok();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _repository.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Private Methods
        private IHttpActionResult GetErrorResult(IdentityResult result)
        {
            if (result == null)
            {
                return InternalServerError();
            }
            if (!result.Succeeded)
            {
                if (result.Errors != null)
                {
                    foreach (string error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
                if (ModelState.IsValid)
                {
                    return BadRequest();
                }
                return BadRequest(ModelState);
            }
            return null;
        }
        #endregion

    }
Now I calling this API register method through the POSTMAN Extension like as follows:
But I'm getting "ASP.NET Web API - No 'MediaTypeFormatter' is available to read an object of type T where T is a primitive type or class type" error. Please suggest me where am I doing wrong.

Answer/Solutions: Actually 'MediaTypeFormatter' is responsible for this exception when you didn't included media type in Request Header. So to fix the problem just add MediaType in header like this way.
{
content-type: "application/json"
}
or in case of POSTMAN extension you can add as follows:

I hope it might be helpful. Please feel free comment for any suggestion.

Monday, 3 November 2014

StyleBundle Index was outside the bounds of the array

Problem Scenarios: I want to include all files in directory like this:
bundles.Add(new StyleBundle("~/Content/ABCJS").Include(
    "~/Content/simpliq/*.js"
));
But I got this error Index was outside the bounds of the array
and red line is:
@Styles.Render("~/Content/ABCJS")
Solution: So there is very simple way you can resolve this problem by just updating two package WEB OPTIMIZATION and WEBGREASE using package manager console or nuget page.