Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 My Global Connection method is correct ???

$
0
0
Dear All,

I am doing a project in vb.net with back end ms-access 2003, I decided my application to works on LAN (Database in one node(server) and many nodes (client) accessing the DB... through LAN and I will install my application in all node machine.

My Application goes likes this...
Two forms (say) frmMain and frmEntry (frmMain has link to open frmEntry using manustrip).

My Global DB Connection in frmMain.
Code:

Public Con As OleDb.OleDbConnection    ' Con declare as public tp access through out the application

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DBConnection()
End Sub

Public Sub DBConnection()
        Dim DBPath As String
        Dim lines = System.IO.File.ReadAllLines(Application.StartupPath + "/DBPath.txt")
        DBPath = lines(0)
        Con = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath & "\SoftSkill.mdb;User Id=admin;Password=;")
        Try
            Con.Open()
        Catch ex As Exception
            MsgBox("Could not find database" & Environment.NewLine & "Make sure the database path is correct or Check network connection ", vbCritical)
            ' '' ''MsgBox(ex.ToString)
            Me.Close()
        End Try
        Con.Close()
End Sub

In frmEntry there is some insert query which uses frmMain.Con like this...
Code:


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

frmMain.Con.Open()    'connection open from main form

Dim SQL_Cust As String = "INSERT INTO EntryRegister (StudentName,StudentNo,Date) VALUES (@StudentName,@StudentNo,@Date)"
                   
Dim Cmd_Insert As New OleDbCommand(SQL_Cust, frmMain.Con)
                   
                    With Cmd_Insert
                            .Parameters.AddWithValue("@StudentName", StudentName)
                            .Parameters.AddWithValue("@StudentNo", StudentNo)
                            .Parameters.AddWithValue("@Date", Date)
                            .ExecuteNonQuery()
                            .Parameters.Clear()
                    End With

frmMain.Con.Close()
End Sub

Kindly advice...

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>