简体   繁体   中英

My ASP.NET Core MVC application stopped saving changes

My ASP.NET Core MVC application (written in C#) stopped for some reason to save changes in database.

It reads perfectly and there is no any error related to connection itself. It worked perfectly yesterday but today it is not longer working. What is going on in here? Could it be related to a "refresh" problem.

This is my controller code:

// POST: Approver/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, [Bind] Approver approver)
{
    try
    {
        if (ModelState.IsValid)
        {
            dbContext.UpdateApprover(approver);
            return RedirectToAction("Index");
        }

        return View(dbContext);
    }
    catch
    {
        return View();
    }
}

This is my CONTEXT update method:

public void UpdateApprover(Approver approver)
{
     using (SqlConnection con = new SqlConnection(connectionString))
     {
         SqlCommand cmd = new SqlCommand("Stored_procedure_update", con)
            {
                CommandType = System.Data.CommandType.StoredProcedure
            };

         cmd.Parameters.AddWithValue("@CODE", approver.CODE);
         cmd.Parameters.AddWithValue("@MAIL", approver.MAIL);
         cmd.Parameters.AddWithValue("@NAME", approver.NAME);

         con.Open();
         cmd.ExecuteNonQuery();
         con.Close();
     }
}

Am I missing something?


CONNECTION FILE:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using MyApplication.Models;

namespace MyApplication.Context
{
    public class Connection
    {
        string connectionString = "Data Source=xxxxxxx;Initial Catalog = ''; Persist Security Info=True;User ID = ''; Password='xxxxxx'";

        /// <summary>
        /// </summary>
        /// <returns></returns>

        public IEnumerable<Approver> GetAllApprovers()
        {
            var ApproversList = new List<Approver>();
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_GetAllApprovers", con)
                {
                    CommandType = System.Data.CommandType.StoredProcedure
                };
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    var Approver = new Approver
                    {
                        CODE = Convert.ToInt32(dr["CODE"].ToString()),
                        MAIL = dr["MAIL"].ToString(),
                        FIRSTNAME = dr["FIRSTNAME"].ToString(),
                        LASTNAME = dr["LASTNAME"].ToString()
                    };

                    ApproversList.Add(Approver);
                }
                con.Close();
            }

            return ApproversList;

        }

        public void UpdateApprover(Approver Approver)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_Update_Approver", con)
                {
                    CommandType = System.Data.CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@CODE", Approver.CODE);
                cmd.Parameters.AddWithValue("@MAIL", Approver.MAIL);
                cmd.Parameters.AddWithValue("@FIRSTNAME", Approver.FIRSTNAME);
                cmd.Parameters.AddWithValue("@LASTNAME", Approver.LASTNAME);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }


        }

        public Approver GetApproverById(int? id)
        {
            var Approver = new Approver();
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_GetapproverById", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@CODE", id);

                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    Approver.CODE = Convert.ToInt32(dr["CODE"].ToString());
                    Approver.MAIL = dr["MAIL"].ToString();
                    Approver.FIRSTNAME = dr["FIRSTNAME"].ToString();
                    Approver.LASTNAME = dr["LASTNAME"].ToString();
                }

                con.Close();
            }

            return Approver;

        }


    }
}

CONTROLLER FILE:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyApplication.Context;
using MyApplication.Models;

namespace MyApplication.Controllers
{
    public class ControllerApprover : Controller
    {
        readonly Connection dbContext = new Connection();

        // GET: Approver
        public ActionResult Index()
        {
            List<Approver> ApproversList = dbContext.GetAllApprovers().ToList();
            return View(ApproversList);
        }

        // GET: Approver/Details/5
        public ActionResult Details(int id)
        {
            Approver approver = dbContext.GetApproverById(id);
            return View(approver);
        }

        // GET: Approver/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Approver/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(int id, [Bind] Approver approver)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    dbContext.UpdateApprover(Approver);
                    return RedirectToAction("Index");
                }

                return View(dbContext);
            }
            catch
            {
                return View();
            }
        }

        // GET: Approver/Edit/5
        public ActionResult Edit(int id)
        {
            Approver Approver = dbContext.GetApproverById(id);
            return View(approver);
        }

        // POST: Approver/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, [Bind] Approver approver)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    dbContext.UpdateApprover(approver);
                    return RedirectToAction("Index");
                }

                return View(dbContext);
            }
            catch
            {
                return View();
            }
        }

    }
}

