2013-11-28 2 views
0

로그인 시스템이 포함 된 맞춤법 프로그램을 만들고 있는데이 특정 오류를 해결하기 위해 수 주일을 보냈습니다. 여기에 내 오류가 있습니다 : 양식을 만드는 동안 오류가 발생했습니다. 자세한 내용은 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 

가능하면이 오류를 수정하는 방법에 대한 조언을 싶어요 :

다음은 로그인 시스템 내 로그인 코드입니다.

+5

최종 등급 : 자세한 내용은의 InnerException 봐. 그러면 InnerException은 무엇을 보여줍니까? –

+0

그것은 밀도가 높은 코드입니다. 더 적은 수의 잘 알려진 메소드로 리팩토링하는 것을 고려 했습니까? – majjam

+0

이 코드는 나를 위해 잘 작동합니다. 우리에게 시험을 보여줄 수 있습니까? – majjam

답변

0

시험의 코드는 종료하지만 거기에 뭔가 넣다 살펴되지 않습니다 문자열 = "E로 정수 희미한 FILE_NAME으로

수입 System.Data.OleDb 공용 클래스 테스트 희미한 vbscore가 : \ OleDb.OleDbDataAdapter 어둡게 박사로 문자열 희미한 다 같이 RESULTS.TXT " 희미한 SQL OleDb.OleDbDataReader으로 이제 정수 희미한 시간으로 OleDb.OleDbCommandBuilder = 아무것도 희미한 I =으로 새로운 데이터 집합 희미한 CB으로 희미한 DS. ToShortTimeString 공개 S hared 정의() As Label = {test.lbldefinition1, test.lbldefinition2, test.lbldefinition3, test.lbldefinition4, test.lbldefinition5, test.lbldefinition6, test.lbldefinition7, test.lbldefinition8, test.lbldefinition9, test.lbldefinition10}

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 

예외는 당신에게 다음 단계를 알려줍니다

+0

나는이 실수를 근본적으로 수정했다. 실수로 코드의 모든 한 줄에 중단 점을 추가하여 깨달았을 때 그들을 모두 제거했다. – user3047373

관련 문제