4 Replies - 8507 Views - Last Post: 26 May 2010 - 06:52 PM Rate Topic: -----

#1 acid15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-July 09

while loop / if else

Posted 25 May 2010 - 08:48 PM

hi, the problem is only the second "while statement" works... the 1st "while statement" seems not working properly, it just stop on the log in form and do not open the student_form. any idea is a great help..

it doesn't give me any error..

  If dr.HasRows = False Then
            If dr2.HasRows = False Then
                MsgBox("Please enter a correct Username and Password")
            Else
                While (dr.Read())
                    If (TextBox1.Text = dr(0).ToString()) And (TextBox2.Text = dr(1).ToString()) Then
                        student_form.Show()
                    End If
                End While

                While (dr2.Read())
                    If (TextBox1.Text = dr2(0).ToString()) And (TextBox2.Text = dr2(1).ToString()) Then
                        admin_form.Show()
                    End If
                End While
            End If
        End If

This post has been edited by acid15: 26 May 2010 - 12:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: while loop / if else

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: while loop / if else

Posted 26 May 2010 - 02:02 AM

Hi,

Are you sure it only goes to the second while loop? it looks like it will step int the first and it will do the work and then it goes to the second...

also why do you use student_form.Show() ?

Should you not dim the form and use ShowDialog()?

Dim StudentForm As New student_form()
StudentForm.ShowDialog()



the show dialog will wait for the user to close the StudentForm.
Was This Post Helpful? 0
  • +
  • -

#3 Charles:)  Icon User is offline

  • D.I.C Regular

Reputation: 148
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: while loop / if else

Posted 26 May 2010 - 04:33 AM

While (dr.Read())
    If (TextBox1.Text = dr(0).ToString()) And (TextBox2.Text = dr(1).ToString()) Then
        student_form.Show()
    End If
End While



If your student_form isn't showing then it must be for one of the following reasons:

1: dr.Read() never evaluates to True so the while loop never starts.
2: the If clause never evaluates to True so the form is never shown.

You can place breakpoints in your code to find out which one of those reasons is stopping your form from being shown. Then you'll be able to fix it :)
Was This Post Helpful? 0
  • +
  • -

#4 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: while loop / if else

Posted 26 May 2010 - 04:49 AM

also,

you start with

If dr.HasRows = False Then



so obviously there are no rows in "dr"

so

While (dr.Read())



will not happen as there are no rows.
Was This Post Helpful? 0
  • +
  • -

#5 acid15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-July 09

Re: while loop / if else

Posted 26 May 2010 - 06:52 PM

thnx for the ideas guys. it really helps..

i figure it out. i only forgot to close each "dr" statement...
here's the whole code:

Imports System.Data.OleDb
Imports System.Data
Public Class login
    Dim con As OleDbConnection, cmd As OleDbCommand

    Dim str1 As String
    Dim str2 As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        con = New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & Application.StartupPath & "\database1.mdb;Persist security Info=False")
        
        
        str1 = "select [StudentNumber],[StudentPassword] from [Members] WHERE [StudentNumber] like '" & Trim(TextBox1.Text) & "' and [StudentPassword] like '" & Trim(TextBox2.Text) & "';"
        str2 = "select [AUserName],[APassword] from [Members2] WHERE [AUserName] like '" & Trim(TextBox1.Text) & "' and [APassword] like '" & Trim(TextBox2.Text) & "';"
        cmd = New OleDbCommand(str1, con)
        cmd2 = New OleDbCommand(str2, con)
        Dim dr As OleDbDataReader
        Dim dr2 As OleDbDataReader
        dr = cmd.ExecuteReader
        dr2 = cmd2.ExecuteReader

        If dr.HasRows = False Then
            If dr2.HasRows = False then
                MsgBox("Please enter a correct username or password")
            End If
        Else
            While (dr.Read())
                If (TextBox1.Text = dr(0).ToString()) And (TextBox2.Text = dr(1).ToString()) Then
                    student_form.Show()
                    Me.Hide()
                    TextBox1.Text = ""
                    TextBox2.Text = ""
                End If
            End While
            dr.Close()
        End If
        While (dr2.Read())
            If (TextBox1.Text = dr2(0).ToString()) And (TextBox2.Text = dr2(1).ToString()) Then
                admin_form.Show()
                Me.Hide()
                TextBox1.Text = ""
                TextBox2.Text = ""
            End If
        End While
        dr.Close()

        cmd = Nothing
        cmd2 = Nothing
        con.Close()
        
    End Sub

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

    End Sub
End Class

TY guys...

This post has been edited by acid15: 26 May 2010 - 07:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1