search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

Change Date Format

Related article: ASP.NET Scheduler Tutorial
Asked by Amit Tiwari
8 years ago.

how to change "DayPilotScheduler1.VisibleStart" date format in dd/MM/yyyy To yyyy-MM-dd.

Answer posted by Dan Letecky [DayPilot]
8 years ago.

DayPilotScheduler1.VisibleStart is a DateTime object so you can get a custom formatted string from it using ToString():

DayPilotScheduler1.VisibleStart.ToString("yyyy-MM-dd");

See also here:
https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx

Comment posted by Amit Tiwari
8 years ago.

How to Solve it?
Error is :-

Conversion failed when converting date and/or time from character string.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Conversion failed when converting date and/or time from character string.

Source Error:

Line 75: da.SelectCommand.Parameters.AddWithValue("start", start);
Line 76: da.SelectCommand.Parameters.AddWithValue("end",end);
Line 77: DataTable dt = new DataTable();
Line 78: da.Fill(dt);
Line 79:

Source File: d:\Maik enviro\erp\DAL\Db.cs Line: 77

Code:-
private void LoadAppointments()
{
DataTable table = DAL.Db.LoadAppointments(DayPilotScheduler1.VisibleStart, DayPilotScheduler1.VisibleEnd);
DayPilotScheduler1.DataSource = table;
DayPilotScheduler1.DataIdField = "ID";
DayPilotScheduler1.DataTextField = "Name";
DayPilotScheduler1.DataStartField = "App_Date";
DayPilotScheduler1.DataEndField = "AppointmentEnd";
DayPilotScheduler1.DataTagFields = "AppointmentStatus";
DayPilotScheduler1.DataResourceField = "Team_id";
DayPilotScheduler1.DataBind();
DayPilotScheduler1.Update();
}

DAL.Db.LoadAppointments Is:-

public static DataTable LoadAppointments(DateTime start, DateTime end)
{
SqlDataAdapter da = new SqlDataAdapter("SELECT ( Client_Master.First_Name + Client_Master.Last_Name) AS Name, Client_Master.Company_Name, Appointments.Team_id,Appointments.ID, Appointments.App_Date, Appointments.AppointmentEnd, Appointments.AppointmentStatus, Appointments.Remark FROM Appointments INNER JOIN Client_Master ON Appointments.Client_id = Client_Master.Client_id WHERE NOT (([AppointmentEnd] <= '" + start + "') OR ([App_Date] >= '" + end + "'))", "Data Source=118.67.248.175;Initial Catalog=NaikEnviro;Persist Security Info=True;User ID=''''''''''''''''';Password=............;Connect Timeout=720000;");
da.SelectCommand.Parameters.AddWithValue("start", start);
da.SelectCommand.Parameters.AddWithValue("end",end);
DataTable dt = new DataTable();
da.Fill(dt);

return dt;

}

Comment posted by Dan Letecky [DayPilot]
8 years ago.

You can try replacing AddWithValue() method with the following code:

da.SelectCommand.Parameters.Add("start", SqlDbType.DateTime); 
da.SelectCommand.Parameters["start"] =start; 

This way you will prevent unwanted conversions to/from string.

Comment posted by Amit Tiwari
8 years ago.

Facing same error.

Comment posted by Dan Letecky [DayPilot]
8 years ago.

You are inserting the dates directly into the query string:

...(([AppointmentEnd] <= '" + start + "') OR ([App_Date] >= '" + end + "'))"

That's not how it's supposed to be done. You should switch to named parameters. Then the SQL server will handle it for you:

...(([AppointmentEnd] <= @start) OR ([App_Date] >= @end))"

The @start and @end placeholders will be replaced by the values supplied using Parameters.Add/AddWithValue:

da.SelectCommand.Parameters.AddWithValue("start", start); 
da.SelectCommand.Parameters.AddWithValue("end",end); 
Comment posted by Amit Tiwari
8 years ago.

Currently My project is running. Thanks for all your great help. I simply couldn't have done without you!!!!!!

Thank you once again.

Comment posted by Dan Letecky [DayPilot]
8 years ago.

Great, thanks for the update!

This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.