Problem with Database connection

Posted on 2009-12-28
Last Modified: 2013-11-08
Hi, i need some pointers and help how to code the folowing

i have a Form which has a couple of fields which represent Servername, userid and Password as well as a
Bttn on the form which opens a connection based on the data provided. I enclosed the code below..

My problem is that i am able to open the connection just fine but as soon as i try to access the connection
from a diffrent part of code in form i have no luck. So my question is where and how do i declare the connection so it
can be opened via my bttn but then accessed by other code outside the click event.


Private Sub bttnConnectDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnConnectDB.Click

        If Me.txtServerName.TextLength <> 0 Then

            If Me.txtUserName.TextLength <> 0 Then

                myconnectionString = "Data Source=" & Trim(txtServerName.Text) & ";Persist Security Info=True;User ID=" & Trim(txtUserName.Text)

                Dim result As Boolean = False

                Dim connection As New SqlConnection(myconnectionString)



                    result = True

                    Me.lblServerVersion.Text = connection.ServerVersion

                Catch exSQL As SqlException


                    result = False

                Catch ex As Exception


                    result = False


                    '   connection.Close()


                End Try


                MsgBox("Please enter a valid User Name for the Database server", MsgBoxStyle.Exclamation, "Error...")

            End If


            MsgBox("Please enter a valid address or name for the Database server", MsgBoxStyle.Exclamation, "Error...")

        End If


    End Sub

Open in new window

Question by:AlexPonnath
    1 Comment
    LVL 62

    Accepted Solution

    Hi AlexPonnath;

    Define this at class level, outside of any function/method/Sub/Event handler.

    Private connection As SqlConnection

    Then replace this line in the event handler:

    Dim connection As New SqlConnection(myconnectionString)

    With this:

    connection = New SqlConnection(myconnectionString)

    The problem is that you are defining the connection inside of a function and will not be available to other methods.


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now