Hi Pal,

I have issues with inserting date into my database. Though i declare the datatype as datetime in my SQL server database I will sincerely appreciate someones assistance in helping me fix the error. the error message is " Conversion failed when converting datetime from character string "

here is my code line:

Dim sqlcommand As New SqlCommand
        With sqlcommand
            .CommandText = "INSERT Into Secure_ProManagement Values('" & Me.CNameBox.Text & "','" & ClientShrtCode.Text & "','" & RMName.Text & "','" & RMContact.Text & "'," & RMNumber.Text & ",'" & StepsTxtBox.Text & "','" & EscalationTxtBox.Text & "','" & ProName.Text & "',' @d  ',' @s  ','" & Descriptn.Text & "')"
            sqlcommand.Parameters.Add("@d", SqlDbType.DateTime).Value = CDate(EndDate.Text)
            sqlcommand.Parameters.Add("@s", SqlDbType.DateTime).Value = CDate(StartDate.Text)
            .Connection = connection
            .ExecuteNonQuery()
        End With

I created a table and added a column as a DateTime.
I left the date to be inserted as a DateTime and made no conversions on it and it inserted.

Imports System
Imports DB_SqlLister ' keeps connection info
Imports System.Data.SqlClient

Module Module1

   Sub Main()
      Dim dateEnd As DateTime = DateTime.Now
      Dim strSQL As String = "insert into XDB.XOWN.HAS_DATE (MY_DATE, COMMENT) values (@d, @s)"
      Dim conn As New SqlConnection(CDB_SqlLister.csb.ToString())
      conn.Open()
      Dim cmd As New SqlCommand(strSQL, conn)
      cmd.Parameters.Add("@d", SqlDbType.DateTime).Value = dateEnd
      cmd.Parameters.Add("@s", SqlDbType.VarChar).Value = "DW_390905"
      cmd.ExecuteNonQuery()
      conn.Close()
   End Sub

End Module

So, I would suggest converting your start and end dates to DateTime objects and everything will work (under the same conditions).

I am not getting your reply
can you kindly simplify it working with 2 different date --- Start Date and End Date,

Once more, i had already declare my StartDate and EndDate column datatype as datetime in my database.

Thanks in advance.

I am not getting your reply
can you kindly simplify it working with 2 different date --- Start Date and End Date,

Once more, i had already declare my StartDate and EndDate column datatype as datetime in my database.

Thanks in advance.

Guys I have successfully get the code working

here is the correction code for other developers

Dim sqlcommand As New SqlCommand
With sqlcommand
.CommandText = "INSERT Into Secure_ProManagement Values('" & Me.CNameBox.Text & "','" & ClientShrtCode.Text & "','" & RMName.Text & "','" & RMContact.Text & "'," & RMNumber.Text & ",'" & StepsTxtBox.Text & "','" & EscalationTxtBox.Text & "','" & ProName.Text & "','" & EndDate.Text &"','"& StartDate.Text & "','" & Descriptn.Text & "')"
sqlcommand.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = EndDate.Text.ToString
sqlcommand.Parameters.Add("StartDate", SqlDbType.DateTime).Value = StartDate.Text.ToString
.Connection = connection
.ExecuteNonQuery()
End With

ellg_date = Date.Parse(TextBox24.Text)

msgbox(ellg_date)

Just one thing to add here: Why you dont use a FULL parametreized query?
This means that you add parameters for all the values, like you did for StartDate and EndDate.

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.