Wednesday, January 18, 2012

Return multiple object using JSON in Asp.net MVC

Here are we going to discuss what are the differnt ways to send json response to browser.

Note: we are assuming that json2. js file is already added into html page  you can use following url to download json2.js file or you can directly add this link in your page .

http://www.JSON.org/json2.js
<script type="text/javascript" src=http://www.JSON.org/json2.js />

1. It  is straight forward to retrun a single json object using following statement:

public JsonResult getResult()
{
        // Create an object of any kind
        Report objReport= new Report();
  
retun Json( objReport);
}

in html file we can use it like

function callback(response)
{
     var JsonObject= Json.parse(response);
//now we can use JsonObject same Report Object


}


2. It straight forward to retrun a Multiple object using json  byfollowing statement:

public JsonResult getResult()
{
        // Create an object of any kind
        Report actualReport= new Report();
        Report scheduledReport= new Report();
  
retun Json( new { ActualReport=actualReport,ScheduledReport=scheduledReport);
}

in html file we can use it like

function callback(response)
{
     var JsonObject= Json.parse(response);
//now we can use JsonObject same Report Object
 var actualReport= JsonObject.ActualReport;
var scheduledReport= JsonObject.ScheduledReport;

}

3. It straight forward to retrun a Dynamically Added Multiple object using json  by following statement:

a) if we are sending list of object we will be able to access these using Indexes
public JsonResult getResult(int numberOfReports)
{
        // Create an object of any kind
      List<Report> reportList= new List<Report>();

        Report report= null;
        for(int i=0;i<numberOfReports;i++){
                 report= new Report(); 
                // set properties to report Object 
                 reportList.Add(report);
        }
       
  
retun Json( reportList);
}

in html file we can use it like

function callback(response)
{
     var JsonObject= Json.parse(response);
//now we can use JsonObject same Report Object
// use JsonObject like
//alert(JsonObject[0]  );

}

b) if we are sending list of object we will be able to access these using Indexes
public JsonResult getResult(int numberOfReports)
{
        // Create an object of any kind
      Dictionary<string,Report> reportList= new Dictionay<string,Report>();

        Report report= null;
        for(int i=0;i<numberOfReports;i++){
                 if(i%2==0){                
                 report= new Report(); 
                // set properties to report Object 
                 reportList.Add("obj"+i.ToString(),report);
               }
               elseif(i%2!=0)
              {
                        report= new Report(); 
                      // set properties to report Object 
                       reportList.Add("obj"+ i.ToString(),report);
               }
        }
       
  
retun Json( reportList);
}

in html file we can use it like

function callback(response)
{
     var JsonObject= Json.parse(response);
//now we can use JsonObject same Report Object
// use JsonObject like
//alert(JsonObject.obj1  );
//alert(JsonObject.obj2  );
// and so on

}

I hope it will help you how can me send JsonResult back to browser.

Happy Living...
Happy Concepts...
Happy Programming...

Monday, January 16, 2012

Exception from HRESULT: 0x80070057 (E_INVALIDARG))

This is very comman issue while we try to run asp.net application we get following error

Could not load file or assembly 'ProjectName' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

So basic reason of this issue is that we havent cleaned the project or temprory files

Do 2 things to remove this issue
1. Clean your project and rebuild the project.
2. After following step 1 if you are still having same issue. check target framework of project and go to following location and delete all files

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files

In My case, project was in .net framework 4.0 so i deleted temporary asp.net files from its child folder. If your project is targeting to some other project delete  Temporary ASP.NET Files from its child folders.

Happy Living...
Happy Concepts...
Happy Programming....