My appsettings.json:

  {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*"}

This should be in the comment section, but I can't comment right now, need 50 reputation. But I think, you could try first see if there is an error on your catch block, change it to catch (Exception e) to see if there is something there. Log it, or put a breakpoint in debugging mode.

Correctly pick your connection string and change your CONTEXT method to something like that, if still not work.

private IConfiguration Configuration;
public HomeController(IConfiguration _configuration)
    {
        Configuration = _configuration;
    }





public void UpdateApprover(Approver approver)
{

  string connectionString=  Configuration.GetConnectionString("YOUR CONNECTION STRING NAME");
     using (SqlConnection con = new SqlConnection(connectionString))
     {
       con.Open();
        using (SqlCommand cmd = new SqlCommand("Stored_procedure_update", con))
            {
                CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@CODE", approver.CODE);
                cmd.Parameters.AddWithValue("@MAIL", approver.MAIL);
                cmd.Parameters.AddWithValue("@NAME", approver.NAME);

         
         cmd.ExecuteNonQuery();
         con.Close();
            }
     }
}


CONNECTION FILE:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using MyApplication.Models;

namespace MyApplication.Context
{
    public class Connection
    {
        string connectionString = "Data Source=xxxxxxx;Initial Catalog = ''; Persist Security Info=True;User ID = ''; Password='xxxxxx'";

        /// <summary>
        /// </summary>
        /// <returns></returns>

        public IEnumerable<Approver> GetAllApprovers()
        {
            var ApproversList = new List<Approver>();
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_GetAllApprovers", con)
                {
                    CommandType = System.Data.CommandType.StoredProcedure
                };
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    var Approver = new Approver
                    {
                        CODE = Convert.ToInt32(dr["CODE"].ToString()),
                        MAIL = dr["MAIL"].ToString(),
                        FIRSTNAME = dr["FIRSTNAME"].ToString(),
                        LASTNAME = dr["LASTNAME"].ToString()
                    };

                    ApproversList.Add(Approver);
                }
                con.Close();
            }

            return ApproversList;

        }

        public void UpdateApprover(Approver Approver)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_Update_Approver", con)
                {
                    CommandType = System.Data.CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@CODE", Approver.CODE);
                cmd.Parameters.AddWithValue("@MAIL", Approver.MAIL);
                cmd.Parameters.AddWithValue("@FIRSTNAME", Approver.FIRSTNAME);
                cmd.Parameters.AddWithValue("@LASTNAME", Approver.LASTNAME);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }


        }

        public Approver GetApproverById(int? id)
        {
            var Approver = new Approver();
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("SP_GetapproverById", con);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@CODE", id);

                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    Approver.CODE = Convert.ToInt32(dr["CODE"].ToString());
                    Approver.MAIL = dr["MAIL"].ToString();
                    Approver.FIRSTNAME = dr["FIRSTNAME"].ToString();
                    Approver.LASTNAME = dr["LASTNAME"].ToString();
                }

                con.Close();
            }

            return Approver;

        }


    }
}

CONTROLLER FILE:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyApplication.Context;
using MyApplication.Models;

namespace MyApplication.Controllers
{
    public class ControllerApprover : Controller
    {
        readonly Connection dbContext = new Connection();

        // GET: Approver
        public ActionResult Index()
        {
            List<Approver> ApproversList = dbContext.GetAllApprovers().ToList();
            return View(ApproversList);
        }

        // GET: Approver/Details/5
        public ActionResult Details(int id)
        {
            Approver approver = dbContext.GetApproverById(id);
            return View(approver);
        }

        // GET: Approver/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Approver/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(int id, [Bind] Approver approver)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    dbContext.UpdateApprover(Approver);
                    return RedirectToAction("Index");
                }

                return View(dbContext);
            }
            catch
            {
                return View();
            }
        }

        // GET: Approver/Edit/5
        public ActionResult Edit(int id)
        {
            Approver Approver = dbContext.GetApproverById(id);
            return View(approver);
        }

        // POST: Approver/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, [Bind] Approver approver)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    dbContext.UpdateApprover(approver);
                    return RedirectToAction("Index");
                }

                return View(dbContext);
            }
            catch
            {
                return View();
            }
        }

    }
}

My appsettings.json:

  {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM