0

hi there i am performing a unit test on my fully operational SMS application.

i have a module with these set of codes

Imports MySql.Data.MySqlClient
Module mdlConnect
Public cn As New MySqlConnection
Public mycom As New MySqlCommand
Public myr As MySqlDataReader
Public fail As String

Function sqlconnect(ByVal strS As String, ByVal strU As String, ByVal strP As String) As Boolean
    Try
        cn.ConnectionString = "SERVER=" & strS & "; USER ID=" & strU & ";Password=" & strP & ""
        cn.Open()
        Return True
    Catch ex As Exception
        fail = ex.Message
        Return False
    End Try
End Function

Function command(ByVal strQ As String) As Boolean
    Try
        mycom.Connection = cn
        mycom.CommandText = strQ
        mycom.ExecuteNonQuery()
        Return True
    Catch ex As Exception
        fail = ex.Message
        Return False
    End Try
End Function
End Module 

And a database connection window with these values

Public Class frmDatabaseConnection
'If Statement for Database Connection
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    Try
        If sqlconnect(txtServer.Text, txtUsername.Text, txtPassword.Text) = True Then
            Dim query(8) As String

            query(0) = "Create database if not exists dbsms;"
            query(1) = "Use dbsms;"
            query(2) = "Create table if not exists tbl_administrator(SecretCode varchar(10) not null default '', Username varchar(20), Password varchar(20),Primary Key(SecretCode));"
            query(3) = "Create table if not exists tbl_students(Idno varchar(10) not null default'',Name varchar(60), Course varchar(10), YearSec varchar(20), Organization varchar(20), MobileNum varchar(15), Primary Key(Idno));"
            query(4) = "Create table if not exists tbl_user(Username varchar(20) not null default '', Password varchar(20), Name varchar(20), Position varchar(20), MobileNum varchar(15), Primary key(Username));"
            query(5) = "Create table if not exists tbl_announcement(ID int(10) not null auto_increment, Recipients varchar(40), Description varchar(500), CDate varchar(100), Sender varchar(30), AnnouncementStatus varchar(20), Primary key(ID));"
            query(6) = "Create table if not exists tbl_activity(ID int(10) not null auto_increment, Username varchar(20), Activity varchar(30),CDate varchar(50), Primary key(ID));"
            query(7) = "Create table if not exists tblgetname(ID int(10) not null auto_increment, UserName varchar(20), Primary key(ID));"
            query(8) = "Create table if not exists tbl_inbox(ID int(10) not null auto_increment, message varchar(300), received_date varchar(50), sender varchar(30), Primary key(ID));"

            'For statement for database and tables
            For i As Integer = 0 To 8
                'If statement for executing all query
                If command(query(i).ToString) Then
                    'If statement for Assuring all query are executed
                    If i >= 8 Then
                        MessageBox.Show("Database Connected", "Database Connection", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                    'End of If statement for Assuring all query are executed
                Else
                    MessageBox.Show(fail)

                End If
                'End of If statement for executing all query
            Next
            'End of For statement for database and tables

            'Function which contains Sql Command for Selecting Administrators SecretCode
            SelectSecretCode()

            'If Statement for assuring SecretCode contains value
            If myr.HasRows Then
                'If SecretCode contains value Log In form will be shown
                Login.Show()
                Me.Hide()
            Else
                'If SecretCode contains no value Create Admin form will be shown to create Admin Account
                CreateAdmin.Show()
                Me.Hide()
            End If
            'End of If Statement for assuring SecretCode contains value
        Else
            MessageBox.Show(fail, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            clear()
        End If
        'End of If Statement for database connection
        myr.Close()
    Catch ex As Exception
        MsgBox("Invalid use of special characters and symbols")
    End Try

End Sub

Public Sub clear()
    txtServer.Clear()
    txtUsername.Clear()
    txtPassword.Clear()
End Sub

Public Sub SelectSecretCode()
    mycom.Connection = cn
    mycom.CommandText = "Select SecretCode from tbl_administrator"
    myr = mycom.ExecuteReader
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    If MessageBox.Show("Exit System", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
        Me.Close()
        Application.Exit()
    End If
End Sub

Private Sub frmDatabaseConnection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class

all these code go perfectly smooth when i run my system. i can establish connection to the database. i can save and retrieve data on the process further than this forms. but as i start to perform unit test in my system i came to an error which states as the image below.

test run error

it keeps on appearing on this line of code

    Public Sub SelectSecretCode()
    mycom.Connection = cn
    mycom.CommandText = "Select SecretCode from tbl_administrator"
    myr = mycom.ExecuteReader <-------error
    End Sub

i also add this second image for further information

test error 2

any solutions or help is very appriciated because we are running out of time. thanks

Ako Ci Divine
  • 69
  • 3
  • 13
  • MySQL connections are not just username/password based it also considers IP. Check if you are using same IP for both connections. another option (not recommended in production): grant permission from all IPs or more secure from the IP `192.168.8.102` to the username you are using – bansi Feb 12 '16 at 03:59
  • @bansi i tried this code 'select host from information_schema.processlist WHERE ID=connection_id();' but it cant find processlist. any suggestions on how can i find this ip connection. by the way im not accessing the system trough a remote connection. my connection details are just **localhost** **root** and **admin**. – Ako Ci Divine Feb 13 '16 at 01:12
  • @bansi why should i have an ip address? my system is running on my own computer? how is the ip address connected with that? – Ako Ci Divine Feb 13 '16 at 02:03
  • from the error message you posted it is trying to connect to the server `from` IP `192.168.8.102` I am not sure why. May be unit test is using its own virtual machine to connect, which is using the `192.168.8.102` IP instead of using localhost (127.0.0.1). – bansi Feb 13 '16 at 04:07
  • that 192.168.8.102 is the ip my machine is using to connect to a wifi network, i cant manually changed the ip to 127.0.0.1 because it wont accept it if i changed it – Ako Ci Divine Feb 13 '16 at 04:10
  • don't change your machine IP. change the authentication of the MySQL server [Access Control, Stage 1: Connection Verification](http://dev.mysql.com/doc/refman/5.7/en/connection-access.html) also check [MySQL root access from all hosts](http://stackoverflow.com/questions/11223235/mysql-root-access-from-all-hosts). Note: root access from all host is never recommended on production machine. Also note: I am suggesting this because I don't know how Unit test works for connection and how to change it. – bansi Feb 13 '16 at 17:28

0 Answers0