VB.net Date Format Gridview dd/mm/yyyy - asp.net

Hello
I'm trying to update a ms access database. I've got a gridview with a date that i use:
Dim tBox As TextBox = CType(gridStaff.Rows(e.RowIndex).FindControl("sDate"), TextBox)
I then want to take the value from this textbox and assign it to a date variable.
Dim test As Date = CDate(tBox.Text)
The problem I have is that now when I put test variable into my sql update query it stores the date in this format. mm/dd/yyyy instead of the dd/mm/yyyy format I want.
Ive tried different ways to format it I read online but to no success yet. Any advice would be great!
Thanks

Is your problem the conversion from tBox.Text to Date, or is it at the sql update query level? -- You should probably give the sql statement a parameter with a strict type. Converting from tBox.Text into test should probably be done in a Culture aware manner:
'String to convert and target date:
Dim dateString As String = "02/15/2011"
Dim d As Date
' Specify English/US culture when converting string:
Dim cul As Globalization.CultureInfo = Globalization.CultureInfo.GetCultureInfo("en-US")
d = Date.Parse(dateString, cul)
You could also rely on the default culture and simply let Date.Parse do the job without specifying the culture.

try using
Dim test As Date = DateTime.ParseExact(tBox.Text, "d/M/yyyy", CultureInfo.InvariantCulture)
or if your System is set up right and CultureInfo.CurrentCulture has the right date format,
Dim test As Date = DateTime.Parse(tBox.Text, CultureInfo.CurrentCulture )
instead of CDate()

Related

Retrieving a Date value from a GridView in VB

I am trying to retrieve a value from a GridView and save it as a Date variable, using the following code:
Dim invoice_date As Date = Convert.ToDateTime((TryCast(row.Cells(2).Controls(0), TextBox)).Text)
I am getting an error:
System.Web.UI.WebControls.TextBox) returned Nothing.
From what I have read, if the TryCast fails, it will return a value of nothing.
I have a similar command for an Integer, which works fine on another column.
Dim order_value As Integer = Convert.ToInt32((TryCast(row.Cells(3).Controls(0), TextBox)).Text)
Can somebody please help with the correct syntax to work with a Date value?
You are right that TryCast returns Nothing if it cannot cast to the target type. So, the problem that you have is most likely that row.Cells(2).Controls(0) is not a TextBox. To debug this, split the line:
Dim invoice_date As Date = Convert.ToDateTime((TryCast(row.Cells(2).Controls(0), TextBox)).Text)
like this:
Dim textbox As TextBox = TryCast(row.Cells(2).Controls(0), TextBox)
Dim invoice_date As Date = Convert.ToDateTime(textbox.Text)
and look at the value of textbox.
Try and use DirectCast() and see if that works.

displaying date in MM/dd/yyyy format using label

im new to asp.net,im trying to display out my date output in terms of
MM/dd/yyyy
but it came out with
MM/dd/yyyy 12:00:00 AM
what can i do to remove the time?
my vb.net code is like this:
Dim date As String = myDataReader("date").ToString()
lbldate.Text = date
You can pass the format to ToString() method.
Dim date As String = myDataReader("date").ToString("MM/dd/yyyy")
You need to pass the format to the .ToString method, like this:
.ToString("MM/dd/yyyy")
It would also be a good idea to cast the myDataReader("date") to a DateTime object, like this:
Dim dateValue As DateTime = CType(myDataReader("date"), DateTime)
Dim date As String
If dateValue Is Not Nothing Then
date = dateValue.ToString("MM/dd/yyyy")
End If
Note - If the TryCast fails, then Nothing would be the value of dateValue, hence the If dateValue Is Not Nothing Then logic is there.

String was not recognized as a valid DateTime. Existing solutions have not worked for me

I have googled this all morning but could not resolve it.
Infact, the solution I am trying below was extracted from this site:
If e.Row.RowType = DataControlRowType.DataRow Then
Dim eDate As TextBox = DirectCast(e.Row.FindControl("txtEventDate"), TextBox)
eDate.Text = Request.QueryString("evdates")
DateTime.ParseExact(eDate.Text, "MM/dd/yyyy", CultureInfo.CurrentCulture)
End If
The date value displays as 14/04/2014 00:00:00 but we would like to display as MM/dd/yyyy.
But we keep getting String was not recognized as a valid DateTime.
I even tried doing this on the markup but to no avail.
<asp:TextBox ID="txtEventDate" DataFormatString="{0:d}" runat="server"></asp:TextBox>
Any ideas how to fix this?
From your question, it looks like the date is in the format of dd/MM/yyyy. British style. Also you might want to use CultureInfo.InvariantCulture. I am not sure where your IIS server is hosted or what culture is configured on the box.
You can try this:
// read the data from the request
string temp = Request.QueryString("evdates");
// convert the string to datetime object
DateTime d = DateTime.ParseExact(temp, "MM/dd/yyyy", CultureInfo.InvariantCulture);
// format any way you like
eDate.Text = d.ToString("MM/dd/yyyy);

UTC to datetime conversion

I have the following date time string:
2013-08-16T09:21:05-04:00
How can I get that into a local datetime format in VB.NET?
UPDATE
Just realised the string had some extra quotes on each side that hadn't been sanitized properly #blush
Look at the DateTime.ParseExact() and DateTime.TryParse() methods.
http://msdn.microsoft.com/en-us/library/System.DateTime_methods.aspx
Have you tried something like this
Dim s As String = "2013-08-16T09:21:05-04:00"
Dim d As DateTime = DateTime.Parse(s)
Dim dateValue As DateTime = DateTime.Parse("2013-08-16T09:21:05-04:00")
Debug.WriteLine(dateValue.ToString)
Output on my machine (British English, GMT+1):
16/08/2013 14:21:05

Separating Date and Time values from SQL Server 2008 to display in asp.net

I have a database table that stores a datetime value.
I want to display that value on my asp.net textboxes, except I need to show date value in TextBox A and Time in TextBox B.
How can I split those values in VB.NET?
The idea is to use format string:
Date: dateTimeReadedFromDb.ToString("dd-MM-yyyy")
Time: dateTimeReadedFromDb.ToString("hh:mm:ss")
You can use the built in date formatting functions in VB:
Dim ThisDate as DateTime = Now
Dim TimePart as string = ThisDate.ToShortTimeString
dim DatePart as string = ThisDate.ToShortDateString

Resources