Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I created a small application and some forms for someone to be able to create a user account and then go to a login screen and the user can login with the account that they just created.
Now my next step is I would like to bring up a new form which I created and I want to be able to use that specific users information on there account.
I want to display some of that users information in the textboxes I have created for the user that logged in
Basically just using the users information passing it to the next page is all I am trying to do
What would be the best way for me to do this?
VB
‘ the user logs in using this button
Private Sub btnLoginNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoginNow.Click
        Try
            con.Open()
            With cmd
                .Connection = con
                .CommandText = "SELECT * FROM dbo.People WHERE UserName = '" & txtUsername.Text & "' AND Password = '" & txtPassword.Text & "'"

                dr = cmd.ExecuteReader

                If (dr.Read() = True) Then
                    MsgBox("User is valid")

                    Me.Close()
                    LoginAfter.Show()
                Else
                    MsgBox("Invalid username or password!")
                End If
                'If result = 0 Then
                '    MsgBox("Login attemp failed")
                'Else
                '    MsgBox("Login successful")
                'End If
            End With
            dr.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            con.Close()
        End Try
    End Sub
‘creating and loading data    
'creating CRUD create read update delete to database
    Public Sub CreateMethod(ByVal sql As String)
        Try
            'open connection
            con.Open()
            'hold the data
            With cmd
                .Connection = con
                .CommandText = sql
                'execute the data
                result = cmd.ExecuteNonQuery
                dr = cmd.ExecuteReader
                If result = 0 Then
                    MsgBox("No data save", MsgBoxStyle.Information)
                Else
                    MsgBox("Data saved to the database")
                End If
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        con.Close()
    End Sub

    'holding the data to retrieve
    Public Sub Reload(ByVal sql As String)
        Try
            dt = New DataTable
            con.Open()
            With cmd
                .Connection = con
                .CommandText = sql
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        con.Close()
        da.Dispose()
    End Sub
Private Sub btnReload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReload.Click
        'reload data from database
        Try
            Reload("SELECT * FROM dbo.People")
            FillTable(DataGridView1)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
Posted
Comments
ZurdoDev 9-Sep-15 16:12pm    
There is rarely a "best" way to do anything. However, I'd suggest passing the id to the next form and let it look up the information do what it needs to.
ZurdoDev 9-Sep-15 16:13pm    
Also, change your sql so that you are not exposed to sql injections.

.CommandText = "SELECT * FROM dbo.People WHERE UserName = @UserName AND Password = @Password"
.Parameters.AddWithValue("@UserName", txtUserName.Text)
.Parameters.AddWithValue("@Password", txtPassword.Text)
TheBigBearNow 9-Sep-15 16:17pm    
could you give me an example for passing the id please and thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900