-
Aug 13th, 2012, 01:36 PM
#1
Thread Starter
New Member
Update statement in vb.net 2008 using SQL 2008
I am working on a windows application that tracks repairs for my business. The goal is to use LabelID (Which is the ID of the record) and update it with the text from combostatus.text
This is what I have so far:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim conn As New SqlConnection(My.Settings.Smitty)
conn.Open()
Dim cmd As SqlCommand = New SqlCommand("Update Repairs set status = ' " & ComboBox1.Text) '" WHERE RepairNumber = '" & Label1.Text & " '", conn))
cmd.ExecuteNonQuery()
conn.Close()
End Sub
I keep getting this error message: ExecuteNonQuery: Connection property has not been initialized.
I have tried all different kinds of combinations and I get one error or another. I appreciate any help I can get on this. Thanks
-
Aug 13th, 2012, 01:59 PM
#2
Re: Update statement in vb.net 2008 using SQL 2008
-
Aug 13th, 2012, 01:59 PM
#3
Re: Update statement in vb.net 2008 using SQL 2008
What is the actual value of My.Settings.Smitty at runtime? And what is the data type of RepairNumber field in your database?
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Aug 13th, 2012, 02:11 PM
#4
Thread Starter
New Member
Re: Update statement in vb.net 2008 using SQL 2008
The repairnumber is an int. It is also the primary key. The connection string is Data Source=******;Initial Catalog=******;Persist Security Info=True;User ID=*****;Password=*****
I know there is nothing wrong with the connection string. I use this on other parts of the application. Thanks
-
Aug 13th, 2012, 02:15 PM
#5
Re: Update statement in vb.net 2008 using SQL 2008
Try this:
Code:
Using conn As New SqlConnection(My.Settings.Smitty)
Using cmd As New SqlCommand()
With cmd
.Connection = conn
.CommandText = "Update Repairs set status = @status WHERE RepairNumber = @repairNumber"
.Parameters.AddWithValue("@status", ComboBox1.Text)
.Parameters.AddWithValue("@repairNumber", CInt(Label1.Text))
End With
conn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Aug 13th, 2012, 02:39 PM
#6
Thread Starter
New Member
Re: Update statement in vb.net 2008 using SQL 2008
Thanks for your help. It worked. I never thought about approaching it from the direction.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|