Autologin into mysql db (1 Viewer)

ionutp23

Registered User.
Local time
Today, 05:39
Joined
Dec 23, 2019
Messages
30
Hi i have a problem with this code i use it on form_load works ok if internet connection but if no internet connection my program crash can you please help me fix it?

sign_in.TextBoxPassword.UseSystemPasswordChar = True


sign_in.TextBoxUsername.Text = My.Settings.username
sign_in.TextBoxPassword.Text = My.Settings.password
sign_in.CheckBox1.Checked = My.Settings.checkbox1

' before the user can login we need to check if the textboxes are empty
' if they contains the default values( username & password )
' check if this user exist in the database

Dim conn As New MY_CONNECTION()
Dim adapter As New MySqlDataAdapter()
Dim table As New DataTable()
Dim command As New MySqlCommand("SELECT `username`, `password` FROM `tbl_users` WHERE `username` = @usn AND `password` = @pass", conn.getConnection())

command.Parameters.Add("@usn", MySqlDbType.VarChar).Value = sign_in.TextBoxUsername.Text
command.Parameters.Add("@pass", MySqlDbType.VarChar).Value = sign_in.TextBoxPassword.Text

If sign_in.TextBoxUsername.Text.Trim() = "" Or sign_in.TextBoxUsername.Text.Trim().ToLower() = "username" Then


ElseIf sign_in.TextBoxPassword.Text.Trim() = "" Or sign_in.TextBoxPassword.Text.Trim().ToLower() = "password" Then


Else

adapter.SelectCommand = command
adapter.Fill(table)

If table.Rows.Count > 0 Then

LoginToolStripMenuItem.Enabled = False
LoginToolStripMenuItem.Visible = False
LogOutToolStripMenuItem.Visible = True
LogOutToolStripMenuItem.Enabled = True
TVToolStripMenuItem.Visible = True

Else

MessageBox.Show("This Username Or/And Password Doesn't Exists", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error)


End If

End If
 

vba_php

Forum Troll
Local time
Today, 07:39
Joined
Oct 6, 2019
Messages
2,880
Hi i have a problem with this code i use it on form_load works ok if internet connection but if no internet connection my program crash
don't you need an internet connection to connect to a mysql db anyway? there's no other way to communicate with a server if there's no web connection.
 

ionutp23

Registered User.
Local time
Today, 05:39
Joined
Dec 23, 2019
Messages
30
don't you need an internet connection to connect to a mysql db anyway? there's no other way to communicate with a server if there's no web connection.

That is ok but there is missing something from the code that instead to show a error about the connection and open my program with out login it crash my program. Instead. So i would like for example to show form2 if no internet connection. Thank you.
 

ionutp23

Registered User.
Local time
Today, 05:39
Joined
Dec 23, 2019
Messages
30
don't you need an internet connection to connect to a mysql db anyway? there's no other way to communicate with a server if there's no web connection.

What i need is to have a check if internet connection=ok then
Run my above code/command

Else

Do nothing

End if

But i have no idea how to do it.
 

vba_php

Forum Troll
Local time
Today, 07:39
Joined
Oct 6, 2019
Messages
2,880
I don't have much experience with this sort of thing, but certainly you can check that with a VBA error handling routine can't you? like....
Code:
on error goto Err_handle 'this goes at the top of the code

Dim conn As New MY_CONNECTION()
         Dim adapter As New MySqlDataAdapter()
        Dim table As New DataTable()
         Dim command As New MySqlCommand("SELECT `username`, `password`  FROM `tbl_users` WHERE `username` = @usn AND `password` = @pass",  conn.getConnection())

.....rest of your code here

'.......this goes at the end of your routine
Err_Handle:
msgbox "An error occurred.  Here is the description:" & vbcrlf & vbcrlf & _
         err.description & vbcrlf & vbcrlf & "Form 2 will now open for you"
docmd.openform "form 2"
 

ionutp23

Registered User.
Local time
Today, 05:39
Joined
Dec 23, 2019
Messages
30
I don't have much experience with this sort of thing, but certainly you can check that with a VBA error handling routine can't you? like....
Code:
on error goto Err_handle 'this goes at the top of the code

Dim conn As New MY_CONNECTION()
         Dim adapter As New MySqlDataAdapter()
        Dim table As New DataTable()
         Dim command As New MySqlCommand("SELECT `username`, `password`  FROM `tbl_users` WHERE `username` = @usn AND `password` = @pass",  conn.getConnection())

