-
Mar 21st, 2019, 12:08 PM
#1
Thread Starter
New Member
Date Unwanted Convert !
Hi
First of all I should Thanks for your time .
When I change my Windows region To Persian , When i Want to add Date Value to my data table with Date Type , The Program See my Value As Hejri Calendar and convert it to gregorian calendar .
if i want to explain my problem :
Code:
Dim DataTable as new DataTable
DataTable.Columns.Add("DT", System.Type.GetType("System.DateTime"))
DataTable.Rows.Add("2019-08-27")
And See This Value in my table :
#11/18/2640 #
i don't know which section Change my Date Value . I be thankful if you solve my problem.
-
Mar 21st, 2019, 12:18 PM
#2
Re: Date Unwanted Convert !
And See This Value in my table
How are you seeing this value?
-
Mar 21st, 2019, 12:21 PM
#3
Thread Starter
New Member
Re: Date Unwanted Convert !
with debug mode and stop program and see value in watch
-
Mar 21st, 2019, 12:29 PM
#4
Thread Starter
New Member
Re: Date Unwanted Convert !
See This code please :
Code:
DataTable.Columns.Add("DT", System.Type.GetType("System.DateTime"))
DataTable.Rows.Add("2019-08-27")
Dim DR() As DataRow
DR = DataTable.Select("DT = #" + "2019-08-27" + "#")
the Dr.count = 0 ! ! !
-
Mar 21st, 2019, 12:30 PM
#5
Re: Date Unwanted Convert !
Welcome to VBForums
The Watch window (and the tools for most databases, as well as most other software) displays values using your regional settings, and you generally cannot do anything about that. In the Watch window you can alter the display by changing the value to use ToString, eg: MyDateValue.ToString("yyyy-MM-dd")
The important thing is that the value is correct, because that is all that gets saved.
The format is purely a display issue, so as long as your program displays/reads values correctly, everything is fine.
-
Mar 21st, 2019, 12:33 PM
#6
Thread Starter
New Member
Re: Date Unwanted Convert !
Thank you for reply ,but i think the value is wrong in data table because i add above code (post 4 and post 5) and see wrong result.
-
Mar 21st, 2019, 12:35 PM
#7
Re: Date Unwanted Convert !
I was just going to say that you cant change the format of the date in the datatable because there is not such thing. This is a display instance derived from region.
In your statement you may instead want to try parameters using culture settings.
-
Mar 21st, 2019, 12:43 PM
#8
Thread Starter
New Member
Re: Date Unwanted Convert !
can you explain the culture setting , what's your idea .thank's
-
Mar 21st, 2019, 12:49 PM
#9
Re: Date Unwanted Convert !
This is really uncharted waters for me, but try something like this (untested)
Code:
Dim MyDate As DateTime = Nothing
Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
Using Cmd As New SqlCommand("Select * from tbl where datecol=@Mydate")
Cmd.Parameters.AddWithValue("@Mydate", MyDate)
End Using
This could be way off, or there could be a totally easier way... /shrug
-
Mar 21st, 2019, 12:55 PM
#10
Re: Date Unwanted Convert !
Ops, you wanted to return datarows
My eyes are getting bad of late. I think I need vac.
So without the command/params
Code:
Dim MyDate As DateTime = Nothing
Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
DR = DataTable.Select("DT = #" & MyDate & "#")
-
Mar 21st, 2019, 04:21 PM
#11
Thread Starter
New Member
Re: Date Unwanted Convert !
thank you very much , i use your method when i want to Write data and for search , it work very well for me .thanks
-
Mar 21st, 2019, 05:16 PM
#12
Thread Starter
New Member
Re: Date Unwanted Convert !
After your hint ,my test code convert to this
Code:
DataTable.Columns.Add("DT", System.Type.GetType("System.DateTime"))
Dim MyDate As DateTime = Nothing
Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
DataTable.Rows.Add(MyDate)
Dim DR() As DataRow
Dim Sr As String = MyDate.ToShortDateString
DR = DataTable.Select("DT = #" & Sr & "#")
'SR = "05/06/1398"
But as you see problem convert to something else , this time the program see Date as gregorian and convert it to persian ! ! !
I realy don't know what happening. . .
-
Mar 21st, 2019, 05:17 PM
#13
Thread Starter
New Member
Re: Date Unwanted Convert !
After your hint ,my test code convert to this
Code:
DataTable.Columns.Add("DT", System.Type.GetType("System.DateTime"))
Dim MyDate As DateTime = Nothing
Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
DataTable.Rows.Add(MyDate)
Dim DR() As DataRow
Dim Sr As String = MyDate.ToShortDateString
DR = DataTable.Select("DT = #" & Sr & "#")
'SR = "05/06/1398"
But as you see problem convert to something else , this time the program see Date as gregorian and convert it to persian ! ! !
I realy don't know what happening. . .
-
Mar 21st, 2019, 05:59 PM
#14
Re: Date Unwanted Convert !
Originally Posted by Hamid.Khp
Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
That line tries to store the value interpreted using US format (so that value will be in a few months time).
However, it is important to note that it tries to convert it. If for any reason it fails, there will not be an error - it will simply return False (and the variable value wont be set), but that currently isn't being checked.
This is how you could check it:
Code:
If Date.TryParseExact("2019-08-27", ..., MyDate) Then
'your other code
Else
'show a message to say it didn't convert
End If
Originally Posted by Hamid.Khp
Dim Sr As String = MyDate.ToShortDateString
That creates a string value which is formatted according to Regional Settings... which will be a different format to the one you started with.
-
Mar 22nd, 2019, 07:29 AM
#15
Thread Starter
New Member
Re: Date Unwanted Convert !
NO It Return True(After Check it) ,I really don't know what happening , first It don't Convert my date but after that my date convert automatically ,perhaps i should change my question like this :
What can i do if I want everybody with any region of windows don't see error in my program ?
thank's again.
-
Mar 22nd, 2019, 07:47 AM
#16
Thread Starter
New Member
Re: Date Unwanted Convert !
NO It Return True(After Check it) ,I really don't know what happening , first It don't Convert my date but after that my date convert automatically ,perhaps i should change my question like this :
What can i do if I want everybody with any region of windows don't see error in my program ?
thank's again.
-
Mar 22nd, 2019, 07:49 AM
#17
Thread Starter
New Member
Re: Date Unwanted Convert !
finaly it work
thanks everyone ,you give me idea to solve it ,for another persones :
Code:
DataTable.Columns.Add("DT", System.Type.GetType("System.DateTime"))
Dim MyDate As DateTime = Nothing
Date.TryParseExact("2019-08-27", "yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"), Globalization.DateTimeStyles.None, MyDate)
DataTable.Rows.Add(MyDate)
Dim DR() As DataRow
Dim Sr As String = MyDate.ToString("yyyy-MM-dd", Globalization.CultureInfo.CreateSpecificCulture("en-US"))
DR = DataTable.Select("DT = #" & Sr & "#")
Last edited by Hamid.Khp; Mar 22nd, 2019 at 08:10 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|