Click here to Skip to main content
15,916,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am working on web application i am facing an issue on web form when click on link button its prompt an error "string was not recognized as a valid date time when converting to date time" although that on local machine working fine but after deployment on server its shows above error.

What I have tried:

protected void lnkbtnAddExisting_Click(object sender, EventArgs e)
           DevExpress.Web.GridViewDataItemTemplateContainer gvRow = ((DevExpress.Web.GridViewDataItemTemplateContainer)((LinkButton)sender).NamingContainer);
           string SchemeNameID = DataBinder.Eval(gvRow.DataItem, "SchemeNameID").ToString();
           DateTime StartDate =Convert.ToDateTime( DataBinder.Eval(gvRow.DataItem, "StartDate"));
           DateTime EndDate =Convert.ToDateTime( DataBinder.Eval(gvRow.DataItem, "EndDate"));

           Response.Redirect("abc.aspx?StartDate=" + StartDate + "&EndDate=" + EndDate + "&SchemeNameID=" + SchemeNameID);
Updated 10-Sep-16 0:33am

So check your DB: the error message is pretty explicit.
string was not recognized as a valid date time when converting to date time

Means that the value that came from your database is not a valid date time in the current locale for the production machine.

The solution for this is to change your DB so that you store values in appropriate data types: Dates in DATE or DATETIME, integers in INT, and so on. When you store dates as strings, this is just the beginning of the problems it will give you.
Share this answer
Noman Suleman 10-Sep-16 6:15am    
sir i have checked in DB although all are set as you suggest, but still have same issue..
Create temp variables to see what you want to convert with the debugger:
string Temp = DataBinder.Eval(gvRow.DataItem, "EndDate");

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Are you sure you need to convert to DateTime in C# ?
Because you have a string converted to DateTime to build a string.
Share this answer
Noman Suleman 10-Sep-16 6:46am    
its is working fine on my local debugger also shows date as format "dd/mm/yyyy"ss:mm on server it shows error

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