BACKGROUND: I am writing an application in VB that ask for a user to login and enter in some data from product test.
PROBLEM: I have login form that talks to a sql database and makes sure the person is supplying the right username/password. The username gets passed to the main form. From here I want to be able to fill records from our main table that was added by the user that logged in. So the user can only interact with the records they have entered.
My issue is that I am fairly confident in VB, and I took a class in SQL but I have zero idea how to search this issue to figure out the proper way. I have looked a tableadapters.fill and .getdata but it doesn't seem to be the answer.
Login Form:
Imports System.Data
Imports System.Data.SqlClient
Public Class frmLogin
Dim userName As String
Dim extrainfo As String
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
If ConnectToSQL() Then
Dim frm As New frmQuality(userName, extrainfo)
Me.Hide()
frm.Show()
Me.Close()
End If
End Sub
Private Function ConnectToSQL() As Boolean
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim dbPassword As String
Dim enteredPasswrd As String
' Dim userName As String
Try
con.ConnectionString = "Data Source "
con.Open()
cmd.Connection = con
cmd.CommandText = " SELECT UserName, Password, extrainfo FROM users WHERE (UserName like '" & "%" & txtUsername.Text.ToLower() & "%" & "' )"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
If lrd.HasRows Then
While lrd.Read()
'Do something here
dbPassword = lrd("Password").ToString()
userName = lrd("UserName").ToString()
extrainfo = lrd("assigned_ei").ToString()
enteredPasswrd = txtPassword.Text()
If dbPassword.Trim() = enteredPasswrd And userName.TrimEnd(vbCrLf.ToCharArray).ToLower() = txtUsername.Text.ToLower() Then
Return True
'Clear all fields
txtPassword.Text = ""
txtUsername.Text = ""
Else
MsgBox("Incorrect Password")
End If
End While
Else
MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'Clear all fields
txtPassword.Text = ""
txtUsername.Text = ""
End If
Return False
Catch ex As Exception
MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
con.Close() 'Whether there is error or not. Close the connection.
End Try
End Function
Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtUsername.Focus()
End Sub
End Class
Main form
Pic:https://i.stack.imgur.com/tnJOv.png
Code
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Data Source = location"
con.Open()
cmd.Connection = con
cmd.CommandText = " SELECT * FROM table WHERE adduser = '" & user & "'"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
If lrd.HasRows Then
While lrd.Read()
' i see that above can pull the info
' but how do I get it into the form?
End While
End If
Catch ex As Exception
MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
con.Close() 'Whether there is error or not. Close the connection.
End Try
End Sub
I have a database with the main table has 9 columns of information. One being called addUser which is the user that logs in and enters data. I need to have when they log in that it only displays their records. I do not want to directly attach to the textbox. As you can see from the form you can hit move around see from first to last record of the owner. I need to be able to browse the records. A person logs in and could 1 to 10 or more records. This application will let them move or create a new record. How do I do this?