Hi i'm new in VB2008 and i have a problem about SQL connection..
this is my module code
Imports System.Data.SqlClient
Module Module1
Public sqlcnn As New SqlConnection
Public sqlcom As New SqlCommand
Public sqldr As SqlDataAdapter
Public strsql As String
Public sqlda As New SqlDataAdapter
Sub connect()
If sqlcnn.State = ConnectionState.Open Then
sqlcnn.Close()
sqlcnn.ConnectionString = "Data Source=.\sqlexpress; integrated security = true; attachdbfilename,=|datadirectory|\SQLSAMPLE.mdf"
sqlcnn.Open()
End If
End Sub
End Module
MY FORM CODE
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Module1.connect()
End Sub
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
strsql = "insert into sample (fname, lname, address) values ('" _
& txtfname.Text & "','" _
& txtlname.Text & "','" _
& txtadd.Text & "')"
Dim sqlcom As New SqlClient.SqlCommand
sqlcom.CommandText = strsql
sqlcom.Connection = sqlcnn
sqlcom.ExecuteNonQuery() <<<<< this is my problem "ExecuteNonQuery requires an open and available Connection. The connection's current state is closed."}
MsgBox("SAVE")
End Sub
End Class
Thanks.......
this is my module code
Imports System.Data.SqlClient
Module Module1
Public sqlcnn As New SqlConnection
Public sqlcom As New SqlCommand
Public sqldr As SqlDataAdapter
Public strsql As String
Public sqlda As New SqlDataAdapter
Sub connect()
If sqlcnn.State = ConnectionState.Open Then
sqlcnn.Close()
sqlcnn.ConnectionString = "Data Source=.\sqlexpress; integrated security = true; attachdbfilename,=|datadirectory|\SQLSAMPLE.mdf"
sqlcnn.Open()
End If
End Sub
End Module
MY FORM CODE
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Module1.connect()
End Sub
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
strsql = "insert into sample (fname, lname, address) values ('" _
& txtfname.Text & "','" _
& txtlname.Text & "','" _
& txtadd.Text & "')"
Dim sqlcom As New SqlClient.SqlCommand
sqlcom.CommandText = strsql
sqlcom.Connection = sqlcnn
sqlcom.ExecuteNonQuery() <<<<< this is my problem "ExecuteNonQuery requires an open and available Connection. The connection's current state is closed."}
MsgBox("SAVE")
End Sub
End Class
Thanks.......