Link to home
Start Free TrialLog in
Avatar of Edward Harford
Edward Harford

asked on

Date conversion problem in VB

I get 'Conversion from string "Date" to type 'Date' is not valid. ' when I upload to site to web server and run it. However when running in Visual Studio it works fine - code below. iR is result of running Stored Proc

   Me.iDate.Text = iR(FormatDateTime("Date", "dd/MM/yyyy")).ToString

Open in new window

Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Are you running it against the exact same dataset?

If not:
My guess is there is data in one dataset that doesn't match the dd/MM/yyyy format.

If you are, then I have nothing.
Avatar of Edward Harford


It is the same dataset with the datatype set as a date. But I am now getting the same error in VS. The value of iR("Date") is coming through as '#3/10/2022 12:00:00 AM#'. How should I convert to the locale to get '10/03/2022'?
Now that I look at the error in more detail, it seems to be treating "Date" as a string not the field in the dataset.

No idea what iR is but is it returning a date or a string?  Do you need it?

Maybe try:
Me.iDate.Text = FormatDateTime("Date", "dd/MM/yyyy")
The call is FormatDateTime(dateValue, formatString).
Thus, your string literal "Date" as first parameter is incorrect.

 Me.iDate.Text = iR(FormatDateTime("Date", "dd/MM/yyyy")).ToString

Date in quotes is a string with the value Date 

Imports System
Imports System.Globalization
Imports System.Threading
Module Module1

    Sub Main()

        Dim dt As DateTime = DateTime.Now
        ' Sets the CurrentCulture property to U.S. English.
        Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
        ' Displays dt, formatted using the ShortDatePattern
        ' and the CurrentThread.CurrentCulture.

        ' Creates a CultureInfo for German in Germany.
        Dim ci As New CultureInfo("de-DE")
        ' Displays dt, formatted using the ShortDatePattern
        ' and the CultureInfo.
        Console.WriteLine(dt.ToString("d", ci))
    End Sub

End Module

Open in new window

Avatar of Edward Harford
Edward Harford

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial