2 Replies - 11005 Views - Last Post: 30 November 2010 - 06:56 AM Rate Topic: -----

#1 Guest_Adam*


Reputation:

VB .NET INSERT INTO TABLE with SQL

Posted 30 November 2010 - 05:34 AM

Hi there, I need help with my problem below:
I'm trying to insert data into a table in my database. I'm using MS Visual Studio 2005 with SQL Express
I get the error message: "Conversion failed when converting from datetime to character string".
Please help!

Dim command As String = ""
                'command = "INSERT INTO LogBook(SystemDate, DayAdded, MonthAdded, " & _
                '    "YearAdded, TimeAdded, FirstName, " & _
                '    "LastName, Description) " & _
                '    "VALUES('" & DateTime.Now.Date & "', '" & CInt(DateTime.Now.Day) & "', " & _
                '    "'" & CInt(DateTime.Now.Month) & "', '" & CInt(DateTime.Now.Year) & "', " & _
                '    "'" & DateTime.Now & "', '" & firstName & "', '" & lastName & "', " & _
                '    "'" & txtDescription.Text & "')"
                command = "INSERT INTO LogBook(SystemDate, DayAdded, MonthAdded, " & _
                    "YearAdded, TimeAdded, FirstName, LastName, Description) " & _
                    "VALUES(' & @p1 & ', ' & @p2 & ', '& @p3 &', '& @p4 &', '& @p5 &', '& @p6 &', '& @p7 &', '& @p8 &')"
                cmd = New SqlCommand(command, mainFormRef.ShareConnection)
                '
                cmd.Parameters.Add(New SqlParameter("@p1", SqlDbType.DateTime)) 'SystemDate
                cmd.Parameters("@p1").Value = DateTime.Now.Date
                cmd.Parameters.Add(New SqlParameter("@p2", SqlDbType.Int)) 'DayAdded
                cmd.Parameters("@p2").Value = CInt(DateTime.Now.Day)
                cmd.Parameters.Add(New SqlParameter("@p3", SqlDbType.Int)) 'MonthAdded
                cmd.Parameters("@p3").Value = CInt(DateTime.Now.Month)
                cmd.Parameters.Add(New SqlParameter("@p4", SqlDbType.Int)) 'YearAdded
                cmd.Parameters("@p4").Value = CInt(DateTime.Now.Year)
                cmd.Parameters.Add(New SqlParameter("@p5", SqlDbType.DateTime)) 'TimeAdded
                cmd.Parameters("@p5").Value = DateTime.Now
                cmd.Parameters.Add(New SqlParameter("@p6", SqlDbType.VarChar, 50)) 'FirstName
                cmd.Parameters("@p6").Value = firstName
                cmd.Parameters.Add(New SqlParameter("@p7", SqlDbType.VarChar, 50)) 'LastName
                cmd.Parameters("@p7").Value = lastName
                cmd.Parameters.Add(New SqlParameter("@p8", SqlDbType.VarChar)) 'Description
                cmd.Parameters("@p8").Value = txtDescription.Text
                '
                cmd.ExecuteNonQuery()

This post has been edited by modi123_1: 30 November 2010 - 08:30 AM
Reason for edit:: please use the code tags


Is This A Good Question/Topic? 0

Replies To: VB .NET INSERT INTO TABLE with SQL

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: VB .NET INSERT INTO TABLE with SQL

Posted 30 November 2010 - 05:37 AM

Moved this to the VB.Net forums, you'll get more helpful answers here.
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: VB .NET INSERT INTO TABLE with SQL

Posted 30 November 2010 - 06:56 AM

Remove the apostrophes, it should be
...VALUES(@p1, @p2, @p3 ... 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1