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...
- Table: empls
- empid int primary key
- empname varchar(50)
- empsalary varchar(50)
- empage int
- [Table("empls")]
- public class stdimg
- {
- [Key]
- public int empid { get; set; }
- public string empname { get; set; }
- public string empsalary { get; set; }
- public int empage { get; set; }
- }
- using System.Data.Entity;
- public class studcontext:DbContext
- {
- public DbSet
stds { get; set; } - }
- public class HomeController : Controller
- {
- studcontext _context = new studcontext();
- [HttpGet]
- public ActionResult InsertData()
- {
- return View();
- }
- [HttpPost]
- public ActionResult InsertData(stdimg studs)
- {
- _context.stds.Add(studs);
- _context.SaveChanges();
- ViewBag.message = "data inserted successfully";
- return View();
- }
- }
- @model ImageUploadUsingMvc.Models.stdimg
- @{
- ViewBag.Title = "InsertData";
- }
InsertData
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
- class="form-horizontal">
stdimg
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
class="form-group">- @Html.LabelFor(model => model.empname, htmlAttributes: new { @class = "control-label col-md-2" })
class="col-md-10">- @Html.EditorFor(model => model.empname, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.empname, "", new { @class = "text-danger" })
class="form-group">- @Html.LabelFor(model => model.empsalary, htmlAttributes: new { @class = "control-label col-md-2" })
class="col-md-10">- @Html.EditorFor(model => model.empsalary, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.empsalary, "", new { @class = "text-danger" })
class="form-group">- @Html.LabelFor(model => model.empage, htmlAttributes: new { @class = "control-label col-md-2" })
class="col-md-10">- @Html.EditorFor(model => model.empage, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.empage, "", new { @class = "text-danger" })
class="form-group">class="col-md-offset-2 col-md-10">- "submit" value="Create" class="btn btn-default" />
- }
- @Html.ActionLink("Back to List", "Index")
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?
Jignesh KumarPosted Dec 22, 2019, 1:17 AM
Rajanikant HawaldarPosted Dec 22, 2019, 1:09 AM
Rajeesh MenothPosted Dec 22, 2019, 12:56 AM
Laxmidhar SahooPosted Dec 22, 2019, 12:14 AM
Rahul PatilPosted Dec 21, 2019, 11:29 PM
Laxmidhar SahooPosted Dec 21, 2019, 4:47 AM
Rajeesh MenothPosted Dec 21, 2019, 2:15 AM
Rahul PatilPosted Dec 21, 2019, 1:37 AM
Rahul PatilPosted Dec 21, 2019, 1:17 AM
Laxmidhar SahooPosted Dec 21, 2019, 12:53 AM