Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 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.

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

So any call to View will result in infinite recursion until you either run out of memory or overflow the stack.
 
Share this answer
 
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.
 
Share this answer
 

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