Results 1 to 11 of 11

Thread: [RESOLVED] End running class.

  1. #1

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Resolved [RESOLVED] End running class.

    Hi all,

    I've got a class called Startupforms as my startup project with the following code.

    VB Code:
    1. Public Class StartupForms
    2.     Shared Sub Main()
    3.         Dim SplashForm As New FrmSplash 'this is your first form
    4.         Dim LoginForm As New FrmLogin 'this is your second form
    5.         Dim MainForm As New FrmMAIN
    6.         SplashForm.ShowDialog() 'open first form
    7.         LoginForm.ShowDialog() 'open second form
    8.         MainForm.ShowDialog()
    9.     End Sub
    10.  
    11. End Class

    Now on the second form which opens, there's a command button with code

    VB Code:
    1. Application.Exit()

    if the user desires to exit the application. (This is a login form)

    When the user clicks this button, it still opens the application, and flashes on the screen and returns errors and crap cause the user didn't logon and the connection wasnt made. Actually it's behaving worse than a baby.

    How can I when the user clicks on the exit button on the second form, halt the class, and exit the application without executing the rest of the class?

    Thanks

    Rudi

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: End running class.

    You should provide an OK and a Cancel button on your login form. If the user presses OK then you should validate their credentials. If the login fails, inform the user and refresh the login form. You should make the Click event handler for the OK button look something like this:
    VB Code:
    1. Private Sub OK_Click(...) Handles okButton.Click
    2.     If Me.ValidateLogin() Then
    3.         Me.DialogResult = DialogResult.OK
    4.     Else
    5.         MessageBox.Show("Login failed. Please try again.")
    6.     End If
    7. End Sub
    where ValidateLogin is function that you write yourself that returns a Boolean indicating whether the login was successful. You would set the AcceptButton property of the form to the OK button and the CancelButton property to the Cancel button. You would also set the DialogResult property of the Cancel button to Cancel, although assigning the CancelButton property of the form should do this automatically. Your Main method would then look something like this:
    VB Code:
    1. Shared Sub Main()
    2.         Dim SplashForm As New FrmSplash 'this is your first form
    3.         Dim LoginForm As New FrmLogin 'this is your second form
    4.  
    5.         SplashForm.ShowDialog() 'open first form
    6.  
    7.         If LoginForm.ShowDialog() = DialogResult.OK Then
    8.             Application.Run(New FrmMain)
    9.         End If
    10.     End Sub
    Also, I suggest you don't create a special class for this Main method. Either put it in a module or make it a Shared member of your main form. Then you simply set the module or form to be the startup object and the Main method will be used by default.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Question Re: End running class.

    Hi JMCilhinney,

    Thanks, I'm trying what you told me now. I assigned the the ok and cancel button properties to my "CmdLogin" and "CmdExit" buttons. When the user clicks exit, it now exits. Thanks, but when the user clicks Login... It still exits. hehe....

    I think it's doing that cause I don't have the validate login stuff yet. Could you help me with that?

    What I want to do is: Create a public connection string, that can be used anywhere in the application, while the application is open.

    In my Login form, I've got a group box, with 2 radio buttons, "RdoNTAuth" & "RdoSQLAuth"

    When the user clicks on RdoSQLAuth, then it shows text buttons and the user enters username and password.

    So what I need is, a connection string needs to be created. or sql connection needs to be created publically. Regardless of the user using nt authentication or sql authentication. It at the end, creates the string for the login connection. (I think I explained that more difficult than it should sound)

    What I would like is: I've got an module which name is: "JMSGlobals.vb"

    With current code:

    VB Code:
    1. Module JMSGlobals
    2.     Public JMSColor = Color.FromArgb(149, 178, 198)
    3.     Public JMSConnection As New SqlClient.SqlConnection
    4. End Module

    Would it be possible to make the connection public in the JMSGlobals module, so that I can just refer to that connection whenever I need a connection to sql server? I just need to set the string for the duration of the lifetime of the open application (the user logs in all the time)

    I hope I didn't make it sound too difficult, but I think you know what I mean. Can the validation be done in the module aswell?

    Thanks

    Rudi

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: End running class.

    You certainly can have a single connection for the whole app, and in fact it is a good idea. I like to actually create the connection in my first form, in your case the login form, so that I can configure it in the design window, like setting the connection string. I then have a public variable in a module, like you do but without the New keyword, which I assign my connection to in the Load event handler of the form it was added to.

    Just one other thing, I don't think it is set by default but make sure the DialogResult property of your OK button has not been set to OK. It should be None, otherwise the form will close regardless of the result of your validation.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: End running class.

    hmmm... ok I'm gonna try it and get back to you on this. Do you have any ideas on validation code. To check the login etc.

    Thanks for all the help thusfar.

    Rudi

  6. #6

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Question Re: End running class.

    JMCilHinney,

    How Can I validate the sql server login, (get the error codes from sql server like invalid login, server not found etc)

    When I do the login procedure, and it succeeds, then the application closes. It doesn't open the next form.

    This is the code in my startup class:

    VB Code:
    1. Public Class StartupForms
    2.     Shared Sub Main()
    3.         Dim SplashForm As New FrmSplash 'this is your first form
    4.         Dim LoginForm As New FrmLogin 'this is your second form
    5.         Dim MainForm As New FrmMAIN
    6.  
    7.         SplashForm.ShowDialog() 'open first form
    8.  
    9.         If LoginForm.ShowDialog() = DialogResult.OK Then
    10.             Application.Run(MainForm)
    11.             'MainForm.ShowDialog()
    12.         End If
    13.     End Sub
    14. End Class

    And this is the code on my login button:

    VB Code:
    1. Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
    2.         If (RdoSQLAuth.Checked = True) Then
    3.             JMSGlobals.JMSConnection.ConnectionString = "Server=" & Me.txtJMSServer.Text & ";Database=MSTRIBOLOGY;User ID=" & Me.txtUsername.Text & ";Password=" & Me.txtPassword.Text & ";Trusted_Connection=False;workstation id=" & Environment.MachineName & ""
    4.             Me.Close()
    5.  
    6.         ElseIf (RdoNTAuth.Checked = True) Then
    7.             JMSGlobals.JMSConnection.ConnectionString = "Data Source=" & Me.txtJMSServer.Text & ";Initial Catalog=MSTRIBOLOGY;Integrated Security=SSPI;workstation id=" & Environment.MachineName & ""
    8.             Me.Close()
    9.  
    10.         End If
    11.     End Sub

    What could be wrong?

    Thanks

    Rudi

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: End running class.

    DO NOT call Me.Close. This closes the form and certainly does not return DialogResult.OK to ShowDialog. I specifically wrote in the code I provided to set Me.DialogResult to OK. Doing that hides the form and returns the value you set to ShowDialog.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: End running class.

    eek.... Sorry about that man... I got it now.
    Any ideas on authentification validation?

    Thanks again, sorry for not listening.

    Rudi

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: End running class.

    Quote Originally Posted by Tjoppie
    Any ideas on authentification validation?
    Unfortunately not. I don't work with SQL Server much so I'll have to leave that to others I'm afraid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: End running class.

    Eish... Ok...

    I'm having some trouble on setting that global sql connection.

    Does the following connection strings look ok?

    VB Code:
    1. Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
    2.         If (RdoSQLAuth.Checked = True) Then
    3.             JMSGlobals.JMSConnection.ConnectionString = "Server=" & Me.txtJMSServer.Text & ";Database=MSTRIBOLOGY;User ID=" & Me.txtUsername.Text & ";Password=" & Me.txtPassword.Text & ";Trusted_Connection=False;workstation id=" & Environment.MachineName & ""
    4.             Me.DialogResult = DialogResult.OK
    5.             'Me.Close()
    6.  
    7.         ElseIf (RdoNTAuth.Checked = True) Then
    8.             JMSGlobals.JMSConnection.ConnectionString = "Data Source=" & Me.txtJMSServer.Text & ";Initial Catalog=MSTRIBOLOGY;Integrated Security=SSPI;workstation id=" & Environment.MachineName & ""
    9.             Me.DialogResult = DialogResult.OK
    10.             'Me.Close()
    11.  
    12.         End If
    13.     End Sub

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: End running class.

    Like I said, I haven't worked with SQL Server much, but you might like to look here if you haven't already. As to validating the logon, I've thought about it and I gues you would just try to open the connection and if it fails then check the exception, which may well differentiate between invalid credentials and other reasons.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width