.....rest of your code here

'.......this goes at the end of your routine
Err_Handle:
msgbox "An error occurred.  Here is the description:" & vbcrlf & vbcrlf & _
         err.description & vbcrlf & vbcrlf & "Form 2 will now open for you"
docmd.openform "form 2"

Thank you works at least now. :D
 

moke123

AWF VIP
Local time
Today, 08:39
Joined
Jan 11, 2013
Messages
3,912
This procedure should tell you if you have an internet connection. Test sub included.

Code:
'paste this code in at the top of your module (it will not work elsewhere)
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef dwflags As Long, ByVal dwReserved As Long) As Long

Private Const INTERNET_CONNECTION_MODEM As Long = &H1
Private Const INTERNET_CONNECTION_LAN As Long = &H2
Private Const INTERNET_CONNECTION_PROXY As Long = &H4
Private Const INTERNET_CONNECTION_OFFLINE As Long = &H20

'paste this code in anywhere
Function IsInternetConnected() As Boolean
    Dim L As Long
    Dim R As Long
    R = InternetGetConnectedState(L, 0&)
    If R = 0 Then
        IsInternetConnected = False
    Else
        If R <= 4 Then IsInternetConnected = True Else IsInternetConnected = False

    End If
End Function

'your main function/calling function would look something like this
Private Sub btnInternetFunction_Click()
    If IsInternetConnected() = True Then
        MsgBox ("You are connected to the Internet")
        'code to execute Internet-required function here
    Else
        MsgBox ("You are not connected to the Internet or there is an issue with your Internet connection.")
    End If
End Sub
 

ionutp23

Registered User.
Local time
Today, 05:39
Joined
Dec 23, 2019
Messages
30
This procedure should tell you if you have an internet connection. Test sub included.

Code:
'paste this code in at the top of your module (it will not work elsewhere)
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef dwflags As Long, ByVal dwReserved As Long) As Long

Private Const INTERNET_CONNECTION_MODEM As Long = &H1
Private Const INTERNET_CONNECTION_LAN As Long = &H2
Private Const INTERNET_CONNECTION_PROXY As Long = &H4
Private Const INTERNET_CONNECTION_OFFLINE As Long = &H20

'paste this code in anywhere
Function IsInternetConnected() As Boolean
    Dim L As Long
    Dim R As Long
    R = InternetGetConnectedState(L, 0&)
    If R = 0 Then
        IsInternetConnected = False
    Else
        If R <= 4 Then IsInternetConnected = True Else IsInternetConnected = False

    End If
End Function

'your main function/calling function would look something like this
Private Sub btnInternetFunction_Click()
    If IsInternetConnected() = True Then
        MsgBox ("You are connected to the Internet")
        'code to execute Internet-required function here
    Else
        MsgBox ("You are not connected to the Internet or there is an issue with your Internet connection.")
    End If
End Sub

Thank you.
 

ionutp23

Registered User.
Local time
Today, 05:39
Joined
Dec 23, 2019
Messages
30
This procedure should tell you if you have an internet connection. Test sub included.

Code:
'paste this code in at the top of your module (it will not work elsewhere)
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef dwflags As Long, ByVal dwReserved As Long) As Long

Private Const INTERNET_CONNECTION_MODEM As Long = &H1
Private Const INTERNET_CONNECTION_LAN As Long = &H2
Private Const INTERNET_CONNECTION_PROXY As Long = &H4
Private Const INTERNET_CONNECTION_OFFLINE As Long = &H20

'paste this code in anywhere
Function IsInternetConnected() As Boolean
    Dim L As Long
    Dim R As Long
    R = InternetGetConnectedState(L, 0&)
    If R = 0 Then
        IsInternetConnected = False
    Else
        If R <= 4 Then IsInternetConnected = True Else IsInternetConnected = False

    End If
End Function

'your main function/calling function would look something like this
Private Sub btnInternetFunction_Click()
    If IsInternetConnected() = True Then
        MsgBox ("You are connected to the Internet")
        'code to execute Internet-required function here
    Else
        MsgBox ("You are not connected to the Internet or there is an issue with your Internet connection.")
    End If
End Sub
i got this error

PInvokeStackImbalance was detected
Message: A call to PInvoke function 'RoTV LIVE!RoTV_LIVE.Main::InternetGetConnectedState' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
 

Attachments

  • upload.png
    upload.png
    40.5 KB · Views: 192

Users who are viewing this thread

Top Bottom