hi all ... i am designing a software where i am using login and password. the user and password is store in notepad which is connected with listview . now i dont know i mean i need something when user enter user and password it should search and if details available in listview then goto form 2 else reject

something like

if textbox1.text=listview.items.find then

form2.show

end if

please help me i need urgent answer

Recommended Answers

All 9 Replies

To search you can use ListView1.Items.ContainsKey(key As String) function.

May I suggest a safer method of storing a password?

You could create a database in something as simple as access and store the passwords in a table.

To increase the security further, you can encrypt the passwords and store the encrypted string into the database.

This method is called trap door encryption.

(Example uses Microsoft's example found here)
(Example uses Access 2010 and Visual Studio 2010)
(Example connection code uses System.Data.OleDB)

For example:

Imports System.Security.Cryptography 
Public Class Encryption
    Private TripleDes As New TripleDESCryptoServiceProvider

    Sub New(ByVal key As String)
        ' Initialize the crypto provider.
        TripleDES.Key = TruncateHash(key, TripleDES.KeySize \ 8)
        TripleDES.IV = TruncateHash("", TripleDES.BlockSize \ 8)
    End Sub

    Private Function TruncateHash( ByVal key As String, ByVal length As Integer) As Byte()
        Dim sha1 As New SHA1CryptoServiceProvider

        ' Hash the key. 
        Dim keyBytes() As Byte =
            System.Text.Encoding.Unicode.GetBytes(key)
        Dim hash() As Byte = sha1.ComputeHash(keyBytes)

        ' Truncate or pad the hash. 
        ReDim Preserve hash(length - 1)
        Return hash
    End Function

    Public Function EncryptData(ByVal plaintext As String) As String

        ' Convert the plaintext string to a byte array. 
        Dim plaintextBytes() As Byte =
            System.Text.Encoding.Unicode.GetBytes(plaintext)

        ' Create the stream. 
        Dim ms As New System.IO.MemoryStream
        ' Create the encoder to write to the stream. 
        Dim encStream As New CryptoStream(ms,
            TripleDes.CreateEncryptor(),
            System.Security.Cryptography.CryptoStreamMode.Write)

        ' Use the crypto stream to write the byte array to the stream.
        encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
        encStream.FlushFinalBlock()

        ' Convert the encrypted stream to a printable string. 
        Return Convert.ToBase64String(ms.ToArray)
    End Function

    Public Function DecryptData(ByVal encryptedtext As String) As String

        ' Convert the encrypted text string to a byte array. 
        Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)

        ' Create the stream. 
        Dim ms As New System.IO.MemoryStream
        ' Create the decoder to write to the stream. 
        Dim decStream As New CryptoStream(ms,
            TripleDes.CreateDecryptor(),
            System.Security.Cryptography.CryptoStreamMode.Write)

        ' Use the crypto stream to write the byte array to the stream.
        decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
        decStream.FlushFinalBlock()

        ' Convert the plaintext stream to a string. 
        Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
    End Function
End Class

And for the connection form:

Public Class myForm
    Private Sub btnTestIt_Click(sender As System.Object, e As System.EventArgs) Handles btnTestIt.Click
        'Encrypted value was inserted into the database beforehand.
        Dim Crypt As New Encryption("MyTestKey")
        Dim con As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                             "Data Source=C:\myFolder\UserInfo.accdb;" & _
                                             "Persist Security Info=False;")
        Dim da As New OleDb.OleDbDataAdapter("SELECT Password FROM tblUserInfo WHERE UserName=?", con)
        Dim ds As New DataSet
        Try
            da.SelectCommand.Parameters.AddWithValue("@UserName", txtUserName.Text)
            da.Fill(ds, "UserInfo")
            If Not IsNothing(ds.Tables("UserInfo")) Then
                If ds.Tables("UserInfo").Rows.Count > 0 Then
                    If ds.Tables("UserInfo").Rows(0)("Password") = Crypt.EncryptData(txtPassword.Text) Then
                        MsgBox("You are authorized!", MsgBoxStyle.Information, "Hooray!")
                    Else
                        MsgBox("You are not authorized!", MsgBoxStyle.Exclamation, "Oh no!")
                    End If
                Else
                    MsgBox("No rows were returned from table!", MsgBoxStyle.Exclamation, "Oops!")
                End If
            Else
                MsgBox("User not found in table!", MsgBoxStyle.Exclamation, "Oops!")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error!")
        End Try
    End Sub
