Andy_Morgan
Andy_Morgan

Reputation: 11

I am unable to enter data into sql table using vb.net?

My table structure is :

       pid nvarchar(15) //foreign key
       mgfdate datetime
       duration numeric(18,0)
       warrantytype varchar(20)
       purchasedate datetime
       expirydate datetime

My Sql query is :

    Dim s1 as String = "insert into productwarranty values(@pid,@mgfdate,@warrantytype,@duration,@purchasedate,@expirydate)"

    Public Function prodwarrantyinsert(ByVal query As String, ByVal product As productform)
    Dim i As Integer = 0
    Dim d As Integer = CInt(product.ptb10.Text)
    Try
        connect()
        cmd = New SqlCommand(query, conn)
        Dim ed As String = CStr(product.ptb11.Text)
        Dim md As String = product.pdt7.Value.ToString
        Dim pd As String = product.pdt9.Value.ToString
        cmd.Parameters.AddWithValue("@pid", PID)
        cmd.Parameters.AddWithValue("@mgfdate", md)
        cmd.Parameters.AddWithValue("@duration", d)
         cmd.Parameters.AddWithValue("@warrantytype", product.ptb8.Text)
        cmd.Parameters.AddWithValue("@purchasedate", pd)

        cmd.Parameters.AddWithValue("@expirydate", ed)

        i = cmd.ExecuteNonQuery()



    Catch ex As Exception
        MessageBox.Show(ex.Message)

    Finally
        disconnect()
    End Try

    Return i
End Function

I constantly keep getting an error as "Error converting datatype nvarchar to numeric"

Any help would be appreciated guys.

Upvotes: 0

Views: 170

Answers (2)

D Stanley
D Stanley

Reputation: 152491

If you don't sepcify the column order in your insert, you must use the natural order in the VALUES section. You seem to have @warrantytype and @duration swapped. I would be safe and specify the columns:

Dim s1 as String = "insert into productwarranty " & _
                   "(pid, mgfdate, warrantytype, duration, purchasedate, expirydate) " & _                          
                   "values(@pid, @mgfdate, @warrantytype, @duration, @purchasedate, @expirydate)"

Upvotes: 1

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

You try to insert text

Dim ed As String = CStr(product.ptb11.Text)

into field of datetime type

cmd.Parameters.AddWithValue("@expirydate", ed)

which maybe can't be converted.

Upvotes: 0

Related Questions