Search code examples
vb.netmdi

SQL Result to Global Variable


Within my MDIParent Me_Load I have an SQL query that returns user information based upon Windows ID. This works well, however I'd really like to move this logic out into perhaps a module and assign each value in the db to a global variable to be used elsewhere. I'd like to be able to access the contact_id in any child form of the parent MDI. I'm used to PHP where I'd just assign it to a session variable that I could reference anywhere.

This is my current SQL Code

            Dim sql_query As String

        Dim errorMessages As New StringBuilder()
        Dim cnn = ConfigurationManager.ConnectionStrings("sql_connection_string").ConnectionString
        Dim adapter As SqlDataAdapter
        Dim ds As New DataTable()
        Dim User_ID As String
        Dim User_First_Name As String
        Dim User_Last_Name As String
        Dim User_Contact_CD As String
        Dim User_Login As String

        sql_query = "SELECT Contact_ID, First_Name_CH, Last_Name_CH, Contact_CD, Login_VC FROM [Worktool].[dbo].[vwEmployees_T] WHERE Login_VC = '" & username & "'"

        Using connection As New SqlConnection(cnn)
            Try
                If connection.State = ConnectionState.Closed Then connection.Open()
                adapter = New SqlDataAdapter(sql_query, connection)
                adapter.Fill(ds)
                User_ID = ds.Rows(0)("Contact_ID").ToString()
                User_First_Name = ds.Rows(0)("First_Name_CH").ToString()
                User_Last_Name = ds.Rows(0)("Last_Name_CH").ToString()
                User_Contact_CD = ds.Rows(0)("Contact_CD").ToString()
                User_Login = ds.Rows(0)("Login_VC").ToString()
                connection.Close()
            Catch ex As SqlException
                MsgBox("Sorry, there was an issue with the connection. Please try again ! ")
                Dim i As Integer
                For i = 0 To ex.Errors.Count - 1
                    errorMessages.Append("Index #" & i.ToString() & ControlChars.NewLine _
                        & "Message: " & ex.Errors(i).Message & ControlChars.NewLine _
                        & "LineNumber: " & ex.Errors(i).LineNumber & ControlChars.NewLine _
                        & "Source: " & ex.Errors(i).Source & ControlChars.NewLine _
                        & "Procedure: " & ex.Errors(i).Procedure & ControlChars.NewLine)
                Next i
                MsgBox(errorMessages.ToString())
            End Try
        End Using


    'Assign messages
        main_window_welcome.Text = "Welcome back, " & Replace(User_First_Name, " ", "") & " " & Replace(User_Last_Name, " ", "")

variable username is

Public username = Environ$("Username")

Solution

  • You've declared the 4 variables in the class and they are private to that class. At this point your code works. Hilight those 4 variable declarations and Cut them. Your code shows errors because you just removed the declarations.

    Add a module to your solution (name it what you want) paste the declarations into the module body.

    change the Dim to Public.

    Your errors disappear.

    Your variables are now public and available throughout your solution.