我正在制作一个拼写蜜蜂程序,它包含一个登录系统,并花了几周试图修复这个错误我在这里是我的错误:
创建表单时出错。有关详细信息,请参阅Exception.InnerException。错误是:The type initializer for 'Spelling_Bee_Programme.test' threw an exception
。
以下是登录系统的登录代码:
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
如果可能的话,会喜欢有关如何解决此错误的任何建议。
答案 0 :(得分:0)
测试中的代码没有完成,但看看它是什么:
导入System.Data.OleDb 公开课测试 昏暗的vbscore作为整数 Dim FILE_NAME As String =“E:\ results.txt” Dim sql As String Dim da As OleDb.OleDbDataAdapter Dim dr As OleDb.OleDbDataReader Dim ds作为新数据集 Dim cb As OleDb.OleDbCommandBuilder =没什么 Dim i As Integer 昏暗时间= Now.ToShortTimeString Public Shared Definition()As Label = {test.lbldefinition1,test.lbldefinition2,test.lbldefinition3,test.lbldefinition4,test.lbldefinition5,test.lbldefinition6,test.lbldefinition7,test.lbldefinition8,test.lbldefinition9,test.lbldefinition10} < / p>
Private Sub cmdsubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsubmit.Click
Dim da As OleDb.OleDbDataAdapter
sql = "SELECT * FROM word"
da = New OleDbDataAdapter(sql, formlogin.db_connection)
Dim ds As New DataSet
formlogin.db_connection.ConnectionString = "Provider=Microsoft.jet.OLEDB.4.0; Data Source= E:\Spelling Bee Programme\Spelling Bee Programme\DBmainframe.mdb" 'uses varible from formlogin db_connection equal to software provider for database and location of database.
'------------------------------------------------------- fills dataset words below ----------------------------------------------
Dim dataset As DataTable = New DataTable("word")
formlogin.db_connection.Open() 'uses variable from formlogin db_connection to open database
da.Fill(ds, "words")
formlogin.db_connection.Close()
formlogin.db_connection.Open() 'uses variable from formlogin db_connection to open database
vbscore = 0 ' resets value to ensure that you can not get a score over 20 which is the maximum value if you got every question right.
Dim wordsarray() As TextBox = {txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8, txt9, txt10}
Dim reviewscore() As String = {lblscorereview1.Text, lblscorereview2.Text, lblscorereview3.Text, lblscorereview4.Text, lblscorereview5.Text, lblscorereview6.Text, lblscorereview7.Text, lblscorereview8.Text, lblscorereview9.Text, lblscorereview10.Text}
Dim i As Integer
For i = 0 To 9
wordsarray(i).Text = wordsarray(i).Text.ToUpper()
i = i + 1
Next
i = 0
For i = 0 To 9
If wordsarray(i).Text = "" Then
MsgBox("Please Fill in all the TextBoxes")
wordsarray(i).Focus()
formlogin.db_connection.Close()
Exit Sub
ElseIf wordsarray(i).Text = ds.Tables("words").Rows(i).Item(1) Then
vbscore = vbscore + 2 'adds 2 to varible "vbscore" if correct
reviewscore(i) = vbscore
lblresult.Text = Val(lblresult.Text) + Val(i)
formlogin.db_connection.Close()
End If
Next
Dim objwriter As New System.IO.StreamWriter(FILE_NAME, True)
Dim tbscore As DataTable = New DataTable("score")
Dim cmdInsert As New OleDbCommandBuilder(da)
sql = "SELECT * FROM result"
da = New OleDbDataAdapter(sql, formlogin.db_connection)
formlogin.db_connection.Open()
da.Fill(ds, "score")
ds.Tables("score").Rows(0).Item(0) = formlogin.txtuser.Text
ds.Tables("score").Rows(1).Item(1) = tbscore
da.Update(ds, "score")
formlogin.db_connection.Close()
time = DateTime.Now.ToLongDateString
DateTime.Now.ToString("hh:mm:ss dddd, dd MMMM yyyy")
objwriter.WriteLine(formlogin.txtuser.Text & " score = " & vbscore & " attempted test at " & time)
objwriter.WriteLine()
objwriter.Close()
formlogin.db_connection.Close()
MsgBox("Thank you for taking part in the test you're score was " & vbscore) ' shows partisapent a thank you message and what score they got in the test.
Me.Close()
formlogin.Show()
End Sub
Private Function Mark(ByVal InputtedWord As String, ByVal DBWord As String) As String
Dim i As Integer
Dim wrongletters As Integer
Dim InputCharacter As Char
Dim DbChar As Char
Dim inputcounter As Integer
Dim dbcounter As Integer
Dim inputtextlength As Integer
Dim dbtextlength As Integer
Dim maxtextlength As Integer
InputtedWord = InputtedWord.ToUpper
inputtextlength = InputtedWord.Length
DBWord = DBWord.ToUpper
dbtextlength = DBWord.Length
If inputtextlength >= dbtextlength Then
maxtextlength = inputtextlength
Else
maxtextlength = dbtextlength
End If
inputcounter = 0
dbcounter = 0
For i = 0 To maxtextlength - 1
InputCharacter = InputtedWord(inputcounter)
DbChar = DBWord(dbcounter)
If InputCharacter <> DbChar Then
wrongletters = wrongletters + 1
End If
If inputcounter <> inputtextlength - 1 Then
inputcounter = inputcounter + 1
End If
If dbcounter <> dbtextlength - 1 Then
dbcounter = dbcounter + 1
End If
Next i
If wrongletters >= 2 Then
Mark = "0"
ElseIf wrongletters = 1 Then
Mark = "1"
Else : Mark = "2"
End If
End Function
Private Sub test_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbWeek.SelectedIndexChanged
If cbWeek.SelectedItem = "" Then
MsgBox("Please select a week and then press load to start the test")
End If
End Sub
Private Sub btnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnload.Click
For Me.i = 0 To 9
Definition(i).Text = ds.Tables("word").Rows(i).Item(0)
Next
End Sub
结束班