none
exception "Failed to convert parameter value from a String to a DateTime."

    Question

  • hi frnds ........this is my scenario......please help me in this exception.......

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim fs As New FileStream(path, FileMode.Open, FileAccess.Read)

    Dim br As New BinaryReader(fs)

    Dim bytes() As Byte

    bytes = br.ReadBytes(fs.Length)

    cmd.Connection = con

    con.Open()

    cmd.CommandText = "insert into licence values('" & mcode.Text & "','" & vcode.Text & "','" & ltype.Text & "','" & lno.Text & "','" & pno.Text & "',@procdate,'" & lname.Text & "','" & fname.Text & "','" & address.Text & "','" & varea.Text & "','" & PS.Text & "','" & reqweapon.Text & "','" & w1.Text & "','" & ammone1.Text & "','" & ammyear1.Text & "',@proddate1,'" & w2.Text & "','" & ammone2.Text & "','" & ammyear2.Text & "',@proddate2,'" & w3.Text & "','" & ammone3.Text & "','" & ammyear3.Text & "',@proddate3,@vdate,'" & PURPOSE.Text & "','" & remarks1.Text & "','" & remarks2.Text & "',@photo)"

    Dim p1, p2, p3, p4, p5, p6

    p1 = cmd.Parameters.Add("@procdate", OleDbType.Date)

    p2 = cmd.Parameters.Add("@proddate1", OleDbType.Date)

    p3 = cmd.Parameters.Add("@proddate2", OleDbType.Date)

    p4 = cmd.Parameters.Add("@proddate3", OleDbType.Date)

    p5 = cmd.Parameters.Add("@vdate", OleDbType.Date)

    p1.value = procdate.Text

    p2.value = proddate1.Text

    p3.value = Proddate2.Text

    p4.value = Proddate3.Text

    p5.value = vdate.Text

    p6 = cmd.Parameters.Add("@photo", OleDbType.LongVarBinary)

    p6.Value = bytes

    cmd.ExecuteNonQuery()

    br.Close()

    fs.Close()

    con.Close()

    End Sub

     

    i am getting exception as "Failed to convert parameter value from a String to a DateTime."

     

    thank u

    in advance

    Saturday, February 16, 2008 7:30 AM

Answers

  • Well, that would make sense wouldn't it? An empty string is not a date. You need to validate data entered by your users before using that data. If your business logic states that an empty string in a date field is valid, then you need to modify your query accordingly.
    Tuesday, February 19, 2008 3:49 PM

All replies

  • In the Add command you told the command object that the parameters were DateTime object, but then went and tried to set their values to string objects. You either have to set the parameter type to string or convert procdateX.Text to a DateTime (DateTime.TryParse).

    Sunday, February 17, 2008 7:27 PM
  • hi....

    thank u very much for ur reply.....

     

    but can u please help me.......where should i apply it in my code.......

     

    please reply me soon.......

     

    thank u....

    in advance,..........

    Monday, February 18, 2008 9:32 AM
  • Code Snippet

    p1.value = DateTime.Parse(procdate.Text)

    p2.value = DateTime.Parse(proddate1.Text)

    p3.value = DateTime.Parse(Proddate2.Text)

    p4.value = DateTime.Parse(Proddate3.Text)

    p5.value = DateTime.Parse(vdate.Text)

     

     

     

    Monday, February 18, 2008 9:30 PM
  • hi ......thanks for replying me.....

    my date format are all maskedtextboxes......with mask-shortdate........

     

    when i enter the date in my maskedtextboxes my code is working.......

    but anyone of my maskedtextbox is empty......then i am getting the exception........as that

     

    "Failed to convert parameter value from a String to a DateTime."

     

    please help me in this query.......

     

    thank u

    in advance....

    Tuesday, February 19, 2008 5:12 AM
  • Well, that would make sense wouldn't it? An empty string is not a date. You need to validate data entered by your users before using that data. If your business logic states that an empty string in a date field is valid, then you need to modify your query accordingly.
    Tuesday, February 19, 2008 3:49 PM
  • hi........

    thank u.......my prob has been solved.......thank u very much for sending  replies.......

    Wednesday, February 20, 2008 5:30 AM