Interviews - C# Corner

vishal sohaliya
How to use a Datatable with C#?
By vishal sohaliya in .NET on Apr 27 2019
  • Gajendra Jangid
    Jun, 2019 3

    https://www.c-sharpcorner.com/uploadfile/4d56e1/working-with-the-datatable-class-in-C-Sharp/

    • 1
  • vishal sohaliya
    Apr, 2019 27

    #region GetUser List[HttpPost]public JsonResult UserList(){try{using (CQIBankEntities objCQIBankEntities = new CQIBankEntities()){var draw = Request.Form.GetValues("draw").FirstOrDefault();var start = Request.Form.GetValues("start").FirstOrDefault();var length = Request.Form.GetValues("length").FirstOrDefault();var sortColumn = Request.Form.GetValues("columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();//var SUserId = Request.Form.GetValues("columns[0][search][value]").FirstOrDefault(); //User Idvar SFName = Request.Form.GetValues("columns[0][search][value]").FirstOrDefault();//First Namevar SLName = Request.Form.GetValues("columns[1][search][value]").FirstOrDefault();var SMob = Request.Form.GetValues("columns[2][search][value]").FirstOrDefault();var SMail = Request.Form.GetValues("columns[3][search][value]").FirstOrDefault();var SUserStatus = Request.Form.GetValues("columns[4][search][value]").FirstOrDefault();//User Statusvar SIsActive = Request.Form.GetValues("columns[7][search][value]").FirstOrDefault();//Is Activevar wheresearch = "";if (!string.IsNullOrEmpty(SFName)){wheresearch = "UserName.ToLower().Contains(\"" + SFName.ToLower() + "\") and ";}if (!string.IsNullOrEmpty(SLName)){wheresearch = wheresearch + "LastName.ToLower().Contains(\"" + SLName.ToLower() + "\") and ";}if (!string.IsNullOrEmpty(SMob)){wheresearch = wheresearch + "UserMobile.ToLower().Contains(\"" + SMob.ToLower() + "\") and ";}if (!string.IsNullOrEmpty(SMail)){wheresearch = wheresearch + "UserEmail.ToLower().Contains(\"" + SMail.ToLower() + "\") and ";}if (!string.IsNullOrEmpty(SUserStatus)){//wheresearch = wheresearch + "(UserTypeNm != null and UserTypeNm.ToLower().Contains(\"" + SUserStatus.ToLower() + "\") or (MstUserTypeNm !=null and MstUserTypeNm.ToLower().Contains(\"" + SUserStatus.ToLower() + "\") )) and ";wheresearch = wheresearch + "UserStatus != null and UserStatus.ToLower().Contains(\"" + SUserStatus.ToLower() + "\") and ";}if (!string.IsNullOrEmpty(SIsActive) && SIsActive.Trim().ToUpper() == "Y"){wheresearch = wheresearch + "IsActive=true and ";}if (!string.IsNullOrEmpty(SIsActive) && SIsActive.Trim().ToUpper() == "N"){wheresearch = wheresearch + "IsActive=false and ";}if (!string.IsNullOrEmpty(SIsActive) && SIsActive.Trim().ToUpper() == "Y"){wheresearch = wheresearch + "IsActive=true and ";}if (sortColumnDir == "asc"){sortColumnDir = " ascending";}else{sortColumnDir = " descending";}// wheresearch = wheresearch + "IsDeleted = false";int pageSize = length != null ? Convert.ToInt32(length) : 0;int skip = start != null ? Convert.ToInt32(start) : 0;if (Convert.ToInt32(start) == 0) { start = "1"; } else { start = ((Convert.ToInt32(start) / Convert.ToInt32(length)) + 1).ToString(); }var getUserTypeId = Convert.ToInt32(Session["userTypeid"]);if (getUserTypeId != 0){int recordsTotal1 = objCQIBankEntities.View_User_UserType.Where(x => x.IsDeleted == false && x.UserTypeId == getUserTypeId).Count();var result1 = (from VUM in objCQIBankEntities.View_User_UserTypewhere VUM.UserTypeId == getUserTypeIdselect new { VUM.uid, VUM.UserName, VUM.UserMobile, VUM.LastName, VUM.UserId, VUM.UserEmail, VUM.UserTypeId, VUM.UserTypeNm, VUM.BatchCount, VUM.MasterUserTypeId, VUM.MstUserTypeNm, VUM.IsActive, VUM.UserStatus }).ToList();if (!string.IsNullOrEmpty(wheresearch)){wheresearch = wheresearch.Substring(0, wheresearch.Length - 4);var nresult = result1.Where(wheresearch).OrderBy(sortColumn + sortColumnDir).Skip(skip).Take(pageSize).ToList();return Json(new { draw = draw, recordsFiltered = result1.Where(wheresearch).OrderBy(sortColumn + sortColumnDir).Count(), recordsTotal = recordsTotal1, data = nresult },JsonRequestBehavior.AllowGet);}else{var nresult = result1.OrderBy(sortColumn + sortColumnDir).Skip(skip).Take(pageSize).ToList();return Json(new { draw = draw, recordsFiltered = recordsTotal1, recordsTotal = recordsTotal1, data = nresult },JsonRequestBehavior.AllowGet);}}else{int recordsTotal = objCQIBankEntities.View_User_UserType.Where(x => x.IsDeleted == false).Count();var result = (from VUM in objCQIBankEntities.View_User_UserTypewhere VUM.IsDeleted == falseselect new { VUM.uid, VUM.UserName, VUM.LastName, VUM.UserMobile, VUM.UserId, VUM.UserEmail, VUM.UserTypeId, VUM.UserTypeNm, VUM.BatchCount, VUM.MasterUserTypeId, VUM.MstUserTypeNm, VUM.IsActive, VUM.UserStatus }).ToList();if (!string.IsNullOrEmpty(wheresearch)){wheresearch = wheresearch.Substring(0, wheresearch.Length - 4);var nresult = result.Where(wheresearch).OrderBy(sortColumn + sortColumnDir).Skip(skip).Take(pageSize).ToList();return Json(new { draw = draw, recordsFiltered = result.Where(wheresearch).OrderBy(sortColumn + sortColumnDir).Count(), recordsTotal = recordsTotal, data = nresult },JsonRequestBehavior.AllowGet);}else{var nresult = result.OrderBy(sortColumn + sortColumnDir).Skip(skip).Take(pageSize).ToList();return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = nresult },JsonRequestBehavior.AllowGet);}}}}catch (Exception Ex){return Json(new { draw = 0, recordsFiltered = 0, recordsTotal = 0, data = "", error = "something went wrong please try again." },JsonRequestBehavior.AllowGet);}}#endregion<script type="text/javascript">var vTable;$(document).ready(function () {dtbind();dtSearch('#UserTBL');});function dtbind() {$.fn.dataTable.ext.errMode = 'none';vTable = $("#UserTBL").DataTable({"aoColumnDefs": [{"bSortable": false,"aTargets": ["no-sort"]}],"processing": true, // for show progress bar"serverSide": true, // for process server side"filter": true, // this is for disable filter (search box)"orderMulti": true, // for disable multiple column at onceserverSide: true,"ajax": {"url": "/Admin/UserList","type": "POST","datatype": "json",},"columns": [// { "data": "srNo", "name": "no", "autoWidth": true },//{ "data": "UserId", "name": "UserId", "autoWidth": true },{ "data": "UserName", "name": "UserName", "autoWidth": true },{ "data": "LastName", "name": "LastName", "autoWidth": true },{ "data": "UserMobile", "name": "UserMobile", "autoWidth": true },{ "data": "UserEmail", "name": "UserEmail", "autoWidth": true },//{// "render": function (data, type, full, meta) {// return ' <button class="btn btn-outline-secondary" title="Edit" onclick="return viewprofile(' + full.uid + ')"><i class="fa fa-pencil-square-o mg-r-10"></i>View Profile</button>';// }//},{"name": "UserStatus", "render": function (data, type, full, meta) {if (full.MasterUserTypeId == 1) {return ' <button class="btn btn-outline-secondary" title="Edit" onclick="return viewusertype(' + full.UserTypeId + ')"><i class="fa fa-pencil-square-o mg-r-10"></i>' + full.UserTypeNm + '</button>';}else if (full.MasterUserTypeId == 3) {return ' <button class="btn btn-outline-secondary" title="Edit" onclick="return paperusertype(' + full.UserTypeId + ')"><i class="fa fa-pencil-square-o mg-r-10"></i>' + full.UserTypeNm + '</button>';}else {return ' <button class="btn btn-outline-secondary" title="Edit" onclick="return viewMstusertype(' + full.uid + ')"><i class="fa fa-pencil-square-o mg-r-10"></i>' + full.MstUserTypeNm + '</button>';}}},{"render": function (data, type, full, meta) {if (full.BatchCount != null) {return ' <button class="btn btn-outline-secondary" data-toggle="tooltip" data-placement="top" title="View" onclick="return addUserBatch(' + full.uid + ')"><i class="fa fa-file-text-o mg-r-10"></i> Project Allocation (' + full.BatchCount + ')</button>';}else {return ' <button class="btn btn-outline-secondary" data-toggle="tooltip" data-placement="top" title="Assign" onclick="return addUserBatch(' + full.uid + ')"><i class="fa fa-file-text-o mg-r-10"></i> Project Allocation (0)</button>';}}},{"render": function (data, type, full, meta) {if (full.MasterUserTypeId == 1) {return ' <button class="btn btn-outline-secondary" data-toggle="tooltip" data-placement="top" title="Expertise In" onclick="return Expertise(' + full.uid + ')"><i class="fa fa-info-circle mg-r-10"></i>Proficiency</button>';}else {return '';}}},{"name": "IsActive", "render": function (data, type, full, meta) {if ('@Model.EdtAlwd' == "true") {var ckb = '';var val = '';var setid = full.uid + '?IsActive';if (full.IsActive == true) {ckb = 'checked';val = true;}else {ckb = '';val = false;}return '<label class="switch"><input value="' + val + '" id="' + setid + '" class="switch-input" type="checkbox" onchange="IsActive(' + '\'' + setid + '\'' + ')" ' + ckb + '/><span class="switch-label" data-on="Yes" data-off="No"></span><span class="switch-handle"></span></label>';}else {if (full.IsActive == true) {return 'True';}else {return 'False';}}}},{"render": function (data, type, full, meta) {var d = "";if ('@Model.DltAlwd' == "true") {d += '<a class="fa fa-trash-o fa-lg text-danger mg-r-10 mg-l-10 mg-t-5 pull-right" style="cursor:pointer" title="Delete" onclick="return deleteuser(' + full.uid + ',' + full.BatchCount + ')"></a>';}if ('@Model.EdtAlwd' == "true") {d += '<a class="fa fa-pencil-square-o fa-lg text-info mg-r-10 mg-l-10 mg-t-5 pull-right" style="cursor:pointer" title="Edit" onclick="return edituser(' + full.uid + ')"></a>';}if ('@Model.VwAlwd' == "true") {d += '<a class="fa fa-eye text-muted fa-lg mg-r-10 mg-l-10 mg-t-5 pull-right" style="cursor:pointer" title="Edit" onclick="return ViewUaser(' + full.uid + ')"></a>';}return d;}}],"initComplete": function (settings, json) {}}).on('error.dt', function (e, settings, techNote, message) {if (message.includes("something went wrong please try again.")) {ShowNoti("something went wrong please try again.", "alert-danger2", "0");}});} </script>

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS