Date time issues on local server and deployment server in C# .Net


Here sometimes we face issue in date conversion from local to deployment server.  I had faced earlier in my projects where my calendar (Date picker) was working fine on local server but once we deployed it on server it started giving error of date conversion.
The main reason of the date conversion is Culture, so it is not aware of which format you are passing the date in (i.e. dd/MM/yyyy) but on server it might change and it understand like (MM/dd/yyyy) . so to avoid these kind of error please user Culture Info provided by Microsoft .net.
here how it works, first we captured the date picker date, month and year in below format:
string strDateFormat = ddlMonth.SelectedValue + "/" + ddlDate.SelectedValue + "/" + ddlYear.SelectedValue;

SqlParameter[] pArray = new SqlParameter[2];

        pArray[0] = new SqlParameter("@Id", SqlDbType.Int);
        pArray[0].Value = Convert.ToInt32(EmpID);
 pArray[1] = new SqlParameter("@DateFormat", SqlDbType.Date);
  DateTime dtDOB = DateTime.ParseExact(strDateFormat, "M/d/yyyy",
  CultureInfo.InvariantCulture);
        pArray[1].Value = dtDOB;
      
try
{
        objDAL.ExecuteNonQuery("[usp_AddDateFormat]", pArray)

  lblMessage.Text= "Record added successfully!";

}
Catch(Exception ex)
{
lblMessage.Text= "Error while adding record!";

}

Comments