Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, July 27, 2017 4:22 PM
I am using the following code to update some records but when I hit the update button I get this error message “ExecuteNonQuery: connection property has not been initialized” I have an idea what it’s saying but I am sure how to initialize it. Every help will be appreciated. Some the things I tries did not work
Dim conn As OleDbConnection
Dim oledbAdapter As New OleDbDataAdapter
conn = New OleDbConnection(connStr)
Dim cmd As New System.Data.OleDb.OleDbCommand
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText = "UPDATE LibBkRegistration set ISBN=@ISBN, CurrentDate=@Cdate, Version=@Vers" & _
" , Subject=@Subj, Price=@Prc, ShelfNumber=@SNumber, YearPublished=@YrPublished, Publisher=@Publisher, Title=@Title, Author=@Auth "
conn.Open()
Try
cmd.ExecuteNonQuery()
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
All replies (3)
Friday, July 28, 2017 3:54 AM ✅Answered
Hi alobi,
You didn't tell the cmd object, which open connection is to use, just write this statement before executing command.
cmd.Connection = con
By the way, you can refer to the code below, use parameter in update sql.
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim row As Integer
'Dim str As String
con = New SqlConnection("server=.;database=Test;integrated security=true")
con.Open()
cmd = New SqlCommand("UPDATE T_Programme Set pro_nom=@nom , pro_nbr_unites=@unit , pro_nbr_heures=@heures WHERE pro_no =@no", con)
cmd.Parameters.Add("@no", SqlDbType.VarChar, 6)
cmd.Parameters.Add("@nom", SqlDbType.VarChar, 50)
cmd.Parameters.Add("@unit", SqlDbType.Float)
cmd.Parameters.Add("@heures", SqlDbType.Int)
''''Set Values
cmd.Parameters("@no").Value = "1234"
cmd.Parameters("@nom").Value = "qwerty"
cmd.Parameters("@unit").Value = 12.0
cmd.Parameters("@heures").Value = 2
row = cmd.ExecuteNonQuery()
con.Close()
Best Regards,
Cherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Thursday, July 27, 2017 5:11 PM
You have no command.connection. You can look at the example in the link.
Thursday, July 27, 2017 7:04 PM
See my reply in the thread below which shows how to do an update.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator