我正在一個拼寫蜜蜂程序,它包含一個登錄系統,並花了數週試圖解決這個特定的錯誤,我在這裏是我的錯誤: 創建表單時發生錯誤。有關詳細信息,請參閱Exception.InnerException。錯誤是:The type initializer for 'Spelling_Bee_Programme.test' threw an exception
。創建表單時發生錯誤。有關詳細信息,請參閱Exception.InnerException。在視覺基礎2010
下面是登錄系統我登錄代碼:
Imports System.Data.OleDb 'imports system O.L.E.D.B(Object Linking and Embedding database) functions
Public Class formlogin ' startes class formlogin for the whole form
Public Shared db_connection As New OleDbConnection 'declares db_connection as global variable and sets as new system O.L.E.D.B(Object Linking and Embedding database) connection
Public Sub cmdlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdlogin.Click ' when cmdlogin button pressed do following
Dim time_data As String 'declares time_data as datatype string
Dim file As String = "E:\login.txt" ' creates varible "file" and sets value as directory location
txtuser.Text = txtuser.Text.ToLower() 'This ensures that the username is converted to lowercase so database doesn't have a problem with random capitals being in a username
txtpass.Text = txtpass.Text.ToLower() 'This ensures that the password is converted to lowercase so database doesn't have a problem with random capitals being in a password
txtuser.Text = Trim(txtuser.Text) ' removes spaces at start and end of text in txtuser's text property
txtpass.Text = Trim(txtpass.Text) ' removes spaces at start and end of text in txt
formlogin.db_connection.ConnectionString = "Provider=Microsoft.jet.OLEDB.4.0; Data Source= H:\Spelling Bee Programme\Spelling Bee Programme\DBmainframe.mdb"
If txtpass.Text = "" Then ' clears textbox of passwordOr txtuser.text = "" ' clears textbox of usernameThen 'If username or password textbox is empty then do following
MsgBox("please enter a username or password") 'make a messagebox pop up and tell user that they have not entered anything in the textbox.
db_connection.Close() 'closes database because the database being open just incase it is accidently left open to ensure no problems. 'closes database because the database being open just incase it is accidently left open to ensure no problems.
ElseIf txtuser.Text = "root" And txtpass.Text = "password" Then 'if first condition is not meet check if username is equal to root and password is equal to password then do following which is not relient on database so that if problem with database. It is still possible to acess program as backup solution.
MsgBox("welcome root") ' makes a message box pop up and tell the user a welcome message saying the account they are logged in as.
Dim objwriter1 As New System.IO.StreamWriter(file, True) ' declares a the varible objwriter as system and pulls value of varible file which is directory of where to write data and true is write to file saying yes
time_data = DateTime.Now.ToLongDateString ' varible time_data's value equal to current time and date
DateTime.Now.ToString("hh:mm dddd, dd MMMM yyyy") ' this line formates the current time and date
objwriter1.WriteLine("root login @ " & time_data) 'appends that a recovery user logged in and time of login to file
objwriter1.Close() ' closes file writing
result.Show() ' show result from test form.
'closes database because the database being open just incase it is accidently left open to ensure no problems.
Else 'executes if condition not meet.
'Try ' this attempts the following
Dim SQLteacher As String = "SELECT * FROM teacher WHERE username = '" & txtuser.Text & "' AND teacher_password = '" & txtpass.Text & "'" ' This line states select everything from table(teacher) and where username is = what is in textbox username and password table is = to textbox password then compare for match but doesn't execute.
Dim sqlcomteacher As New System.Data.OleDb.OleDbCommand(SQLteacher, db_connection) 'setups connection settings for database
Dim SQLpupil As String = "SELECT * FROM pupil WHERE username = '" & txtuser.Text & "' AND pupil_password = '" & txtpass.Text & "'" ' This line states select everything from table(teacher) and where username is = what is in textbox username and password table is = to textbox password then compare for match but doesn't execute.
Dim sqlcompupil As New System.Data.OleDb.OleDbCommand(SQLpupil, db_connection) 'setups connection settings for database
db_connection.Open() ' opens database
Dim sqlreadteacher As System.Data.OleDb.OleDbDataReader = sqlcomteacher.ExecuteReader() 'executes database query
Dim sqlreadpupil As System.Data.OleDb.OleDbDataReader = sqlcompupil.ExecuteReader() 'executes database query
If sqlreadpupil.Read() Then 'if match for a teacher account sucessful then do following
MsgBox("Login Sucessful") 'make messagebox appear and say "Login SucessFul"
Dim objwriter As New System.IO.StreamWriter(file, True) 'declares the name of the file writer and uses variable file for location of writting of data and true to allow writting
time_data = DateTime.Now.ToLongDateString 'sets value of varible time_data = current time set on the computer
DateTime.Now.ToString("hh:mm dddd, dd MMMM yyyy") 'structures time and data by Hours, Minutes, Days, months and Year
objwriter.WriteLine("Teacher login @ " & time_data) 'writes on current line on file being created a "pupil login @ and then inputs time and data from variable time_data
objwriter.Close() ' closes file writing operation
My.Forms.test.Visible = True
Me.Hide() 'closes current form "Formlogin"
db_connection.Close() 'closes database because the database being open just incase it is accidently left open to ensure no problems.
ElseIf sqlreadteacher.Read() Then 'if match for a student account sucessful then do following.
MsgBox("Login Sucessful") 'make messagebox appear and say "Login SucessFul"
Dim objwriter As New System.IO.StreamWriter(file, True) 'declares the name of the file writer and uses variable file for location of writting of data and true to allow writting
time_data = DateTime.Now.ToLongDateString 'sets value of varible time_data = current time set on the computer
DateTime.Now.ToString("hh:mm dddd, dd MMMM yyyy") 'structures time and data by Hours, Minutes, Days, months and Year
objwriter.WriteLine("pupil login @ " & time_data) 'writes on current line on file being created a "pupil login @ and then inputs time and data from variable time_data
objwriter.Close() ' closes file writing operation
result.Show() 'opens form called "result"
Me.Hide() 'closes current form "Formlogin"
db_connection.Close() 'closes database because the database being open just incase it is accidently left open to ensure no problems.
Else : db_connection.Close() : MsgBox("Your username or password was incorrect.") 'closes database because the database being open just incase it is accidently left open to ensure no problems.
End If ' ends if statement
'Catch ex As Exception ' if error do following
'MessageBox.Show("Failed to connect to Database.. System Error Message: " & ex.Message, "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 'makes messagebox popup and say "Failed to connect to database... System error message:" shows error message and code for user to give back to us.
db_connection.Close() 'closes database because the database being open just incase it is accidently left open to ensure no problems.
'End Try 'ends 'try statement
End If ' ends if statement
End Sub ' ends subprocedure.
Private Sub cmdcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcancel.Click 'when cancel button is clicked do following in subprocedure
db_connection.Close()
End 'ends whole program
End Sub 'ends subprocedure
End Class 'ends whole form procedure
很想上如果可能的話如何解決這個錯誤的任何建議。
例外告訴你下一步:看的InnerException瞭解詳細信息。那麼這個InnerException顯示了什麼? –
這是一些密集的代碼,你有沒有考慮將它重構爲更少的,命名方法? – majjam
這段代碼適合我。你能向我們展示測試嗎? – majjam