[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] cn.State <> ConnectionState.Open [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]cn.Open()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]cm = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlCommand
cm.Connection = cnPodaci
cm.CommandType = CommandType.Text
cm.CommandText = "SELECT Username, Password FROM TableName WHERE " &_
"Username=" & [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtUsername.Text & " AND Password=" & [COLOR=#0000ff]Me[/COLOR][SIZE=2].txtPassword.Text[/SIZE]
dr = cm.ExecuteReader(CommandBehavior.CloseConnection)[/SIZE][SIZE=2]
dr.Read()
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] dr.HasRows [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=green]'let user enter app[/COLOR][/SIZE][SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=green]'forbid access (do nothing or notify user that login info is bad)[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]dr.Close()
[/SIZE]
Dim myCommand As New SqlCommand("SELECT COUNT(*) FROM Users WHERE UserID = @UserID AND Password = @Password", myConnection)
myCommand.Parameters.Add("@UserID", userIDValue)
myCommand.Parameters.Add("@Password", passwordValue)
myConnection.Open()
If CInt(myCommand.ExecuteScalar()) = 0 Then
MessageBox.Show("Login failed.")
Else
MessageBox.Show("Login successful.")
End If
myConnection.Close()
jmcilhinney said:Use parameters, as suggested earlier, and use ExecuteScalar instead of ExecuteReader:It is also not a good idea to store the actual password in the database. What you can do, if security is genuinely important, is hash the password when the user creates it and then store that value. When the user logs in you would then hash the password they provide using the same algorithm and compare that to the hash value stored in the database. The advantage is that the hash value can never be used to recreate the original password. The .NET Framework has in-built support for the SHA1 and MD5 algorithms.VB.NET:Dim myCommand As New SqlCommand("SELECT COUNT(*) FROM Users WHERE UserID = @UserID AND Password = @Password", myConnection) myCommand.Parameters.Add("@UserID", userIDValue) myCommand.Parameters.Add("@Password", passwordValue) myConnection.Open() If CInt(myCommand.ExecuteScalar()) = 0 Then MessageBox.Show("Login failed.") Else MessageBox.Show("Login successful.") End If myConnection.Close()