when saving the "conversion failed when converting datetime from character string" is displayed. Please help

Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
Try
Dim cn As New SqlConnection(ConnectionString)
cn.Open()
put = "insert into NewAccount(Account_no,Surname,First_Name,sex,Date_Applied,Date_Birth,Contact_no,Contact_Address,Acco unt_type,Branch_ID)values('" & Me.TxtAddress.Text & "','" & Me.txtAccount_No.Text & "','" & Me.txtSurname.Text & "','" & Me.txtContact_Address.Text & "','" & Me.cboAccount_type.Text & "','" & Me.cboBranch_id.Text & "','" & Me.dtDate_Applied.Textmaskformat & "','" & Me.dtdate_birth.Textmaskformat & "','" & Me.txtFirst_Name.Text & "','" & Me.cbosex.Text & "')"
da = New SqlDataAdapter(put, ConnectionString)
da.Fill(ds, "NewAccount")
Catch ex As Exception
MessageBox.Show(ex.Message)

End Try
End Sub

Recommended Answers

All 4 Replies

In database the datatype is Datetime and ur passing String from Front end so its causing the error. Either make the datatype as varchar in DB or pass the date from front end

in the propertis of textbox mask is date format:-'00/00/0000' When i Changed to dateTimePicker, I had the same error. am uing Maskedtextbox for date field for the front end.
What is the actual cause of the error.
Please advice

try using a string variable and then pass your date and time value to it. then use another date data type variable, give it your string value(date/time). then use your date datatype variable for inserting your data into the database.

it may be ambigous but it worked for me. :)

Dim x As String = "15/Feb/2011"
   x = Convert.ToDateTime(x)

try something like this...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.