Hello Team,
In my MVC C# project, I'm using Json, and Ajax to pass data to the databaase and I want to implement data search engine with 3 textbox and when I search the first name, last name and birthdate in the 3 textbox and if all this data matches then it should export the data from my Sql database as PDF or say data not found, can anyone assist me with a code for that, thank you in advance

Naimish MakwanaRank #324 · 7.7K reputationPosted Sep 9, 2024, 2:58 AM EDT
Sure, I can help you with that! Here’s a step-by-step guide to implement the search functionality and export the data as a PDF in your MVC C# project.
Step 1: Create the View
First, create a view with three textboxes for First Name, Last Name, and Birthdate, and a button to trigger the search.
@{ ViewBag.Title = "Search"; } Search First Name: Last Name: Birthdate: Search function searchData() { var firstName = $('#firstName').val(); var lastName = $('#lastName').val(); var birthdate = $('#birthdate').val(); $.ajax({ url: '@Url.Action("SearchData", "Home")', type: 'POST', data: JSON.stringify({ firstName: firstName, lastName: lastName, birthdate: birthdate }), contentType: 'application/json; charset=utf-8', success: function (response) { if (response.success) { window.location.href = '@Url.Action("ExportToPdf", "Home")'; } else { $('#result').html('Data not found'); } } }); }Step 2: Create the Controller Actions
Next, create the controller actions to handle the search and export functionality.
using System; using System.Linq; using System.Web.Mvc; using YourProjectNamespace.Models; using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; namespace YourProjectNamespace.Controllers { public class HomeController : Controller { private YourDbContext db = new YourDbContext(); [HttpPost] public JsonResult SearchData(string firstName, string lastName, DateTime birthdate) { var data = db.YourTable .Where(x => x.FirstName == firstName && x.LastName == lastName && x.Birthdate == birthdate) .FirstOrDefault(); if (data != null) { TempData["SearchData"] = data; return Json(new { success = true }); } else { return Json(new { success = false }); } } public ActionResult ExportToPdf() { var data = TempData["SearchData"] as YourTable; if (data == null) { return RedirectToAction("Index"); } using (MemoryStream stream = new MemoryStream()) { Document pdfDoc = new Document(PageSize.A4); PdfWriter.GetInstance(pdfDoc, stream).CloseStream = false; pdfDoc.Open(); pdfDoc.Add(new Paragraph("First Name: " + data.FirstName)); pdfDoc.Add(new Paragraph("Last Name: " + data.LastName)); pdfDoc.Add(new Paragraph("Birthdate: " + data.Birthdate.ToString("dd/MM/yyyy"))); pdfDoc.Close(); byte[] bytes = stream.ToArray(); stream.Close(); return File(bytes, "application/pdf", "SearchData.pdf"); } } } }Step 3: Update the Model
Ensure your model matches the database schema.
using System; namespace YourProjectNamespace.Models { public class YourTable { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime Birthdate { get; set; } } }Step 4: Configure the Database Context
Make sure your database context is set up correctly.
using System.Data.Entity; namespace YourProjectNamespace.Models { public class YourDbContext : DbContext { public DbSet YourTable { get; set; } } }Thanks
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Sep 9, 2024, 6:24 AM EDT
Thank you Naimish for the response, and I will check and revert please
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Sep 9, 2024, 7:11 AM EDT
Hello Naimish,
Please it is not working, let me provide you with a section for the code to see.
I'm using AdmineLite bootstrap template, and here is my code.
CONTROLLER
public JsonResult GetAllStaffInfo()
{
var dataList = objRestaurantDBEntities.tblStaffs.ToList();
var modifiedData = dataList.Select(x => new StaffViewModel
{
StaffId = x.StaffId,
StaffImage = x.StaffImage,
JoinDate = x.JoinDate,
FName = x.FName,
LName = x.LName,
BirthDate = x.BirthDate,
Phoneno = x.Phoneno,
Email = x.Email,
NextofKing = x.NextofKing,
Phone = x.Phone
}).ToList();
return Json(modifiedData, JsonRequestBehavior.AllowGet);
}
JAVASCRIPT FUNCTION
EXPORT DATA PDF PAGE
TEMPLATE PAGE FOR LOADINDG THE DATA#