Andrew Parker
asked on
Basic login script help
Hi
I have just created a login form for my app, it is simple, it has two text boxes (username, password)
I need to validate this againt a DB called "Test", table called "Surveyors", columns called "SurveyorName" & "Password".
Any help is appriciated, cant find any good exmples in the books I have to on previous question on here.
Thanks
Andrew Parker
I have just created a login form for my app, it is simple, it has two text boxes (username, password)
I need to validate this againt a DB called "Test", table called "Surveyors", columns called "SurveyorName" & "Password".
Any help is appriciated, cant find any good exmples in the books I have to on previous question on here.
Thanks
Andrew Parker
Something like this (to simplify)
SQL = "SELECT * FROM Surveyors WHERE SurveyorName = '" & txtLoginName.Text & "' AND Password = '" & txtPassword.Text
dim cmd as SqlCommand = SqlCommand (SQL, objConnection)
dim dtr as SqlDataReader = cmd.ExecuteReader()
If dtr.Read Then
' OK
End If
SQL = "SELECT * FROM Surveyors WHERE SurveyorName = '" & txtLoginName.Text & "' AND Password = '" & txtPassword.Text
dim cmd as SqlCommand = SqlCommand (SQL, objConnection)
dim dtr as SqlDataReader = cmd.ExecuteReader()
If dtr.Read Then
' OK
End If
ASKER
Morning,
Ok, now you are confusing me, :)
' I have this at the moment, Should your code be going in the declarations sections or within the button click for the ok button
Think this code may be wrong, esp the sqlcommand call, please help.
Private Sub btnOK_click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnOk.Click
Dim objConnection As SqlConnection = New _
SqlConnection("data source=sqlserver;Initial Catalog=Test;user id=sa;password=")
Dim ObjDataAdaptor As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Surveyors WHERE SurveyorName = '" & txtLoginName.Text & "' AND Password = '" & txtPassword.Text, objConnection)
Dim cmd As SqlCommand = SqlCommand(ObjDataAdaptor, objConnection)
Dim dtr As SqlDataReader = cmd.ExecuteReader()
If dtr.Read Then
' OK
End If
End Sub
Ok, now you are confusing me, :)
' I have this at the moment, Should your code be going in the declarations sections or within the button click for the ok button
Think this code may be wrong, esp the sqlcommand call, please help.
Private Sub btnOK_click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnOk.Click
Dim objConnection As SqlConnection = New _
SqlConnection("data source=sqlserver;Initial Catalog=Test;user id=sa;password=")
Dim ObjDataAdaptor As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Surveyors WHERE SurveyorName = '" & txtLoginName.Text & "' AND Password = '" & txtPassword.Text, objConnection)
Dim cmd As SqlCommand = SqlCommand(ObjDataAdaptor,
Dim dtr As SqlDataReader = cmd.ExecuteReader()
If dtr.Read Then
' OK
End If
End Sub
Dim cmd As SqlCommand = NEW SqlCommand(ObjDataAdaptor,
ASKER
Its now telling me that the ObjDataAdaptor cannot be converted to string
ASKER
I have treid also as your code, but using "SQL = ...." it has an erro saying "sys.data.sql.... is not accessable in this context as it is private
I have this functions inside a module
Public Function GetConnection() As SqlConnection
Dim conn As SqlConnection
Dim strConn As String = "Data Source=Server1;Initial Catalog=MyDatabaseName;Int egrated Security=True"
conn = New SqlConnection(strConn)
Try
conn.Open()
Catch ex As SqlException
Console.WriteLine("SQL Server ERROR: " & ex.Message & " in " & ex.StackTrace)
Catch ex As Exception
Console.WriteLine("ERROR opening connection: " & ex.Message)
End Try
GetConnection = conn
End Function
Public Function RetrieveData(ByVal query As String) As SqlDataReader
Dim rdr As SqlDataReader
Dim conn As SqlConnection = GetConnection()
Try
Dim cmd As SqlCommand = New SqlCommand(query, conn)
''CommandBehavior.CloseCon nection closes the underlying connection
''whenever the reader is closed
rdr = cmd.ExecuteReader(CommandB ehavior.Cl oseConnect ion)
Catch ex As SqlException
Console.WriteLine("SQL Server ERROR: " & ex.Message & " in " & ex.StackTrace)
Catch ex As Exception
Console.WriteLine("ERROR retrieving data: " & ex.Message)
End Try
RetrieveData = rdr
End Function
And I use it this way
Dim SQL as String = "SELECT * FROM Surveyors WHERE SurveyorName = '" & txtLoginName.Text & "' AND Password = '" & txtPassword.Text
Dim sqlReader as SqlDataReader = ExecuteReader(SQL)
If sqlReader.Read Then
End If
sqlReader.Close()
sqlReader = Nothing
Public Function GetConnection() As SqlConnection
Dim conn As SqlConnection
Dim strConn As String = "Data Source=Server1;Initial Catalog=MyDatabaseName;Int
conn = New SqlConnection(strConn)
Try
conn.Open()
Catch ex As SqlException
Console.WriteLine("SQL Server ERROR: " & ex.Message & " in " & ex.StackTrace)
Catch ex As Exception
Console.WriteLine("ERROR opening connection: " & ex.Message)
End Try
GetConnection = conn
End Function
Public Function RetrieveData(ByVal query As String) As SqlDataReader
Dim rdr As SqlDataReader
Dim conn As SqlConnection = GetConnection()
Try
Dim cmd As SqlCommand = New SqlCommand(query, conn)
''CommandBehavior.CloseCon
''whenever the reader is closed
rdr = cmd.ExecuteReader(CommandB
Catch ex As SqlException
Console.WriteLine("SQL Server ERROR: " & ex.Message & " in " & ex.StackTrace)
Catch ex As Exception
Console.WriteLine("ERROR retrieving data: " & ex.Message)
End Try
RetrieveData = rdr
End Function
And I use it this way
Dim SQL as String = "SELECT * FROM Surveyors WHERE SurveyorName = '" & txtLoginName.Text & "' AND Password = '" & txtPassword.Text
Dim sqlReader as SqlDataReader = ExecuteReader(SQL)
If sqlReader.Read Then
End If
sqlReader.Close()
sqlReader = Nothing
ASKER
Ok thats cool, I see the advantages of having them in a module,
I have entered the above functions into a module and called it functions.vb
I have entered the last bit of code into my apps login form.
I have put this under the OK button, is this correct as it is saying that ExecuteReader is not declared?
I have entered the above functions into a module and called it functions.vb
I have entered the last bit of code into my apps login form.
I have put this under the OK button, is this correct as it is saying that ExecuteReader is not declared?
ASKER
This is the complete code for my login form,
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class Login
Inherits Form
'Dim objConnection As SqlConnection = New _
' SqlConnection("data source=sqlserver;Initial Catalog=Test;user id=sa;password=")
#Region " Windows Form Designer generated code "
#End Region
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
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 = ExecuteReader(SQL)
If sqlReader.Read Then
End If
sqlReader.Close()
sqlReader = Nothing
End Sub
End Class
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Public Class Login
Inherits Form
'Dim objConnection As SqlConnection = New _
' SqlConnection("data source=sqlserver;Initial Catalog=Test;user id=sa;password=")
#Region " Windows Form Designer generated code "
#End Region
Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
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 = ExecuteReader(SQL)
If sqlReader.Read Then
End If
sqlReader.Close()
sqlReader = Nothing
End Sub
End Class
Sorry, is not ExecuteReader but RetrieveData.
ASKER
This is all put in the button event.
The erro it comes up with is "Object Reference not set to instance of an object"
The erro it comes up with is "Object Reference not set to instance of an object"
Show me your code and where it breaks
ASKER
It breaks on the "If sqlreader.read then"
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
MsgBox("OK")
Else
MsgBox("Not OK")
End If
sqlReader.Close()
sqlReader = Nothing
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
MsgBox("OK")
Else
MsgBox("Not OK")
End If
sqlReader.Close()
sqlReader = Nothing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cheers for all your help, that works ok.
Youve been more than helpful so I win end this question here.
I will however be opening up another question, as I need to assigned the SurveyorId to a globalVariable (globalSurveyorId)
Thanks again for all your help jpaulino
Youve been more than helpful so I win end this question here.
I will however be opening up another question, as I need to assigned the SurveyorId to a globalVariable (globalSurveyorId)
Thanks again for all your help jpaulino
ASKER
Dim objConnection As SqlConnection = New _
SqlConnection("data source=sqlserver;Initial Catalog=Test;user id=sa;password=")
Dim ObjDataAdaptor As SqlDataAdapter = New SqlDataAdapter("Select SurveyorName, Password from Surveyors", objConnection)
Dim objDataSet As DataSet
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
If txtLoginName.Text & txtPassword.Text <> "" Then
If txtLoginName.Text = ("SurveyorName") & txtPassword.Text = ("Password") Then
Dim Main As New FormMain()
Main.Show()
Else
MsgBox("login Failed")
End If
End If
End Sub
Any ideas?