Bineeshcp Viswanath

Bineeshcp Viswanath

  • NA
  • 140
  • 39.2k

How to get Convert Json datetime value in asp.net c#

Apr 17 2019 10:01 AM
Dear All,
 
I have a issue in connecting sql table value into signalr json query.
 
I am loading two value from table - descr(nvarchar) and uploadDate(DateTime).
 
I want to display date value in "dd/MM/yyyy format. Please help me on this.
 
The value from descr is loading perfectly by using below query:-
  1. <script type="text/javascript">  
  2. function getData() {  
  3. var $tbl = $('#NotificationUL');//tbl  
  4. var $Count = $('#NotificationCount');  
  5. $.ajax({  
  6. url: 'wfrmUserPanel.aspx/GetData',  
  7. contentType: "application/json; charset=utf-8",  
  8. dataType: "json",  
  9. type: "POST",  
  10. success: function (data) {  
  11. debugger;  
  12. if (data.d.length > 0) {  
  13. var newdata = data.d;  
  14. // Count = data.rows.Count;  
  15. $tbl.empty();  
  16. //$tbl.append(' <tr><th>Description</th></tr>');  
  17. var rows = [];  
  18. for (var i = 0; i < newdata.length; i++) {  
  19. rows.push(' <li><p>' + newdata[i].Description + ' <span class="timeline-icon"><i class="fa fa-comment-o" style="color: green"></i></span><span class="timeline-date">Date: ' + newdata[i].UploadDate + '</span> </p></li>');  
  20. }  
  21. $tbl.append(rows.join(''));  
  22. }  
  23. }  
  24. });  
  25. }  
  26. </script>  
And the c# code I wrote to trigger this function is given below:-
  1. [WebMethod]  
  2. public static IEnumerable<Products> GetData()  
  3. {  
  4. SqlDataReader reader = null;  
  5. try  
  6. {  
  7. using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString))  
  8. {  
  9. connection.Open();  
  10. //using (SqlCommand command = new SqlCommand(@"SELECT [Description] FROM [dbo].[tblActionDetaills]", connection))  
  11. //{  
  12. using (SqlCommand command = new SqlCommand("SELECT Description,UploadDate FROM tblActionDetaills WHERE IsRead=0 AND VID='" + 1 + "' Order By UploadDate DESC", connection))  
  13. {  
  14. // Make sure the command object does not already have  
  15. // a notification object associated with it.  
  16. command.Notification = null;  
  17. SqlDependency.Start(ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString);  
  18. SqlDependency dependency = new SqlDependency(command);  
  19. dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);  
  20. if (connection.State == ConnectionState.Closed)  
  21. connection.Open();  
  22. using (reader = command.ExecuteReader())  
  23. return reader.Cast<IDataRecord>()  
  24. .Select(x => new Products()  
  25. {  
  26. Description = x.GetString(0),  
  27. UploadDate = x.GetDateTime(1)  
  28. }).ToList();  
  29. }  
  30. }  
  31. }  
I have attached a file where you can see how the value displaying in the web page. Please find that file.

Attachment: JsonNotification.rar

Answers (5)