Your question unfortunately raises even more questions than it allows valid solutions.
First of all, as in my comment, what is the point changing the format of your date times everytime a cell haas been clicked?
Second, as we can see the
Value
property of the
Cell
object has type
string
. Depending on the datatype of the
fldDateB
and
fldDateR
columns in your database, there are two solutions:
-
the columns in the database have a datetime type: you have to get a valid
DateTime
value out of the
string
stored in the datagridview's column (see below).
-
the columns in the database have a (n)varchar type: you can store the value as is in the database. But then, you have another problem: you are not using the proper datatype to store your data. As one of my teachers liked to say to us while reviewing our newbies' codes, if you are not using the proper datatype for the data you want to work on, you deserve your problems.
So, there are several ways to get a valid datetime value out of a string; all best of them imply the use of
DateTime.Parse
[
^],
DateTime.TryParse
[
^] and
DateTime.TryParseExact
[
^] methods and their overloads.
For example, using
TryParseExact(string, string, IFormatProvider)
method applied to your case:
Dim dtb As DateTime = DateTime.TryParseExact(
dgLogs.Rows(e.RowIndex).Cells(1).Value,
"MM/dd/yyyy hh:mm",
CultureInfo.CurrentCulture)
Dim dtr As DateTime = DateTime.TryParseExact(
dgLogs.Rows(e.RowIndex).Cells(2).Value,
"MM/dd/yyyy hh:mm",
CultureInfo.CurrentCulture)
When you do that, you get valid datetime values in
dtb
and
dtr
variables. Values that you can then feed into your database.
Which brings us to the last point, by far the most important of all: never, in any situation, use concatenated strings to build SQL queries. Never. Just, do not do that. This leaves your code open to SQL injection attacks, which is a subject that is addressed several times a day on this forum.
Griff's solution is talking about that, too. Please follow this advise, it is a crucial one.