Insert Data In Database Table | CRUD App In ASP.NET MVC Using ADO.NET | CRUD App
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace CRUDAppUsingADO.Models
{
public class Employee
{
public int id { get; set; }
[Required]
public string name { get; set; }
[Required]
public string gender { get; set; }
[Required]
public int age { get; set; }
[Required]
public int salary { get; set; }
[Required]
public string city { get; set; }
}
}
Above File is CRUDAppUsingADO\Models\Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using CRUDAppUsingADO.Models;
namespace CRUDAppUsingADO
{
public class EmployeeDBContext
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
public List<Employee> GetEmployees()
{
List<Employee> EmployeeList = new List<Employee>();
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("spGetAllEmployees", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
Employee emp = new Employee();
emp.id = Convert.ToInt32(dr.GetValue(0).ToString());
emp.name = dr.GetValue(1).ToString();
emp.gender = dr.GetValue(2).ToString();
emp.age = Convert.ToInt32(dr.GetValue(3).ToString());
emp.salary = Convert.ToInt32(dr.GetValue(4).ToString());
emp.city = dr.GetValue(5).ToString();
EmployeeList.Add(emp);
}
con.Close();
return EmployeeList;
}
public bool AddEmployee(Employee emp)
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("spAddEmployee",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name",emp.name);
cmd.Parameters.AddWithValue("@gender",emp.gender);
cmd.Parameters.AddWithValue("@age",emp.age);
cmd.Parameters.AddWithValue("@salary",emp.salary);
cmd.Parameters.AddWithValue("@city",emp.city);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return true;
}
else
{
return false;
}
}
}
}
Above File is CRUDAppUsingADO\EmployeeDBContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CRUDAppUsingADO.Models;
namespace CRUDAppUsingADO.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
EmployeeDBContext db = new EmployeeDBContext();
List<Employee> obj = db.GetEmployees();
return View(obj);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Employee emp)
{
try
{
if (ModelState.IsValid == true)
{
EmployeeDBContext context = new EmployeeDBContext();
bool check = context.AddEmployee(emp);
if (check == true)
{
TempData["InsertMessage"] = "Data has been Inserted Successfully.";
ModelState.Clear();
return RedirectToAction("Index");
}
}
return View();
}
catch (Exception ex)
{
return View();
}
}
}
}
Above File is CRUDAppUsingADO\Controllers\HomeController.cs
@model IEnumerable<CRUDAppUsingADO.Models.Employee>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@if (TempData["InsertMessage"] != null)
{
<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>Success!</strong> @TempData["InsertMessage"]
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table table-bordered table-hover table-striped">
<tr>
<th>
@Html.DisplayNameFor(model => model.id)
</th>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.gender)
</th>
<th>
@Html.DisplayNameFor(model => model.age)
</th>
<th>
@Html.DisplayNameFor(model => model.salary)
</th>
<th>
@Html.DisplayNameFor(model => model.city)
</th>
<th>Operations</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.id)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.age)
</td>
<td>
@Html.DisplayFor(modelItem => item.salary)
</td>
<td>
@Html.DisplayFor(modelItem => item.city)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
</table>
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment