Click here to Skip to main content
14,298,693 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have created a CRUD operation. In the controller page i am getting this error.
In the con.Open(); line. Can you please find and help me out.

What I have tried:

public ActionResult View(GetUserData getUserData)
             {

                List<GetUserData> GetUserData = new List<GetUserData>();
                string DefaultConnection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                string query = "SELECT Email,UserName FROM AspNetUsers";
                using (SqlConnection con = new SqlConnection(DefaultConnection))
                {                   
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.Connection = con;
                        con.Open();
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                            while (sdr.Read())
                            {
                                GetUserData.Add(new GetUserData
                                {
                                    UserName = Convert.ToString(sdr["UserName"]),
                                    Email = Convert.ToString(sdr["Email"])
                                });
                            }
                        }
                    }
                    con.Close();
                }
                return View(getUserData);
             }
Posted
Updated 9-Sep-19 6:17am
Comments
F-ES Sitecore 9-Sep-19 11:32am
   
Normally you'll return View(...) where ... is the path to a cshtml file, or just View() which lets MVC work out the path based on the name. If those aren't working due to a conflict with the fact that you have called your action "View" then the easiest solution is probably to rename your action to something else.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You have the following return statement at the end of your View method:
return View(getUserData);

So any call to View will result in infinite recursion until you either run out of memory or overflow the stack.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

MVC has a class called "View", so you naming your method "View" was a bad idea.

It's also non-descriptive of what the method is supposed to return. Rename your method to something else and it'll take care of the infinite recursion problem.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100