Hi rani,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespaces
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public JsonResult GetEmployees()
{
List<Employee> employees = new List<Employee>();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("SELECT TOP 3 * FROM Employees");
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
employees.Add(new Employee
{
ID = Convert.ToInt32(sdr["EmployeeID"]),
Name = sdr["FirstName"] + " " + sdr["LastName"],
Address = sdr["Address"] + "," + sdr["City"] + "," + sdr["Country"],
ImageBase64 = ImageToBase64String(sdr["Photo"]),
DOB = Convert.ToDateTime(sdr["BirthDate"]).ToString("MM/dd/yyyy")
});
}
}
con.Close();
}
return Json(employees, JsonRequestBehavior.AllowGet);
}
private static string ImageToBase64String(object imageData)
{
byte[] bytes = null;
System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
System.Drawing.Image img = (System.Drawing.Image)converter.ConvertFrom((byte[])imageData);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
bytes = ms.ToArray();
}
return Convert.ToBase64String(bytes, 0, bytes.Length);
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string ImageBase64 { get; set; }
public string DOB { get; set; }
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.1.114/styles/kendo.default-v2.min.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="https://kendo.cdn.telerik.com/2020.1.114/js/angular.min.js"></script>
<script type="text/javascript" src="https://kendo.cdn.telerik.com/2020.1.114/js/kendo.all.min.js"></script>
<script type="text/javascript">
$(function () {
$("#txtDate").kendoDatePicker({
format: "dd/MM/yyyy",
dateInput: true,
value: new Date()
});
$("#tblEmployees").kendoGrid({
dataSource: {
transport: { read: "/Home/GetEmployees/" },
pageSize: 5
},
height: 320,
pageable: { refresh: true, pageSizes: [2, 25, 50] },
groupable: false,
sortable: true,
columns: [
{ field: "ID", title: "ID", width: 30 },
{ field: "Name", title: "Name", width: 90 },
{ field: "Address", title: "Address", width: 120 },
{ field: "DOB", title: "Birth Date", width: 60 },
{
template: "<img src='data:image/jpg;base64,#:data.ImageBase64#' style='width:50px;height:50px;border-radius: 30%;' />",
title: "Photo", width: 50
},
{
title: "Details", lock: true, width: "10%",
command: [{
name: "View", text: "", iconClass: "k-icon k-i-preview",
click: function (e) {
var tr = $(e.target).closest("tr");
var dob = this.dataItem(tr).DOB;
var kendoDateTimePicker = $("#txtDate").data("kendoDatePicker");
kendoDateTimePicker.destroy();
$("#dvDate").empty();
$("#dvDate").append('<input id="txtDate" />');
$("#txtDate").kendoDatePicker({
format: "dd/MM/yyyy",
dateInput: true,
value: new Date(dob)
});
}
}]
}
]
});
});
</script>
</head>
<body>
<div id="dvDate">
<input id="txtDate" />
</div>
<hr />
<div id="tblEmployees"></div>
</body>
</html>
Screenshot