Question Executing Sql Query. Need Someone To Check The Code!

dejanc

New member
Joined
Dec 31, 2010
Messages
3
Programming Experience
Beginner
Hello VbDotNetForums,
So far I have learn some basics. I`m now at the point calling server from vb.net, and run query inside vb.net code.
I have write two codes on button click. One button is checked server connection, other should execute query for server table.
Connection code is working, as I have set two messages, and getting successfull message.
Second code is not working, as nothing happened when press button.
Sql statement is very simple, and it works if I run it manually.

So, need someone to check below firs Privat Sub code, or and give me some helpful tips.

Imports System.Data.SqlClient
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim SQLCONN As New SqlConnection
Dim SQLCMD As New SqlCommand
SQLCONN.ConnectionString = "Server=DEJANC-PC\SQLEXPRESS;Database=TEST;Integrated security=True"
SQLCONN.Open()
SQLCMD.Connection = SQLCONN
SQLCMD.CommandType = CommandType.Text
SQLCMD.CommandText = "DELETE FROM TABLE1"
SQLCMD.ExecuteNonQuery()
SQLCONN.Close()

End Sub



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

Dim sqlconn As New SqlConnection
sqlconn.ConnectionString = "Server=DEJANC-PC\SQLEXPRESS;Database=TEST;Integrated security=True"
Try
sqlconn.Open()
Catch ex As Exception
MessageBox.Show("Error while connecting to sql server." & ex.Message)
End Try
If sqlconn.State = 1 Then
MessageBox.Show("Successfull")
End If

End Sub


Thank you in advance to anyone for help!
 
Foun solution:

Dim con As New SqlConnection("Server=DEJANC-PC\SQLEXPRESS;Database=TEST;Integrated security=True")
Dim cmd As New SqlCommand("INSERT INTO Table2(FirstName, SecondName) SELECT FirstName, SecondName FROM Table1", con)
con.open()

cmd.ExecuteNonQuery()
 
Back
Top