Pls help me(Beginner in VB.net ) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Pls help me(Beginner in VB.net )

takumar.engtakumar.eng Posts: 1Member
I am making a application having many forms on VB.net.

In every form I am using

[b]Dim con As MySqlConnection

Dim str As String

Dim cmd As MySqlCommand

Dim dr As MySqlDataReader

Dim pass As String[/b]

for database connection declaration
and
using

[b]con = New MySqlConnection
con.ConnectionString = connectionstring
Try
'con = New MySqlConnectioncon.ConnectionString = Sample.connectionstring
Catch ex As Exception
MsgBox("connection not establisbed")
End Try[/b]
for connection establishment

Now My Problem

I want to make this access this snippets from a single class to all my forms. Is it Possible. Pls reply me immediately.
if possible send the code.

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    I am not exactly sure I understand your question. Are you trying to make that connection global so you can use it on any form?

    If so, you can do that by adding a module to your project. Add a new module (call it what you like and add this code):

    [code]

    Module Whatyoulike

    Public con As MySqlConnection
    Public str As String
    Public cmd As MySqlCommand
    Public dr As MySqlDataReader
    Public pass As String

    Public Sub InitConnection(ByVal ConnectStr as String)
    connectionstring = ConnectStr
    con = New MySqlConnection
    con.ConnectionString = connectionstring
    Try
    'con = New MySqlConnectioncon.ConnectionString = Sample.connectionstring
    Catch ex As Exception
    MsgBox("connection not establisbed")
    End Try
    End Sub

    End Module

    [/Code]

    Inside your code you can init your connection by calling InitConnection and passing it a correct Connection string. Now the Con object can be used on any form, just be sure to check if it is working before doing data transfers, and make sure you close the connection when the application closes (or when you are done with it for the rest of the routine)

    -Hope this Helps
    -sC
Sign In or Register to comment.