로그인 시스템이 포함 된 맞춤법 프로그램을 만들고 있는데이 특정 오류를 해결하기 위해 수 주일을 보냈습니다. 여기에 내 오류가 있습니다 : 양식을 만드는 동안 오류가 발생했습니다. 자세한 내용은 Exception.InnerException을 참조하십시오. 오류 : The type initializer for 'Spelling_Bee_Programme.test' threw an exception
.양식을 만드는 동안 오류가 발생했습니다. 자세한 내용은 Exception.InnerException을 참조하십시오. in visual basic 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