Loading
Implement data search
  • Thank you Naimish for the response, and I will check and revert please 

  • Naimish Makwana

    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

    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