Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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

Edited by chicago75
  • Moderators
Posted

For Update,Delete Insert queries you can use the Command object and the Excecute method....

Here's a simple function to do this...

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

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...