Click here to Skip to main content
15,888,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Am trying to retrieve the a date from database in a textbox using jQuery but it is displaying values that I could not understand.

this is the code from C#.:

Checkin = Convert.ToDateTime(reader["Checkin"]),


this is the code from JQuery:

$("#<%=txtCheckin.ClientID%>").val(result.reservationInfo[0].Checkin);


so this the value I am getting into textbox: /Date(1419890400000)/

Please could some tell me what is wrong.
Posted

1 solution

I think you need to convert that to date object. Because that is in milliseconds. Zero time is 01 January 1970 00:00:00 UTC. The number is specified in milliseconds.
JavaScript
$("#<%=txtCheckin.ClientID%>").val(new Date(result.reservationInfo[0].Checkin));
 
Share this answer
 
v5
Comments
El Dev 7-Mar-15 4:48am    
I am getting this error: ReferenceError: date is not defined
My bad. "D" should be capital. Answer updated.
El Dev 7-Mar-15 5:04am    
yeah I have updated but now the value of textbox is "invalid date".
Okay, that means there is some problem in parsing the date. Can you tell me what exactly is returned from the database?
El Dev 7-Mar-15 5:15am    
This is my webmethod code from c#:

public static string GetReservationById(int Id)
{
ReservationInfo _ReservationInfo = new ReservationInfo();
_ReservationInfo.ReservationId = Id;
var result = new
{
reservationInfo = _ReservationInfo.GetReservationById(),

};
var serializer = new JavaScriptSerializer();
return serializer.Serialize(result);
}
#endregion


and this the code inside the GetReservationById() method:

public IEnumerable<reservationinfo> GetReservationById()
{
using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), "[GetReservationById]", ReservationId))
{
if (reader.HasRows)
{
while (reader.Read())
{
yield return new ReservationInfo
{
Checkin = Convert.ToDateTime(reader["Checkin"]),
Checkout = Convert.ToDateTime(reader["Checkout"]),
};
}
}
}

}
and this my sp:

ALTER proc [dbo].[GetReservationById]
@ReservationId int
as
begin
select RS.ReservationId,convert(nvarchar(50),RS.Checkin, 101) as Checkin, convert(nvarchar(50),RS.Checkout, 101) Checkout,RS.IsReservedOn, RS.IsModifiedOn,
C.CustomerFirstName, C.CustomerSecondName, C.CustomerEmail, C.CustomerTelephone,
C.CustomerAddressLine1, C.CustomerPOB, C.City, C.Country, R.RoomName,
R.RoomPrice, R.RoomDescription, R.RoomNbrPlace, R.IsAvailable, RT.ReservationType
from Reservation RS
inner join Customer C on C.CustomerId = RS.CustomerId
inner join Room R on R.RoomId = RS.RoomId
inner join ReservationType RT on RT.ReservationTypeID = RS.ReservationTypeId
Where RS.ReservationId = @ReservationId --and R.IsAvailable = 0

end

and this my Jquery code:

function BtnEdit() {

$('a.item-edit').on("click", function (e) {
e.preventDefault();
var $editedtr = $(this).closest("tr").get(0);
showloading();
var aData = $itemTable.fnGetData($editedtr);
$Id = aData[index];
PageMethods.GetReservationById(aData[index], function (response) {
var result = eval("(" + response + ")");
if ($('#<%=txtRoomName.ClientID%>').length > 0) {
$("#<%=txtCheckin.ClientID%>").val(new Date(result.reservationInfo[0].Checkin).toString());
$("#<%=txtCheckout.ClientID%>").val(new Date(result.reservationInfo[0].Checkout));
$("#<%=txtRoomName.ClientID%>").val(result.reservationInfo[0].RoomName);
$("#<%=txtRoomDescription.ClientID%>").val(result.reservationInfo[0].RoomDescription);
$('#ddlRoomType').val();
$('#ddlNbrePlace').val();
if (result.reservationInfo[0].IsDisplayed)
$('#<%=chkbTax.ClientID%>').trigger("click");
}
$('#pnlGrid').hide();
$('#pnlEntry').show();
hideloading();
});
});
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900