using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using Application.Models;
using System.Text;
namespace Application.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult InsertUpdateSudentDetails(string studentId, string studentName, string emailId,
string dob, string gender)
{
Dictionary<string, string> Dic = new Dictionary<string, string>();
Dic["Result"] = "";
Dic["Status"] = "0";
DataTable dt = new DataTable();
try
{
if (studentName.Trim() == "")
{
Dic["Result"] = "Please enter your Name.";
}
else if (emailId.Trim() == "")
{
Dic["Result"] = "Please enter your emailId.";
}
else if (dob.Trim() == "")
{
Dic["Result"] = "Please enter DOB.";
}
else if (gender.Trim() == "")
{
Dic["Result"] = "Please enter your gender";
}
else
{
string[,] Param = new string[,]
{
{"@StudentId",studentId.Trim()},
{"@StudentName",studentName.Trim()},
{"@Email",emailId.Trim()},
{"@DOB",dob},
{"@Gender",gender.Trim()},
};
dt = SQLhelper.ExecuteProcedure(Param, "USP_InsertUpdateStudentData");
if (dt.Rows.Count > 0)
{
Dic["Result"] = dt.Rows[0]["result"].ToString();
Dic["Status"] = dt.Rows[0]["Status"].ToString();
}
}
}
catch (Exception Ex)
{
Dic["Result"] = Ex.Message;
}
var jsonResult = Json(Dic, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
[HttpPost]
public JsonResult ShowData()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic["Result"] = "";
dic["Grid"] = "";
StringBuilder htmlCode = new StringBuilder();
try
{
string[,] Param = new string[,]
{
{"@StudentID","0"},
};
DataTable Dt = SQLhelper.ExecuteProcedure(Param, "USP_ShowStudentDetails");
if (Dt.Rows.Count > 0)
{
htmlCode.Append("<table class='table table-responsive table-bordered table-stripped table-hover'>");
htmlCode.Append("<tr>");
htmlCode.Append("<th>Sr.No</th>");
htmlCode.Append("<th>StudentName</th>");
htmlCode.Append("<th>Email</th>");
htmlCode.Append("<th>DOB</th>");
htmlCode.Append("<th>Gender</th>");
htmlCode.Append("<th>CreatedDate</th>");
htmlCode.Append("<th>ModifedDate</th>");
htmlCode.Append("<th>Edit</th>");
htmlCode.Append("<th>Del</th>");
htmlCode.Append("</tr>");
int a=1;
foreach (DataRow rw in Dt.Rows)
{
htmlCode.Append("<tr>");
htmlCode.Append("<td>"+a+"</td>");
htmlCode.Append("<td>" + rw["StudentName"].ToString() + "</td>");
htmlCode.Append("<td>" + rw["Email"].ToString() + "</td>");
htmlCode.Append("<td>" + rw["DOB"].ToString() + "</td>");
htmlCode.Append("<td>" + rw["Gender"].ToString() + "</td>");
htmlCode.Append("<td>" + rw["CreatedDate"].ToString() + "</td>");
htmlCode.Append("<td>" + rw["ModifiedDate"].ToString() + "</td>");
htmlCode.Append("<td><i class='fa fa-edit' style='font-size: 22px;' onclick='EditData(" + rw["StudentID"].ToString() + ")'></i></td>");
htmlCode.Append("<td><i class='fa fa-trash' style='font-size: 22px;' onclick='DeleteData(" + rw["StudentID"].ToString() + ")'></i></td>");
htmlCode.Append("</tr>");
a++;
}
htmlCode.Append("</table>");
}
dic["Grid"] = htmlCode.ToString();
}
catch (Exception Ex)
{
dic["Result"] = Ex.Message;
}
var jsonResult = Json(dic, JsonRequestBehavior.AllowGet);
jsonResult.MaxJsonLength = int.MaxValue;
return jsonResult;
}
public JsonResult EditData(string StudentId)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic["Result"] = "";
try
{
string[,] Param = new string[,]
{
{"@StudentID ",StudentId}
};
DataTable dt = SQLhelper.ExecuteProcedure(Param, "USP_ShowStudentDetails");
if (dt.Rows.Count > 0)
{
dic["StudentID"] = dt.Rows[0]["StudentID"].ToString();
dic["StudentName"] = dt.Rows[0]["StudentName"].ToString();
dic["Email"] = dt.Rows[0]["Email"].ToString();
dic["Gender"] = dt.Rows[0]["Gender"].ToString();
dic["dob"] = dt.Rows[0]["DOB"].ToString();
}
}
catch (Exception ex)
{
dic["Result"] = ex.Message;
}
return Json(dic);
}
[HttpPost]
public JsonResult DeleteData(string StudentId)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic["Result"] = "";
try
{
string[,] Param = new string[,]
{
{"@StudentID",StudentId},
};
DataTable dt = SQLhelper.ExecuteProcedure(Param, "USP_Delete_StudentDetails");
if (dt.Rows.Count > 0)
{
dic["Result"] = dt.Rows[0]["result"].ToString();
}
}
catch (Exception ex)
{
dic["Result"] = ex.Message;
}
return Json(dic);
}
}
}