0

I have create a library management system. here if I want to update a book's particular record its updating all the records in the SQL-server database. how can I write code for update a particular record only. here is my code,

Private Sub btnedit_Click(sender As Object, e As EventArgs) Handles btnedit.Click

    con.ConnectionString = "data source=hp-pc\sqlexpress; initial catalog=Library_DB;integrated security= true"
    con.Open()
    Dim comd As New SqlCommand("update  Book set Book_Id='" & TextBox1.Text & "',Bk_Name='" & TextBox2.Text & "',Author_Name='" & TextBox3.Text & "', Year_of_release='" & TextBox4.Text & "',Availability_of_bks='" & TextBox5.Text & "'", con)
    comd.ExecuteNonQuery()
    MessageBox.Show("Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub
1
  • 1
    You need to add a where cause to your SqlCommand. Commented Nov 6, 2017 at 5:44

2 Answers 2

2

Add a WHERE clause in your SQL command to specify which book will be updated..
use the ID number of the book you want to update.
and avoid concatenating in your sql command, use parameter @

Dim comd As New SqlCommand("update  Book set Book_Id=@bookID, Bk_Name=@bkName, Author_Name=@author, Year_of_release=@release, Availability_of_bks=@avail WHERE Book_Id=@whereID", con)
comd.Parameters.Add("@bookID", SqlDbType.String).Value = TextBox1.Text
comd.Parameters.Add("@bkName", SqlDbType.String).Value = TextBox2.Text
comd.Parameters.Add("@author", SqlDbType.String).Value = TextBox3.Text
comd.Parameters.Add("@release", SqlDbType.String).Value = TextBox4.Text
comd.Parameters.Add("@avail", SqlDbType.String).Value = TextBox5.Text
comd.Parameters.Add("@whereID", SqlDbType.String).Value = "Book ID HERE"
comd.ExecuteNonQuery()
MessageBox.Show("Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add a WHERE clause to your SqlCommand so that SQL Server knows what record to update. Without a WHERE clause, it will update the entire table. See below:

 con.ConnectionString = "data source=hp-pc\sqlexpress; initial catalog=Library_DB;integrated security= true"
    con.Open()
    Dim comd As New SqlCommand("update  Book set Book_Id='" & TextBox1.Text & "',Bk_Name='" & TextBox2.Text & "',Author_Name='" & TextBox3.Text & "', Year_of_release='" & TextBox4.Text & "',Availability_of_bks='" & TextBox5.Text & "' WHERE Book_Id='{**Put your book id here**}'", con)
    comd.ExecuteNonQuery()
    MessageBox.Show("Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

3 Comments

If Book_Id is a number such as 10, you don't need the quotes so the query becomes WHERE Book_Id=10.
Got it, Thanks for your answer
You re most welcome! Please mark as answer if that answer helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.