0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Our_Web_Project.Models.Entitiy; 

namespace Our_Web_Project.Controllers
{​​​​​
    public class GonderiController : Controller
    {​​​​​
        web_siteEntities db = new web_siteEntities();

        // GET: Gonderi
        public ActionResult Index()
        {​​​​​
            return View();
        }​​​​​         

        public ActionResult Show()
        {​​​​​
            var degerler = db.Posts.ToList();

            return View(degerler);
        }​​​​​          

        public ActionResult Create()
        {​​​​​  
            return View();
        }​​​​​ 

        [HttpPost]
        public ActionResult Create(FormCollection form)
        {​​​​​
            Posts model = new Posts
            {​​​​​
                PostDescription = form["PostDescription"].Trim(),
                PostComment = form["PostComment"].Trim(),
            };

            db.Posts.Add(model);
            db.SaveChanges();

            return View();
        }​​​​​   
    }​​​​​
}​​​​​

I used form structure to insert data. I am trying to insert PostDescription and PostComment data into SQL. But it shows this error:

System.NullReferenceException Object reference not set to an instance of an object
HResult=0x80004003 line 37

I'm using ASP.NET MVC. What should I do?

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
rik8585
  • 1
  • 1
  • Seems like some of your data at "Posts model = new Posts {​​​​​ PostDescription = form["PostDescription"].Trim(), PostComment = form["PostComment"].Trim(), }​​​​​;" might be not initialized, Have you tried debugging? – Ghaamae Oct 26 '21 at 20:18

0 Answers0