none
Check whether the database is empty

    Question

  • hi,

         Whenever I load a form ("purchase") , a "login form" came.....when i enter "username"  and "password" ,it checks whether it exists in the table (UserAccount)..........if it exists in the table then the "purchase" form shows.........now the problem is if there is no data in the table (UserAccount) , i want to show the "Purchase"  form only.......then there is no need of "login form"...........my code is as follows


    Private Function ValidateLogin(ByVal userName As String, _ByVal password As String) As Boolean
    con = New SqlConnection("Data Source=.;Initial Catalog=FA;Integrated Security=True;Pooling=False ")
    Dim sqlstring As String = ("select UserName,Password from UserAccount where UserName=@name and Password=@pwd "
    )
    Dim command As New
    SqlCommand(sqlstring, con)
    command.Parameters.AddWithValue(
    "@name"
    , txt_uname.Text)
    command.Parameters.AddWithValue(
    "@Pwd"
    , txt_Password.Text)
    Try
    con.Open()
    If Not command.ExecuteScalar Is Nothing
    Then
    MessageBox.Show("Right, Logged In"
    )
    Return
    True
    Else
    MessageBox.Show("Wrong, Stay out"
    )
    Return
    False
    End
    If
    Catch ex As
    Exception
    MessageBox.Show(ex.Message)
    Me
    .Close()
    Finally
    con.Close()
    End
    Try
    End
    Function

    Private Sub Bt_Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Login.Click
    If ValidateLogin(Me.txt_uname.Text, Me.txt_Password.Text)
    Then
    Me
    .DialogResult = Windows.Forms.DialogResult.OK
    Else
    Me
    .DialogResult = Windows.Forms.DialogResult.Cancel
    End
    If
    Me
    .Close()
    End
    Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    Me
    .DialogResult = Windows.Forms.DialogResult.Cancel
    Me
    .Close()
    End
    Sub

    on Purchase form's page load event use this code snippet

    Dim frmLogin As New LoginForm 
    If frmLogin.ShowDialog = Windows.Forms.DialogResult.Cancel Then 
        Me.Close() 
    End If 
    Sunday, February 15, 2009 12:59 PM

Answers


  • I have no idea why you are showing login after loading the purchase form but replace your purchase's forms load event with following


    Dim con as New SqlConnection("Data Source=.;Initial Catalog=FA;Integrated Security=True;Pooling=False "
            Dim sqlstring As String = "Select count(Username) from UserAccount" 
            Dim command As New SqlCommand(sqlstring, con) 
            Dim res As Integer = 0 
            Try 
                con.Open() 
                res = Integer.Parse(command.ExecuteScalar) 
            Catch ex As Exception 
                MessageBox.Show(ex.Message) 
                Me.Close() 
            Finally 
                con.Close() 
            End Try 
            If res > 0 Then 
    Dim frmLogin As New LoginForm 
    If frmLogin.ShowDialog = Windows.Forms.DialogResult.Cancel Then 
    Me.Close() 
    End If 

    Arjun Paudel
    • Marked as answer by Yichun Feng Saturday, February 21, 2009 4:51 AM
    Sunday, February 15, 2009 4:51 PM

All replies

  • I may be mistaken but I don't think you'll know if the user has a login on the system before the login is run.

    An alternative design would be to have an "Exit" option on the login form that would cause the form to exit.
    Sunday, February 15, 2009 4:12 PM

  • I have no idea why you are showing login after loading the purchase form but replace your purchase's forms load event with following


    Dim con as New SqlConnection("Data Source=.;Initial Catalog=FA;Integrated Security=True;Pooling=False "
            Dim sqlstring As String = "Select count(Username) from UserAccount" 
            Dim command As New SqlCommand(sqlstring, con) 
            Dim res As Integer = 0 
            Try 
                con.Open() 
                res = Integer.Parse(command.ExecuteScalar) 
            Catch ex As Exception 
                MessageBox.Show(ex.Message) 
                Me.Close() 
            Finally 
                con.Close() 
            End Try 
            If res > 0 Then 
    Dim frmLogin As New LoginForm 
    If frmLogin.ShowDialog = Windows.Forms.DialogResult.Cancel Then 
    Me.Close() 
    End If 

    Arjun Paudel
    • Marked as answer by Yichun Feng Saturday, February 21, 2009 4:51 AM
    Sunday, February 15, 2009 4:51 PM
  • Arjun,

    If the world were perfect, we'd have a lot less to do. I was worried about the case where the system has does not know whether a user is logged on or off or who a user is in terms of having an identity.

    But I agree there is no need to show a login form after loading a "purchase form" after logging-in.

    Renee
    Sunday, February 15, 2009 5:12 PM
  • Hi Renee!

    Thanks for the comment, yep I agree with you.
    I am not sure whether I answered the question :) its not logical. In my understanding, OP wants to check whether database table has the records of username and password or not. If table has records, show the login form, if not, do not display login, show the purchase form :)

    Anyway, have a good day

    Arjun Paudel
    Sunday, February 15, 2009 5:18 PM
  • hi Arjun,


                   thanks a lot...........its working
    Tuesday, February 17, 2009 6:19 AM