Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
If txtuser.Text = "" Then
MsgBox("Please Enter a Username or Password, DOnt leave it blank", MsgBoxStyle.Exclamation, "Access Error")
ElseIf txtPass.Text = "" Then
MsgBox("Please Enter a Username or Password, DOnt leave it blank", MsgBoxStyle.Exclamation, "Access Error")
Else
End If

Dim ConStr As String = "server=localhost;database=library;uid=root;password=localhost;"
Dim conn As MySqlConnection
conn = New MySqlConnection(ConStr)
Dim qry As String = "select * from accesscode "
Dim ds As New DataSet
Dim dat As MySqlDataAdapter
dat = New MySqlDataAdapter(qry, conn)
dat.Fill(ds, "accesscode")

' Open connection
conn.Open()

If ds.Tables("accesscode").Rows(0).Item("username") = txtuser.Text And _
ds.Tables("accesscode").Rows(0).Item("password").ToString = txtPass.Text Then

Me.Hide()
About.Show()


Else


MsgBox("Access Denied", MsgBoxStyle.Critical, "Security error")
End If
txtPass.PasswordChar = "*"
End Sub


this code is running but it only reads the first column in my database...the other stored account,if i entered will be"Access Denied ..."


i hope someone can help me to solved this problem ..

thanks in advance for you help..

this code is running but it only reads the first column in my database.

Do you mean first row?

You reference the second row:

If ds.Tables("accesscode").Rows(1).Item("username") = txtuser.Text And _
ds.Tables("accesscode").Rows(1).Item("password").ToString = txtPass.Text Then

and I think you're trying to lookup a matching username/password pair so you'll need a loop:

Dim RowIX As Integer
RowIX = 0
Do Until <ds.Tables("accesscode") has no more rows>
  If ds.Tables("accesscode").Rows(RowIX).Item("username") = txtuser.Text And _
  ds.Tables("accesscode").Rows(RowIX).Item("password").ToString = txtPass.Text Then
  < your code in here >
  ' Increase row index counter
  RowIX += 1
Loop

That's a bit pseudo code, I was too lazy to check the exact syntax, but I believe you got the idea.

HTH

create a new class for SQLconnection
ex:

'*********************************************
'Execute_SP_Reader() 
'Call this to execute a SELECT statement 
 '*********************************************
Public Function Execute_SP_DataReader(ByVal sp_name As String, _
             ByVal param As SqlParameter()) As SqlDataReader

    Open()
    Dim cmd As SqlCommand = New SqlCommand(sp_name, c)
    cmd.CommandType = CommandType.StoredProcedure

    'parse and add the parameters
    For Each p As SqlParameter In param
        cmd.Parameters.Add(p)
    Next
    Return cmd.ExecuteReader()

End Function

Then Call using this in your prog:

Conn.Open()
      Dim drResult As SqlDataReader
      Dim param() As SqlParameter = {New SqlParameter("@employeeName", employeeName)                     ,_ New SqlParameter("@Password", Password)}

       drResult = Conn.Execute_SP_DataReader("[sp_CheckLogIn]", param)
           'display all associated info here

           dim x as string
           dim y as string
           drResult.Read()

try

      x = drResult("username")
      y = drResult("password")

      if (x.Equals(txtuser.text) and y.Equals(txtPass.text)) then
      MsgBox("Your Connected")


drResult.Close()

Catch ex As Exception
            MsgBox("User does not exist in the Database!")
End Try

        'garbage collection
Conn.Close()

Note: Create first a Stored Procedure in SQLserver in this example i named it sp_CheckLogIn..

=)

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Do you mean first row?

You reference the second row:

If ds.Tables("accesscode").Rows(1).Item("username") = txtuser.Text And _
ds.Tables("accesscode").Rows(1).Item("password").ToString = txtPass.Text Then

and I think you're trying to lookup a matching username/password pair so you'll need a loop:

Dim RowIX As Integer
RowIX = 0
Do Until <ds.Tables("accesscode") has no more rows>
  If ds.Tables("accesscode").Rows(RowIX).Item("username") = txtuser.Text And _
  ds.Tables("accesscode").Rows(RowIX).Item("password").ToString = txtPass.Text Then
  < your code in here >
  ' Increase row index counter
  RowIX += 1
Loop

That's a bit pseudo code, I was too lazy to check the exact syntax, but I believe you got the idea.

HTH

yah right thats row.. sorry my bad ...

but tnx for your response sir...

Edited 6 Years Ago by neolyte120109: n/a

Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!

This question has already been answered. Start a new discussion instead.