SQL UpdateCommand not executing..Help!!!

chicago75

Member
Joined
Nov 27, 2002
Messages
8
All,
THis is confusing, but I am probably missing something very obvious. When this code is executed, the debug locals show the command text to have values (CID=19, Custfnu=brian, etc...) but the information is not written to the DB.. It is my understanding that with the string and the connection in the declaration, I do not need the ExecuteNonQuery statement. Is this correct? WHy is it not working?


Here is the code...




Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim SQLCN As New SqlClient.SqlConnection("Server=ICS-SVR1;User ID=sa;Password=;Database=ICS-DISPATCH")
SQLCN.Close()
SQLCN.Open()
Dim SqlDataAdapter1 As New SqlDataAdapter("UPDATE custdb SET custfn=" & "" & custfnu.Text & " , " & "custln= " & custlnu.Text & " ," & "custcn= " & custcnu.Text & ", " & "custadd1=" & custadd1u.Text & " , " & "custadd2=" & custadd2u.Text & " , " & "custcity=" & custcityu.Text & " , " & "custst= " & "" & custstu.Text & ", " & "custZIP=" & custzipu.Text & ", " & "custph=" & custphu.Text & ", " & "custnote=" & CustNoteu.Text & " " & "WHERE cid=" & cidu.Text & ";", SQLCN)
Me.Close()

End Sub
 
Last edited by a moderator:
For Update,Delete Insert queries you can use the Command object and the Excecute method....
Heres a simple function to do this...
Code:
Friend Function ExecuteNonQuery(ByVal sSql As String) As Integer
        Dim cmd As SqlCommand
        Dim con As New SqlConnection(YOUR_CONNECTION_STRING)
        Dim recordsAffected As Integer

        Try
            If con.State = ConnectionState.Closed Then con.Open()
            cmd = New SqlCommand(sSql, con)
            recordsAffected = cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
            recordsAffected = -1
        Finally
            If con.State = ConnectionState.Open Then con.Close()
            If Not cmd Is Nothing Then cmd.Dispose()
        End Try
        Return recordsAffected returns more than 0 if successful
    End Function
 
Back
Top