Private Sub btnInsert_Click(sender As System.Object, e As System.EventArgs) Handles btnInsert.Click
        Dim con As New OleDbConnection
        Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader

        Dim Pnum As Integer
        Dim InDate As Date
        Dim PDate As Date
        Dim Amount As Integer
        Dim CmpID As Integer
        Dim Tax As Integer
        Dim TaxAmt As Integer
        Dim Innumber As Integer
        Dim Totamt As Integer
        Dim Qty As Integer


        con = New OleDbConnection("Provider=Microsoft.ace.Oledb.12.0;Data Source= C:\Users\Lenovo\Documents\Visual Studio 2010\Soft\Database\Login.accdb")
        con.Open()

        If txtamt.Text = "" And txtID.Text = "" And txtqty.Text = "" And txtinvoiceno.Text = "" And txtno.Text = "" And txttax.Text = "" And txttaxamt.Text = "" And txttotamt.Text = "" Then
            MessageBox.Show(" Enter All The Feilds", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Else

            Pnum = Convert.ToInt32(txtno.Text)
            InDate = DateTimePicker1.Text
            PDate = DateTimePicker2.Text
            Amount = Convert.ToInt32(txtamt.Text)
            CmpID = Convert.ToInt32(txtID.Text)
            Tax = Convert.ToInt32(txttax.Text)
            TaxAmt = Convert.ToInt32(txttaxamt.Text)
            Innumber = Convert.ToInt32(txtinvoiceno.Text)
            Totamt = Convert.ToInt32(txtamt.Text)
            Qty = txtqty.Text


            cmd = New OleDbCommand("INSERT INTO Purchase1 VALUES( '" & Pnum & "', '" & InDate & "', '" & PDate & "', '" & Amount & "','" & CmpName & "','" & Tax & "'," & CmpID & ", '" & TaxAmt & "', '" & Innumber & "', '" & Totamt & "', '" & Qty & "')", con)
            cmd.ExecuteNonQuery()
            MessageBox.Show("Added successfully", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If


        cmd = New OleDbCommand("Select * from  Purchase1", con)
        dr = cmd.ExecuteReader()
        Dim dt As New DataTable("Purchase1")
        dt.Load(dr)
        DataGridView1.DataSource = dt
    End Sub
End Class

Can any one telll me why Am I getting the error (Operator '&' is not defined for types 'String' and 'System.Windows.Forms.Label' ) in the above code.

Recommended Answers

All 2 Replies

Date can assigned like this also:

Dim dateOfBirthAsString As String = dtpDOB.Value

When you inserting into a table you write query like this:

    Dim myCommand As SqlCommand

myCommand = New SqlCommand("Insert into Student(Column1,Column2,Column3,Column4,Column5,Column6) values VALUES( '" & Pnum & "', '" & InDate & "', '" & PDate & "', '" & Amount & "','" & CmpName & "','" & Tax & "'," & CmpID & ", '" & TaxAmt & "', '" & Innumber & "', '" & Totamt & "', '" & Qty & "')", con)
        myCommand.ExecuteNonQuery()

What line is throwing the error?

Why bother converting all of the control values to integers and dates when all you are doing is putting them back into a string?

Don't put single quotes around values in the query when the corresponding database field is numeric.

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.