End Class

Place this code in a test form and test it for yourself!

For my example I stored the following in the UserInfo table:
UserName:User1 Password:(EncrpytedUserPasswordString)

Please see the attached files for a better understanding.

commented: High antibiotics for a simple temperature +4

thank you for your reply but i need simple code because i am not working on database. what i am doing is that i made a login and password.when user will enter password and username and press enter, it will retrive data from listview and if user exits it will allow user to enter.

LOGIN SCRREN CONNECTED TO LISTVIEW ...something like this...

or tell me who i can search items from listview column 1 and 2 and if record found the messagebox or label must tell "record found"

thanks in advance

Shark_1 ..i tried it

    If ListView1.Items.ContainsKey(TextBox1.Text) Then

        MsgBox(" done")
    Else
        MsgBox(" not found")

    End If

but msgbox shows not found message. can you please write the code .

Followings should work.

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim lv As New ListViewItem
        For i As Integer = 65 To 90
            lv = ListView1.Items.Add(Chr(i), Chr(i), 0)
            lv.SubItems.Add(i.ToString)
        Next
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        If ListView1.Items.ContainsKey(Trim(TextBox1.Text)) = True Then
            If ListView1.Items(TextBox1.Text).SubItems(1).Text = Trim(TextBox2.Text) Then
                MessageBox.Show("Yes! I do.")
            Else
                MessageBox.Show("No! I don't.")
            End If
        Else
            MessageBox.Show("No! I don't.")
        End If
    End Sub


End Class

Here Button1 to populate listview and button2 to search data on the value of Textbox1 and TextBox2. Assume the text of TextBOx1 as user and TextBox2 as password.

   Dim boolFound As Boolean = False
        Dim str As String = TextBox1.Text
        Dim str1 As String = TextBox2.Text
        For Each LvItem As ListViewItem In Me.ListView1.Items

            If LvItem.SubItems(1).Text.Contains(str1) Then
                boolFound = True

                Label3.Text = "found"

                Label2.Text = "Found"

                selection.Show()

                Exit For

            End If
        Next

        If Not boolFound Then
            MsgBox("Wrong User\Password", vbOKOnly + vbCritical, "Wrong User\Pass")

        End If

i write above code which is working for me but it will search from one textbox. i have 2 textbox. 1st is for user and 2nd is for password. first is working fine but how i can add 2nd textbox i above code so it search itemsub 1 and itemsub 2

Shark_1 thanks alot for your last code it worked but it only searching the numbers not strings. my user and passwords depends on strings like
username = "admin"
password = "admin"

but it not searching it. if i seach 1 2 3 .... then it work or only for a b c d it working but not for complete word

You can do it like

Imports System
Imports System.Globalization

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim lv As New ListViewItem
        For i As Integer = 65 To 90
            lv = ListView1.Items.Add(Chr(i) & Chr(i + 32) & Chr(i), Chr(i) & Chr(i + 32) & Chr(i), 0)
            lv.SubItems.Add(Chr(i + 32) & Chr(i + 32) & Chr(i + 32))
        Next
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

        Dim MatchResult As Boolean = False
        For Each lv As ListViewItem In ListView1.Items

            If String.Compare(lv.Text, TextBox1.Text, False, CultureInfo.CurrentCulture) = 0 Then
                If String.Compare(lv.SubItems(1).Text.Trim, TextBox2.Text.Trim, False, CultureInfo.CurrentCulture) = 0 Then
                    MessageBox.Show("Yes! I do.")
                    ListView1.Focus()
                    'lv.Focused = True
                    lv.Selected = True
                    lv.EnsureVisible()
                    MatchResult = True
                    Exit For
                Else
                    MatchResult = False
                End If
            Else
                MatchResult = False
            End If

        Next

        If Not MatchResult Then
            MessageBox.Show("No! I don't.")
        End If

    End Sub


End Class

thankssssssssssssss it worked

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.