none
Conversion failed to convert Date/Time to string error while inserting records in form RRS feed

  • Question

  • Hi,

    Actually I am trying to develop registration form which includes DOJ field. I can only enter in mm/dd/yyyy format. But I want to enter data in dd/mm/yyyy format. When I tried to write inmywebapplication which was in  Windows Server it is working good.

    //DateTime doj = Convert.ToDateTime(txt_DOJ.Text,System.Globalization.CultureInfo.GetCultureInfo("en-US").DateTimeFormat);


               

    //DateTime doj = Convert.ToDateTime(txt_DOJ.Text).ToString("dd-MM-yyyy");


               

    DateTimedoj = DateTime.ParseExact(txt_DOJ.Text, "dd/mm/yyyy", CultureInfo.InvariantCulture);

    But when I try to execute same application in windows8 its not working properly I am getting error as conversion failed
    Please help me in doing this..

    Thanks in Advance...

    Friday, September 20, 2013 9:30 AM

Answers

  • Hi Sanam

    Date Format of SQL Server is yyyy-MM-dd

    Change insert query to doj.ToString("yyyy-MM-dd")

    Happy Coding, RDRaja


    Saturday, September 21, 2013 9:25 AM
  • SqlCommand cmd = new SqlCommand("Insert into Employee(EmployeeId,EmployeeName,DOJ,EmailID) values('" + empid + "','" + empname + "','" + doj.ToString("yyyy-MM-dd") + "','" + emailid + "')");


    Happy Coding, RDRaja

    Saturday, September 21, 2013 9:36 AM

All replies

  • You can use this:

    String.Format("{0:dd/mm/yyyy}", txt_DOJ.Text);


    João Antonio Marques

    Friday, September 20, 2013 1:35 PM
  • Hi  Sanam,

    First split date string in to 3 integers for Date,Month,year

    Then use DateTime type to combine All

    DateTime obj = new DateTime(iyear, imonth, iday).ToString("dd-MM-yyyy");


    Happy Coding, RDRaja

    Friday, September 20, 2013 3:26 PM
  • Hi Raja,

    Thanks for your reply. Please explain me in detail as I am new to .Net. Please help me

    Thanks in advance...

    Saturday, September 21, 2013 6:51 AM
  • Hi JoaoMarques,

    Thanks for your reply. I have tried your solution but I am getting same error...

    Saturday, September 21, 2013 6:52 AM
  • Saturday, September 21, 2013 6:54 AM
  • Hi Sanam

    Date Format of SQL Server is yyyy-MM-dd

    Change insert query to doj.ToString("yyyy-MM-dd")

    Happy Coding, RDRaja


    Saturday, September 21, 2013 9:25 AM
  • SqlCommand cmd = new SqlCommand("Insert into Employee(EmployeeId,EmployeeName,DOJ,EmailID) values('" + empid + "','" + empname + "','" + doj.ToString("yyyy-MM-dd") + "','" + emailid + "')");


    Happy Coding, RDRaja

    Saturday, September 21, 2013 9:36 AM