To a large extent, this means an error message - the Connection property of the SqlCommand object was not assigned to an open connection (in this case you called myConnection ).
In addition, advice is given here. Read some sql parameters - doing sql concatenation with user input without any conformance checks is a way to attack SQL injection .
This is one way to do this:
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click Try myConnection.Open() myCommand = New SqlCommand( _ "INSERT INTO tblBook(BookCode, BookTitle, Author, PublishingYear, Price, " & _ " EnterDate, CatID, RackID, Amount) " & _ "VALUES(@bookCode, @bookTitle, @author, @publishingYear, @price, @enterDate, " & _ " @catId, @rackId, @amount)") myCommand.Connection = myConnection with myCommand.Parameters .AddWithValue("bookCode", txtBookCode.Text) .AddWithValue("bookTitle", txtTitle.Text) .AddWithValue("author", txtAuthor.Text) .AddWithValue("publishingYear", txtPublishYear.Text) .AddWithValue("price", txtPrice.Text) .AddWithValue("enterDate", txtEnterDate.Text) .AddWithValue("catId", txtCategory.Text) .AddWithValue("rackId", txtRack.Text) .AddWithValue("amount", txtAmount.Text) end with myCommand.ExecuteNonQuery() MsgBox("The book named '" & txtTitle.Text & "' has been inseted successfully") ClearBox() Catch ex As Exception MsgBox(ex.Message()) End Try myConnection.Close() End Sub
source share