Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. DateFormat Problem in ASP.NET

DateFormat Problem in ASP.NET

Scheduled Pinned Locked Moved ASP.NET
csharphelpasp-netdatabasesql-server
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sharad Pyakurel
    wrote on last edited by
    #1

    I have needed to insert and update DateTime Field in MSSQL Database Table. I am using Text Querry. My input Control is TextBox where the format of Date is "dd/MM/yyyy". In Database it should be save as "yyyy-MM-dd". I have used a Function to convert Date Format as: public static string ConvertToDate(string pstr) { string result=string.empty; try { result = "'" + Convert.ToDateTime(str).ToString("yyyy/MM/dd") + "'"; } catch { result = System.Data.SqlTypes.SqlDateTime.Null.ToString(); } } The string Data Returned after conversion is used for insertion. I I execute Application Visual studio by compiling it works well. but when i make virtual Directory of this application and run this through browser it doesn't work. How can i solve this issue.

    S W K G 4 Replies Last reply
    0
    • S sharad Pyakurel

      I have needed to insert and update DateTime Field in MSSQL Database Table. I am using Text Querry. My input Control is TextBox where the format of Date is "dd/MM/yyyy". In Database it should be save as "yyyy-MM-dd". I have used a Function to convert Date Format as: public static string ConvertToDate(string pstr) { string result=string.empty; try { result = "'" + Convert.ToDateTime(str).ToString("yyyy/MM/dd") + "'"; } catch { result = System.Data.SqlTypes.SqlDateTime.Null.ToString(); } } The string Data Returned after conversion is used for insertion. I I execute Application Visual studio by compiling it works well. but when i make virtual Directory of this application and run this through browser it doesn't work. How can i solve this issue.

      S Offline
      S Offline
      saini arun
      wrote on last edited by
      #2

      As far as I know there should not be any case for not working the same code from virtual directory if it works from visual studio. Are you sure you set the correct path while configuring the virtual directory?

      1 Reply Last reply
      0
      • S sharad Pyakurel

        I have needed to insert and update DateTime Field in MSSQL Database Table. I am using Text Querry. My input Control is TextBox where the format of Date is "dd/MM/yyyy". In Database it should be save as "yyyy-MM-dd". I have used a Function to convert Date Format as: public static string ConvertToDate(string pstr) { string result=string.empty; try { result = "'" + Convert.ToDateTime(str).ToString("yyyy/MM/dd") + "'"; } catch { result = System.Data.SqlTypes.SqlDateTime.Null.ToString(); } } The string Data Returned after conversion is used for insertion. I I execute Application Visual studio by compiling it works well. but when i make virtual Directory of this application and run this through browser it doesn't work. How can i solve this issue.

        W Offline
        W Offline
        walterhevedeich
        wrote on last edited by
        #3

        Perhaps providing us some error messages from your application can help us to figure out what the problem is.

        S 1 Reply Last reply
        0
        • W walterhevedeich

          Perhaps providing us some error messages from your application can help us to figure out what the problem is.

          S Offline
          S Offline
          sharad Pyakurel
          wrote on last edited by
          #4

          Error when using virtual Directory is: Exception Details: System.FormatException: String was not recognized as a valid DateTime. Source Error: Line 298: if (str.ToString().Trim().Length > 0) Line 299: { Line 300: result = "'" + Convert.ToDateTime(str).ToString("MM/dd/yyyy") + "'"; Line 301: } Line 302: else But it is ok from Visual Studio.

          W 1 Reply Last reply
          0
          • S sharad Pyakurel

            Error when using virtual Directory is: Exception Details: System.FormatException: String was not recognized as a valid DateTime. Source Error: Line 298: if (str.ToString().Trim().Length > 0) Line 299: { Line 300: result = "'" + Convert.ToDateTime(str).ToString("MM/dd/yyyy") + "'"; Line 301: } Line 302: else But it is ok from Visual Studio.

            W Offline
            W Offline
            walterhevedeich
            wrote on last edited by
            #5

            It will never be catched during compile time since its a runtime error. Try using DateTime.TryParse method. http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx[^]

            1 Reply Last reply
            0
            • S sharad Pyakurel

              I have needed to insert and update DateTime Field in MSSQL Database Table. I am using Text Querry. My input Control is TextBox where the format of Date is "dd/MM/yyyy". In Database it should be save as "yyyy-MM-dd". I have used a Function to convert Date Format as: public static string ConvertToDate(string pstr) { string result=string.empty; try { result = "'" + Convert.ToDateTime(str).ToString("yyyy/MM/dd") + "'"; } catch { result = System.Data.SqlTypes.SqlDateTime.Null.ToString(); } } The string Data Returned after conversion is used for insertion. I I execute Application Visual studio by compiling it works well. but when i make virtual Directory of this application and run this through browser it doesn't work. How can i solve this issue.

              K Offline
              K Offline
              kupps
              wrote on last edited by
              #6

              Are you sure the current culture is the same between IIS and Visual Studio...? Try outputting System.Globalization.CultureInfo.CurrentCulture.ToString() on both sites and see if there are any differences. If this is different, then Convert.ToDateTime(str) will complain.

              modified on Tuesday, June 8, 2010 4:16 AM

              S 1 Reply Last reply
              0
              • K kupps

                Are you sure the current culture is the same between IIS and Visual Studio...? Try outputting System.Globalization.CultureInfo.CurrentCulture.ToString() on both sites and see if there are any differences. If this is different, then Convert.ToDateTime(str) will complain.

                modified on Tuesday, June 8, 2010 4:16 AM

                S Offline
                S Offline
                sharad Pyakurel
                wrote on last edited by
                #7

                This problem is solved after changing Globalization setting in Web.Config File. Previously i had used en_US culture, Now it is changed to en_GB then problem is solved. Thank you all for yours remarkable helps.

                1 Reply Last reply
                0
                • S sharad Pyakurel

                  I have needed to insert and update DateTime Field in MSSQL Database Table. I am using Text Querry. My input Control is TextBox where the format of Date is "dd/MM/yyyy". In Database it should be save as "yyyy-MM-dd". I have used a Function to convert Date Format as: public static string ConvertToDate(string pstr) { string result=string.empty; try { result = "'" + Convert.ToDateTime(str).ToString("yyyy/MM/dd") + "'"; } catch { result = System.Data.SqlTypes.SqlDateTime.Null.ToString(); } } The string Data Returned after conversion is used for insertion. I I execute Application Visual studio by compiling it works well. but when i make virtual Directory of this application and run this through browser it doesn't work. How can i solve this issue.

                  G Offline
                  G Offline
                  Goutam Patra
                  wrote on last edited by
                  #8

                  This might be a culture problem Try Converting using those 2 options result = Left(Convert.ToString(CDate(pstr), New CultureInfo("ja-JP")), 10) or if your date in Dd/MM/YYYY format then try using very basic method like result = Strings.Right(pstr, 4) & "-" & Strings.Right(Strings.Left(pstr, 5), 2) & "-" & Strings.Left(pstr, 2)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups