Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code work fine when i run on localhost, but when i upload to server it give error..
String was not recognized as a valid DateTime.


What I have tried:

{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

    string userid = GVmydsr.DataKeys[e.RowIndex].Value.ToString();
    GridViewRow row = (GridViewRow)GVmydsr.Rows[e.RowIndex];
    Label date = (Label)row.FindControl("lblcurrentdate");

    string cdate = date.Text.ToString();
    DateTime date1 = DateTime.ParseExact(cdate, "MM/dd/yyyy 00:00:00", null);
    string dt1 = date1.ToString("MM/dd/yyyy 00:00:00");


    GVmydsr.EditIndex = -1;


    string dt2 = DateTime.Today.ToString("MM/dd/yyyy 00:00:00");

    if (dt1 == dt2)
    {
        //
    }

    else
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "Success", "alert('Cannot Edit !!!.');", true);
        gvbind();
    }
}
Posted
Updated 3-Jan-18 23:32pm
Comments
ADI@345 4-Jan-18 5:31am    
I am getting date in this format offline - 01-04-2018 00:00:00
but after uploading it, get convert into - 01/04/2018 00:00:00
Richard MacCutchan 4-Jan-18 5:39am    
Why are you using strings to compare dates? Use DateTime types to compare accurately.

1 solution

use DateTime.TryParseExact Method (System)[^]

Label date = (Label)row.FindControl("lblcurrentdate");
           string cdate = date.Text;
           DateTime date1 = DateTime.MinValue;
           DateTime.TryParseExact(cdate.Split(' ')[0], "MM-dd-yyyy", null, System.Globalization.DateTimeStyles.None, out date1);
           string dateDB = date1.ToString("MM-dd-yyyy");
           string day = DateTime.Today.ToString("MM-dd-yyyy");
           if (dateDB == day)
           {
               ////
           }


Quote:
I am getting date in this format offline - 01-04-2018 00:00:00
but after uploading it, get convert into - 01/04/2018 00:00:00

if your string is like "01-04-2018" then use "MM-dd-yyyy"
if "01/04/2018" then use "MM/dd/yyyy"
this will handle both the case
DateTime.TryParseExact(cdate.Split(' ')[0], new string[] { "MM-dd-yyyy", "MM/dd/yyyy" }, null, System.Globalization.DateTimeStyles.None, out date1);
 
Share this answer
 
v3
Comments
ADI@345 4-Jan-18 5:41am    
i apply all this , but it work fine on localhost , but not on server
ADI@345 4-Jan-18 5:45am    
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

string userid = GVmydsr.DataKeys[e.RowIndex].Value.ToString();
GridViewRow row = (GridViewRow)GVmydsr.Rows[e.RowIndex];
Label date = (Label)row.FindControl("lblcurrentdate");

string cdate = date.Text.ToString();
DateTime date1 = DateTime.MinValue;
DateTime.TryParseExact(cdate.Split(' ')[0], new string[] { "MM-dd-yyyy", "d/M/yyyy" }, null, System.Globalization.DateTimeStyles.None, out date1);

string dt1 = date1.ToString("d/M/yyyy 00:00:00");

GVmydsr.EditIndex = -1;


string dt2 = DateTime.Today.ToString("d/M/yyyy 00:00:00");

if (dt1 == dt2)
{

ScriptManager.RegisterStartupScript(this, GetType(), "Success", "alert('Update Successfully!!!.');", true);
gvbind();
}

else
{
ScriptManager.RegisterStartupScript(this, GetType(), "Success", "alert('Cannot Edit !!!.');", true);
gvbind();
}
}
ADI@345 4-Jan-18 5:45am    
i made changes , this code works for me online..
ADI@345 4-Jan-18 5:47am    
Thanks for help
Karthik_Mahalingam 4-Jan-18 6:10am    
welcome

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