Link to home
Start Free TrialLog in
Avatar of Andrew Parker
Andrew ParkerFlag for Australia

asked on

Storing Userid globally after login

Hi

I have just worked out the login page and it works fine,  I now need to store the id of the login for use in the next form.

Private Sub btnOK_click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles btnOk.Click

        Dim SQL As String = "SELECT * FROM Surveyors WHERE SurveyorName = '" & txtLoginName.Text & "' AND Password  = '" & txtPassword.Text & "'"
        Dim sqlReader As SqlDataReader = RetrieveData(SQL)

        If sqlReader.Read Then
            globalSurveyorId = 1
            MsgBox(globalSurveyorId)
            Dim Main As New FormMain()
            Main.Show()

        Else
            MsgBox("Not OK")
        End If
        sqlReader.Close()
        sqlReader = Nothing

    End Sub

End Class

Thanks
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

In your module you must declare your variable

Public globalSurveyorId As Int16

after the  If sqlReader.Read Then

' ID is your field name
globalSurveyorId  = sqlReader.Item("ID")

now you can use it whenever you want
Avatar of Andrew Parker

ASKER

Cool, Works a treat.

That logs me in and sets the SurveyorId, This now populates a ComboBox of all there contracts

Do you know how to pull the contract number from a combobox which is showing Contract name.  I ahve at present

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

        'globalContractNumber = 423
        'DisableFormCloseButton(Me)

        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT ContractNumber, ContractName FROM Contracts Where SurveyorId = " & globalSurveyorId

        Try
            cmd.Connection.Open()
            dr = cmd.ExecuteReader
            While dr.Read
                MyComboBox.Items.Add(dr.GetValue(dr.GetOrdinal("ContractName")).ToString)
            End While
        Catch ex As SqlException
            MessageBox.Show(ex.Message)
        Finally
            cmd.Connection.Close()
        End Try

        MyComboBox.SelectedValueChanged()
        Dim val As String
        val = MyComboBox.SelectedValue

    End Sub


    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        'MyComboBox_SelectedValueChange("ContractId")

        Dim ContractNumber As String

        globalContractNumber = ContractNumber
        Dim Test As New Formtest()
        Test.Show()
    End Sub

ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, does the bottom part of the code go into the button or within the try section?
sorry, me being doppy, I didnt realise that the tag value should be replace with the "ID" column name

Thanks