Rahul Patil

Rahul Patil

  • 1.5k
  • 180
  • 29.6k

Issue With Insert Record In Database In Mvc(Asp.NET)

Dec 20 2019 11:50 PM

I have table in SQL Server management studio and then going to Visual Studio -> Server Explorer -> Add connection, my table is added successfully.

Problem is data is not inserted into SQL Server when I press a create button then not any error shown, I think the record is created but when I refresh in SQL Server Management Studio, in the table, the record is not displayed...

  1. Table: empls  
  2.   
  3. empid int primary key  
  4. empname varchar(50)  
  5. empsalary varchar(50)  
  6. empage int  
Class:
  1. [Table("empls")]  
  2. public class stdimg  
  3. {  
  4.     [Key]  
  5.     public int empid { getset; }  
  6.   
  7.     public string empname { getset; }  
  8.     public string empsalary { getset; }  
  9.     public int empage { getset; }  
  10. }  
Context class is the bridge between the database and application:
  1. using System.Data.Entity;  
  2.   
  3. public class studcontext:DbContext  
  4. {  
  5.     public DbSet stds { getset; }  
  6. }  
HomeController:
  1. public class HomeController : Controller  
  2. {  
  3.     studcontext _context = new studcontext();  
  4.   
  5.     [HttpGet]  
  6.     public ActionResult InsertData()  
  7.     {  
  8.         return View();  
  9.     }  
  10.   
  11.     [HttpPost]  
  12.     public ActionResult InsertData(stdimg studs)  
  13.     {  
  14.         _context.stds.Add(studs);  
  15.         _context.SaveChanges();  
  16.         ViewBag.message = "data inserted successfully";   
  17.         return View();  
  18.     }  
  19. }  
InsertData.cshtml:
  1. @model ImageUploadUsingMvc.Models.stdimg    
  2.     
  3. @{    
  4.     ViewBag.Title = "InsertData";    
  5. }    
  6.     
  7. <h2>InsertData</h2>    
  8.     
  9. @using (Html.BeginForm())     
  10. {    
  11.     @Html.AntiForgeryToken()    
  12.     
  13.     <div class="form-horizontal">    
  14.         <h4>stdimg</h4>    
  15.         <hr />    
  16.         @Html.ValidationSummary(true""new { @class = "text-danger" })    
  17.         <div class="form-group">    
  18.             @Html.LabelFor(model => model.empname, htmlAttributes: new { @class = "control-label col-md-2" })    
  19.             <div class="col-md-10">    
  20.                 @Html.EditorFor(model => model.empname, new { htmlAttributes = new { @class = "form-control" } })    
  21.                 @Html.ValidationMessageFor(model => model.empname, ""new { @class = "text-danger" })    
  22.             </div>    
  23.         </div>    
  24.     
  25.         <div class="form-group">    
  26.             @Html.LabelFor(model => model.empsalary, htmlAttributes: new { @class = "control-label col-md-2" })    
  27.             <div class="col-md-10">    
  28.                 @Html.EditorFor(model => model.empsalary, new { htmlAttributes = new { @class = "form-control" } })    
  29.                 @Html.ValidationMessageFor(model => model.empsalary, ""new { @class = "text-danger" })    
  30.             </div>    
  31.         </div>    
  32.     
  33.         <div class="form-group">    
  34.             @Html.LabelFor(model => model.empage, htmlAttributes: new { @class = "control-label col-md-2" })    
  35.             <div class="col-md-10">    
  36.                 @Html.EditorFor(model => model.empage, new { htmlAttributes = new { @class = "form-control" } })    
  37.                 @Html.ValidationMessageFor(model => model.empage, ""new { @class = "text-danger" })    
  38.             </div>    
  39.         </div>    
  40.     
  41.         <div class="form-group">    
  42.             <div class="col-md-offset-2 col-md-10">    
  43.                 <input type="submit" value="Create" class="btn btn-default" />    
  44.             </div>    
  45.         </div>    
  46.     </div>    
  47. }    
  48. <div>    
  49.     @Html.ActionLink("Back to List""Index")    
  50. </div>    
  51.     
  52. <script src="~/Scripts/jquery-1.10.2.min.js"></script>    
  53. <script src="~/Scripts/jquery.validate.min.js"></script>    
  54. <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>  

I already created a database in SQL Server Management Studio, and adding a database in my project successfully. But when I click on the create button, the code does not generate any errors, but after a refresh of my database, the record is not shown....

I put the viewbag.message="record successfully" the message did not display and record not display in SQL Server?

Can someone please help? What am I doing wrong?


Answers (10)