0

What is wrong with my code? when i run the program, it says ExecuteNonQuery: Connection property has not been initialized." Thank you. Please help me.

       Private Sub btnentidel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnentidel.Click
    conn.Open()
    Dim cmd As New SqlCommand
    cmd.CommandText = String.Format("DELETE FROM tblentityreg WHERE [Registered Business Trade Name] = @p1")
    cmd.Parameters.AddWithValue("@p1", txtregplace.Text)
    Dim affectedRows = cmd.ExecuteNonQuery
    Dim dr As SqlDataReader = cmd.ExecuteReader
    Dim cmd1 As SqlCommand = conn.CreateCommand
    cmd1.CommandText = "SELECT * FROM tblentityreg"
    Dim dtentity As New DataTable
    dtentity.Load(dr)
    DataGridView1.DataSource = dtentity
    dr.Close()
    If affectedRows > 0 Then
        MsgBox("Succesfully Deleted the Record!")
    Else
        MsgBox("Failed!")
    End If
    conn.Close()
    txtentisearch.Text = ""
End Sub

0 Answers0