ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.ASP.Net MVC Error: Object reference not set to an instance of an object after form submit<p>Hi&nbsp;nedash,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>For this example I have used of Northwind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-Microsoft-Northwind-and-Pubs-Sample-databases-in-SQL-Server-Management-Studio.aspx" target="_blank">Download Northwind Database</a></p> <p><strong><span style="text-decoration: underline;">Model</span></strong></p> <pre class="brush: csharp">public class AddResellerViewModel { public Reseller Reseller { get; set; } public List&lt;SelectListItem&gt; Ostans { get; set; } public string OstanName { get; set; } } public class Reseller { public int OstanId { get; set; } public string Name { get; set; } public string Address { get; set; } }</pre> <p><strong><span style="text-decoration: underline;">Controller</span></strong></p> <pre class="brush: csharp">public class HomeController : Controller { // GET: /Home/ public ActionResult Index() { var model = new AddResellerViewModel(); model.Ostans = PopulateOstan(); return View(model); } [HttpPost] public ActionResult AddReseller(Reseller reseller) { SqlConnection constr = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings[&#34;Constring&#34;].ConnectionString); using (SqlCommand cmd = new SqlCommand(&#34;InsertReseller&#34;, constr)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = constr; constr.Open(); cmd.Parameters.AddWithValue(&#34;@Name&#34;, reseller.Name); cmd.Parameters.AddWithValue(&#34;@Address&#34;, reseller.Address); cmd.Parameters.AddWithValue(&#34;@Ostanid&#34;, reseller.OstanId); cmd.ExecuteNonQuery(); constr.Close(); } return RedirectToAction(&#34;Index&#34;); } private static List&lt;SelectListItem&gt; PopulateOstan() { SqlConnection constr = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings[&#34;Constring&#34;].ConnectionString); List&lt;SelectListItem&gt; items = new List&lt;SelectListItem&gt;(); using (SqlCommand cmd = new SqlCommand(&#34;SELECT EmployeeID,FirstName + &#39; &#39; + LastName Name FROM Employees&#34;, constr)) { cmd.CommandType = System.Data.CommandType.Text; cmd.Connection = constr; constr.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { items.Add(new SelectListItem { Text = sdr[&#34;Name&#34;].ToString(), Value = sdr[&#34;EmployeeID&#34;].ToString() }); } constr.Close(); } return items; } } }</pre> <p><strong><span style="text-decoration: underline;">View</span></strong></p> <pre class="brush: html">&lt;%@ Page Language=&#34;C#&#34; Inherits=&#34;System.Web.Mvc.ViewPage&lt;_DropDownList_Selected_Insert_MVC.Models.AddResellerViewModel&gt;&#34; %&gt; &lt;!DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.0 Transitional//EN&#34; &#34;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;&gt; &lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;Index&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;% using (Html.BeginForm(&#34;AddReseller&#34;, &#34;Home&#34;, FormMethod.Post)) {%&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;Ostan&lt;/td&gt; &lt;td&gt;&lt;%:Html.DropDownListFor(model =&gt; model.Reseller.OstanId, new SelectList((List&lt;SelectListItem&gt;)Model.Ostans, &#34;Value&#34;, &#34;Text&#34;), &#34;please Select&#34;)%&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Name&lt;/td&gt; &lt;td&gt;&lt;%:Html.TextBoxFor(model =&gt; model.Reseller.Name)%&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Address&lt;/td&gt; &lt;td&gt;&lt;%:Html.TextBoxFor(model =&gt; model.Reseller.Address)%&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br /&gt; &lt;input type=&#34;submit&#34; value=&#34;Save&#34; /&gt; &lt;% } %&gt; &lt;/body&gt; &lt;/html&gt; </pre> <p><strong><span style="text-decoration: underline;">Screenshots</span></strong></p> <p><span style="text-decoration: underline;">The form Data</span></p> <p><span style="text-decoration: underline;"><img src="https://i.imgur.com/8U4F7EL.jpg" alt="" width="251" height="192" /></span></p> <p><span style="text-decoration: underline;">The values in Controller</span></p> <p><span style="text-decoration: underline;"><img src="https://i.imgur.com/oV7yFmL.jpg" alt="" width="472" height="198" /></span></p>https://www.aspforums.net:443/Threads/189581/ASPNet-MVC-Error-Object-reference-not-set-to-an-instance-of-an-object-after-form-submit/https://www.aspforums.net:443/Threads/189581/ASPNet-MVC-Error-Object-reference-not-set-to-an-instance-of-an-object-after-form-submit/Fri, 05 Jul 2019 00:16:52 GMTASP.Net MVC Error: Object reference not set to an instance of an object after form submit<p>Hi&nbsp;<a class="username" rel="nedash">nedash,</a></p> <p>In your GET action method, you are loading data for the DropDownList and sending to the view.</p> <p>But in your HttpPost action method, When you are returning the same view. But you are not passing any model / viewmodel to that which has data for your DropDownList.</p> <p>Your razor view is trying to use the&nbsp;Ostans collection to render the DropDownList, but since Model&nbsp;is NULL you are getting this error.</p> <p>What you should do is before returning the view, reload the Ostans property values again.</p> <pre class="brush: csharp">[HttpPost] public ActionResult AddReseller(Reseller reseller) { SqlConnection constr = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings[&#34;Constring&#34;].ConnectionString); using (SqlCommand cmd = new SqlCommand(&#34;InsertReseller&#34;, constr)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = constr; constr.Open(); cmd.Parameters.AddWithValue(&#34;@Name&#34;, reseller.Name); cmd.Parameters.AddWithValue(&#34;@Address&#34;, reseller.Address); cmd.Parameters.AddWithValue(&#34;@Ostanid&#34;, reseller.OstanId); cmd.ExecuteNonQuery(); constr.Close(); } var model = new AddResellerViewModel(); model.Ostans = PopulateOstan(); return View(model); }</pre> <p>Or simply use RedirectToAction method which i provided earlier in the sample.</p>https://www.aspforums.net:443/Threads/189581/ASPNet-MVC-Error-Object-reference-not-set-to-an-instance-of-an-object-after-form-submit/https://www.aspforums.net:443/Threads/189581/ASPNet-MVC-Error-Object-reference-not-set-to-an-instance-of-an-object-after-form-submit/Mon, 08 Jul 2019 04:14:45 GMT