Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my MVC program I need to grab datas one by one.But I'm facing "argumentexception was unhandled by user code" exception.I cannot solve it.The exception is coming on
int len=obj.sort.Count()
How can I solve this.

What I have tried:

public class KendoSort
{
public string field { get; set; }
public string dir { get; set; }
public string compare { get; set; }
}

public class dataObject
{
public int page { get; set; }
public int pageSize { get; set; }
public int take { get; set; }
public int skip { get; set; }

public List<kendosort> sort { get; set; }

}

//
// GET: /License/GetLicView
// JSON Result
[Authorize]
public ActionResult GetLicView(dataObject obj)
{
if (Session["FirstName"] != null && Session["userId"] != null && Session["guid"] != null)
{
string guid = (string)Session["Guid"];
List<getalllicensesdetailsforroles_result> lic = new List<getalllicensesdetailsforroles_result>();
lic = manager.GetuserLicensesDetails(guid, user.getuserrole(Guid.Parse(guid)));

int take = 20;
int skip = 0;

take = obj.take;
skip = obj.skip;
int end = take+skip; // 0+20

int len = obj.sort.Count();
for (int i = 0; i < len; i++)
{
var item = obj.sort[i];

if (item.field == "Start_Date" && item.dir == "asc")
{
lic = lic.OrderBy(x => x.Start_Date).ToList();
}
else
{
lic = lic.OrderByDescending(x => x.Start_Date).ToList();
}
}
Posted
Updated 8-Aug-19 2:06am
v2

Two ways: the "proper way" and the "time consuming way"

The proper way is easy in the long run, but requires a little effort from you to learn how to use the debugger.
The time consuming way involves manually adding try ... catch blocks throughout your code and reporting errors to a log file or teh user, and adding more as you narrow down where the problem is occurring and start to work out why. Hence the name. It's effective, but ... the proper way is quicker in the long run (and often in the short term as well).

So we'll concentrate on the proper way.
Run your app in the debugger.
When it hits the exception, it will stop and display a message explaining what happened - this will be the same as the message you have, but since you are in the debugger, you will now have control over the code and can look at why, and specifically at the arguments provided to whatever method you called.
In the debugger you can look at variable values, change them, run code from a different place - even edit your code and carry on! Loads of stuff which helps you work out what you did wrong!

Google will help you if you just search for "visual studio debugger tutorial" and start thinking about your code and how it failed.
 
Share this answer
 
I found the solution using if condition.Like this.
if (obj.sort != null)
               {
                   int len = obj.sort.Count();


                   for (int i = 0; i < len; i++)
                   {
                       var item = obj.sort[i];
                          ..................................
                          ............................



Now the exception is gone.
 
Share this answer
 
Comments
Richard Deeming 8-Aug-19 10:43am    
Since the sort property is a list, if you'd used the .Count property instead of the .Count() extension method, you'd have got a NullReferenceException instead of an ArgumentNullException. That might have made it easier to spot the problem. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900