Format date (dd/MM/yyyy)

Asked

Viewed 2,831 times

1

I have here a column in a Datagridview with the name "DBO" (date of Birth) which has the format month/day/year, I wanted to change the style of the date to dd-MM-yyyy but I’m not getting it.

    myCommand = "UPDATE DoctorBasic SET " & _
    "DOB = '" & Convert_Null(DGV1(9, 0).Value.ToString("dd/MM/yyyy"), #1/1/1900#)** & "' " & _
    "WHERE DoctorId = " & DGV1(0, 0).Value

    cmd = New SqlCommand(myCommand, myConnectionString)
    MsgBox(myCommand)
    cmd.Connection.Open()
    cmd.ExecuteNonQuery()
    cmd.Connection.Close()

convert_null is used to display this date 1/1/1900 as default if the entry you are looking for has no date and you want to save some changes to avoid error because the field is null.

  • as this the grid?

  • And what’s the problem?

  • It seems to me that you are trying to update the date in the database so that it is shown with the desired format on the screen. If this is the case, this is not how it is done. You should format).

  • Yes from what I understood, that’s what I want. I want you to present yourself in this format so that I can update the date without problems. How can I do it then?

2 answers

2

Make the change in SQL, since you are using SQL manual for the update:

myCommand = "UPDATE DoctorBasic SET " & _
"DOB = convert(datetime, '" & Convert_Null(DGV1(9, 0).Value.ToString("dd/MM/yyyy"), #1/1/1900#)** & "', 103) " & _
"WHERE DoctorId = " & DGV1(0, 0).Value

The specification of CONVERT() with 103 format can be read here.

  • Thanks, I already changed the line but give me this error: The conversion from string "dd/MM/yyyy" to type 'Integer' is not valid.

  • Integer? Are you sure that your functions are working with the type datetime?

  • I didn’t quite understand the question, want to refer to the type of field?

  • Yes. Apparently there is a conversion error when mounting the SQL string.

  • yes that I’m sure. also did not realize what was the "integer" here in the sql string

  • I think I got it, I changed the line to this (...) "DOB = Convert(datetime, '" & (DGV1(9, 0).Value.Tostring) & "',103), " & _ and I think it is now working correctly

Show 1 more comment

0

Algumas dicas :

To play date inside the datagrid, it can stay with text, you can notice when ordering, in relation to day/month/year, to stay as date, be aware in the null issue, that there must be a treatment, only that often there must be null, complicate, because comes more treatment, if leave with text, is easier treatment, think about it.

In the matter of format, you can use the string.format.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.