Imports MySql.Data.MySqlClient
Imports System.Net
Class memberID
'This function allows the member ID to be randomly generated.
Public Function RandomNumber(ByVal MaxNumber As Integer, _
Optional ByVal MinNumber As Integer = 0) As Integer
Dim r As New Random(System.DateTime.Now.Millisecond)
If MinNumber > MaxNumber Then
Dim t As Integer = MinNumber
MinNumber = MaxNumber
MaxNumber = t
End If
Return r.Next(MinNumber, MaxNumber)
End Function
'This allows the generated memberid to be used outside of the sub it generates it in.
Dim memberidrand As Integer
'This uses the above function generate a random number between 100,000 and 999,999
Sub getid()
Dim memberidrand = RandomNumber(100000, 999999)
End Sub
'This defines variables for use for the code below.
Dim localHost As String
Dim checkRecord As String
Dim NoMore As Integer = 0
'This sub is used to get the member ID and put it in the database. There are errors here because MySQL Queries are executed incorrectly inside this sub.
Sub getmembid()
'localHost will get the current person's host name to make sure they don't already have a member ID.
localHost = Dns.GetHostName()
Console.WriteLine(checkIP("SELECT IP FROM allmemberidtable WHERE IP = " & localHost))
'This If checks to make sure that their host name is not in the database.
If checkIP = "" Then
getid()
Dim conn = New MySqlConnection()
conn.ConnectionString = "server=x.x.x.x; user id=root; password=xxx; database=xxx"
conn.Open()
'This puts their host name into the database.
Console.WriteLine(updateRecordIP("INSERT INTO allmemberidtable VALUES (IP) " & localHost))
'This query checks to make sure the randomly generated member ID isn't already in the database.
Console.WriteLine(checkRecord("SELECT memberid FROM allmemberidtable WHERE memberid = " & memberidrand))
Else
'This should only happen if the person already has a member ID. It will tell the code to stop working and gives them their current member ID.
NoMore = 1
Console.WriteLine(getMemberID("SELECT memberid FROM allmemberidtable WHERE IP = " & localHost))
MsgBox("Your member ID is: " & getMemberID)
End If
End Sub
'This is what happens when they click the main button.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'This runs the above sub to check for their host name and duplicates of the randomly generated member ID.
getmembid()
'This uses the variable set in the above sub to make sure that the member id is not duplicated.
If checkRecord = "" Then
'This puts their member ID into the database.
Console.WriteLine(updateRecord("INSERT INTO allmemberidtable VALUES (memberid) " & memberidrand))
Else
'If it is duplicated and their host name isn't in the database, it makes a new member ID.
If NoMore = 0 Then
getmembid()
End If
End If
End Sub
End Class