Click here to Skip to main content
15,301,624 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,

I am working on a MVC project, I currently finished three different excel sheets and depending on the type of insurance the user selects a certain default excel sheet format should be used for that insurance.
Currently I can only select one format in the return statement. What I was trying to do is have an if condition depending the string context to decide what excel format to use.

public ActionResult GenerateTripLog(int driverId, int vehicleId, int typeId, int customerId)
                var results = (from s in db.usp_TripLog(driverId, vehicleId, typeId, customerId, sDate, eDate) 
                               select s).ToList();

                var driver = new DriverRepository(GetCurrentUser()).GetDriver(driverId);
                var iType = db.insurancetypes.SingleOrDefault(c => == typeId);
                var tenant = GetTenant(GetCurrentUser());                
                var period = "All";           

                var driverSignature = 
                                (from s in driver.driveravailabilityevents
                                orderby s.createdon descending
                                select s.signature).FirstOrDefault();

                object[] filterStrs =   
                    tenant == null ? "All" : tenant.tenantname,
                    driver == null || driver.person == null ? "All" : driver.person.fullname,
                    iType == null ? "All" :,
                string insuranceType = Convert.ToString(iType);
                if(insuranceType == "FAMILY CARE")
                    return TripLogSpreadsheet(results, filterStrs);
                else if(insuranceType == "BELLIN")
                    return BELLINLogSpreadsheet(results, filterStrs);

      //return TripLogSpreadsheet(results, filterStrs);
      catch (Exception ex)
            ViewData["errormessage"] = ex.Message;
            return View("Error");
DotNetSteve 17-Nov-15 17:23pm
Take a look at this link - Might help.

Look at FileResult types (FileResult,FileContentResult,FilePathResult,FileStreamResult)
Sinisa Hajnal 18-Nov-15 5:02am
You should probably override (inherit from) ActionResult with your specific result. But I admit I never had the pleasure of trying it.
TheBigBearNow 18-Nov-15 10:38am
My issue is my method that GenerateTripLog() wants to return a single value, when I have my ‘if’ condition it will return a value depending on the condition, While the if is being used because the returns are in the if the method throws an error because it says not all paths return a value for the method.
DotNetSteve 18-Nov-15 12:45pm
You need to return something in the case it is neither of your values - you could return a view in the Finally block or simply at the end of the function.
TheBigBearNow 18-Nov-15 16:18pm
I believe this may work for me.

Is there a better suggestion?

string insuranceType = Convert.ToString(iType);
string akeString = "";
string logicstr = "";
string bellinStr = "";
if (insuranceType.Length > 2) akeString = insuranceType.Substring(0, 3);
if (insuranceType.Length > 10) logicstr = insuranceType.Substring(0, 11);
if (insuranceType.Length > 10) bellinStr = insuranceType.Substring(0, 11);
if (logicstr == "Logisticare")
return TripLogSpreadsheet(results, filterStrs);//logisticare;//
else if (akeString.ToUpper() == "ake")
return MTMLogSpreadsheet(results, filterStrs);
else if (insuranceType == "Bellin")
return BellinLogSpreadsheet(results, filterStrs);
return TripLogSpreadsheet(results, filterStrs);
DotNetSteve 18-Nov-15 17:43pm
Looks like it would get you past the not all paths return a value. I was going to suggest changing to a switch statement but note you are testing for different case (a string like "ake" and insuranceType == 'Bellin') but it would not work for you. Is there a way you can switch on something more consistently?
Sinisa Hajnal 19-Nov-15 2:24am
You have if (insuranceType.Length > 10) check twice.

You don't need the first if that returns TripLogSpreadsheet since it will be returned if no ifs are satisfied (default) so you can safely remove it. Unless there are logisticare items that would satisfy one of the other ifs?
TheBigBearNow 19-Nov-15 10:00am
now when i have a list of clients i get a dictionary error such as -

Now one issue I have come across is when the list of clients with the same driver and same insurance is long for example 22. The program gives an error about System.Web.Mvc.ViewDataDictionary.

“A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in System.dll

'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/5/ROOT-1-130923551687352717): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.DebuggerVisualizers\\Microsoft.VisualStudio.DebuggerVisualizers.dll'. Cannot find or open the PDB file.”

This only happens when there is a list of clients whenever it is small the excel template opens with no problems.

I tried to do this Options dialog box -> Debugging node -> Symbols with enabling symbols but still crashes.

I have my condition working its like above just changed a little but now i have this problem with the list of clients is bigger then a couple
Sinisa Hajnal 20-Nov-15 2:25am
I'd hazard a guess :) Somehow you're using dictionary items without checking if the key exists :) You see, KeyNotFoundException...There is .Contains(key) method you can use to avoid this kind of error, you just have to find it now. Good luck

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