Update statement in vb.net 2008 using SQL 2008-VBForums
Results 1 to 6 of 6

Thread: Update statement in vb.net 2008 using SQL 2008

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    3

    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

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,241

    Re: Update statement in vb.net 2008 using SQL 2008

    My.Settings.Smitty
    Which would be ..... ?

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,237

    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 -

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    3

    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

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,237

    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 -

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    3

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.