-2

Hi i am trying to figure out why i get this error only when i try to edit a record in the database but the code has nothing to do with the edit update code. This code that is used to run a stored procedure that is used to pull groups and put the names into a list. It works if you add a record or add multiple records.

List<String> groups = new List<String>(); 

groups = db.spGetCurrentInsuranceGroups().ToList();

/* This where the stored procedure is being called to populate the insurance groups*/
foreach (String group in groups)
{
    <option value="@group">@group</option>
}

here are images of adding a new record and editing. it populates the dropdown list. but if you click select then it wants to give a problem.

adding new record

editing record

as you can see it populates the insurance part that is giving it a problem if you hit the submit to update the record.

here is the error page i get

Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 202: foreach (var group in groups.OfType().ToList())

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271 System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +112 System.Web.WebPages.WebPage.ExecutePageHierarchy() +128 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +131
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +178

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.81.0

Marcus
  • 1
  • 5
  • 2
    Null Reference Exception means your program is encountering a null value when it expects a non-null. What is db.spGetCurrentInsuranceGroups() actually returning? – sschimmel Oct 26 '15 at 15:15
  • The stored procedure is returning the names of the groups in string form. but like i said it works if i run it normally and bring up the page to add a record but gives me that error only when i try to edit an existing record. – Marcus Oct 26 '15 at 15:28

3 Answers3

0

Marcus, your code is too short to get an opinion (I really don´t know exactly what the line 202), but I see you mixing in your code and I think it´s wrong.

But, respectiing your code, you should just reference the group var like this:

   List<String> groups = new List<String>(); 
   groups = db.spGetCurrentInsuranceGroups().ToList();

   foreach (String group in groups)
      {
         <option value="@group">group</option>
      }

Good luck

David BS
  • 1,614
  • 1
  • 14
  • 26
  • David, Thanks for the quick response. The '' code is pulling the name for the values and the inner html text. That's the only difference i see in your code. It works if i run it normally and bring up the page to add a record but gives me that error only when i try to edit an existing record. But that code doesn't have anything to do with editing the record at all. – Marcus Oct 26 '15 at 15:32
  • Hmm... I see... The problem may be related to the Stored Procedure to get records to edit (not your code itself), supposing you checked in database if new records are really appended into. – David BS Oct 26 '15 at 15:53
  • Yeah the records are being appended into the database. I have them being pulled into a dynamic table being created on another page with the edit link in there. then when you click the edit link it takes you to the same page where you add a new record and auto populates the form. – Marcus Oct 26 '15 at 16:23
  • Is the Stored Procedure returning strings, isn´t it? But the ToList() instruction is made to get an array of strings and convert to list. So, are these strings returned by the stored procedure formatted as an array? If not, is there any separator among them (like ; or ,) ? – David BS Oct 26 '15 at 17:24
  • I can run the stored procedure on its own and it works. i added pictures to the original post that shows it is pulling and populating the options. when the submit button is clicked, that is when it throws that error but i even tried to do a redirect before it writes the html code and i still receive the error. – Marcus Oct 26 '15 at 17:51
0

Check your functions, most likely "spGetCurrentInsuranceGroups" is not returning an object to your code. I cannot tell for sure because you have not posted the code that is actually causing the error, but I would be willing to guess the problem is caused by the function returning a null object because of disordered conditions.

Joshua Dannemann
  • 1,841
  • 1
  • 11
  • 30
0

I figured out the problem. when i was passing the information to the update code it wasn't passing the id of the record to change and for some reason i was throwing the error on the foreach loop that was for the insurance.

Thanks everyone for the help and ideas.

Marcus
  • 1
  • 5