converting datetime picker value in vb.net

Hi,
I'm trying to conver datetime picker value dd.mm.yyyy into yyyy.mm.dd and pass it as param value in my vb.net code to execute delete command.
Unfortunatelly it does not work this way. Can someone help me...
Thanks
Dim cmd As New System.Data.SqlClient.SqlCommand()
            cmd.Connection = cn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "DELETE FROM BAL_SHEET WHERE DATE_VAL = @date_val"
            cmd.Parameters.Add("@date_val", SqlDbType.NVarChar)
            cmd.Parameters("@date_val").Value = Format(Me.DateTimePicker2.Text.ToString,"YYYY.MM.DD"))
            cmd.ExecuteNonQuery()

Open in new window

LVL 1
alsamAsked:
Who is Participating?
 
ZhaolaiConnect With a Mentor Commented:
It is:

cmd.Parameters("@date_val").Value = Me.DateTimePicker2.Value.ToString("yyyy.MM.dd")

0
 
Meir RivkinFull stack Software EngineerCommented:
cmd.Parameters("@date_val").Value = Format(Me.DateTimePicker2.Text.ToString,"YYYY/MM/DD")
0
 
Meir RivkinFull stack Software EngineerCommented:
my mistake, better use the following:
Dim cmd As New System.Data.SqlClient.SqlCommand()
            cmd.Connection = cn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "DELETE FROM BAL_SHEET WHERE DATE_VAL = @date_val"
            cmd.Parameters.Add("@date_val", SqlDbType.NVarChar)
            cmd.Parameters("@date_val").Value = Format(Me.DateTimePicker2.Text.ToString,"YYYY-MM-DD"))
            cmd.ExecuteNonQuery()

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Meir RivkinFull stack Software EngineerCommented:
wrong again, that will do it:cmd.Parameters("@date_val").Value = Format(Me.DateTimePicker2.Text.ToString,"yyyy/mm/dd"))
0
 
alsamAuthor Commented:
It returns

Syntax error converting datetime from character string.

0
 
ZhaolaiCommented:
Have you tried my solution?
0
 
Meir RivkinFull stack Software EngineerCommented:
is this doesn't work???

cmd.Parameters("@date_val").Value = Format(Me.DateTimePicker2.Text.ToString("yyyy/mm/dd"))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.