2015-09-16 39 views
1

我一直有這個錯誤型system.data.sqlclient.sqlexception的未處理的異常出現在System.dll中

「型system.data.sqlclient.sqlexception的未處理的異常出現在System.dll中

我不能似乎發現我在想念着什麼,它指向我

Private Sub Check_Info()       
etc.. 
etc... 
etc. 
dDA.FILL(dDS) '<< 

注: 我有我的SQLserver運行和運行它們的連接表是確定 代碼如下

#Region " Variable Declarations " 

    Public sConn As SqlConnection 
    Dim eDS As DataSet = New DataSet 
    Dim eDA As SqlDataAdapter = New SqlDataAdapter 
    Dim eDR As DataRow 
    Dim dDS As DataSet = New DataSet 
    Dim dDA As SqlDataAdapter = New SqlDataAdapter 
    Dim dDR As DataRow 
    'Public bExitApplication As Boolean 

#End Region 

#Region " User-defined Procedures " 

    Private Sub Check_Info() 
     sConn.Open() 
     eDA.SelectCommand = New SqlCommand("SELECT emp_fname,emp_lname,emp_mname FROM tblEmployee WHERE emp_idno='" & TextBox1.Text.ToString & "' and emp_pass='" & TextBox2.Text & "'", sConn) 
     eDS.Clear() 
     eDA.Fill(eDS) 
     If eDS.Tables(0).Rows.Count > 0 Then 
      eDR = eDS.Tables(0).Rows(0) 
      TextBox3.Text = eDR("emp_lname") & ", " & eDR("emp_fname") & " " & eDR("emp_mname") 
      dDA.SelectCommand = New SqlCommand("SELECT * FROM tblDTR WHERE emp_idno='" & TextBox1.Text & "' AND date_timein=#" & Format(Now, "MM/d/yyyy") & "# AND time_timeout IS NULL", sConn) 
      dDS.Clear() 
      dDA.Fill(dDS) 

      If dDS.Tables(0).Rows.Count > 0 Then 
       dDR = dDS.Tables(0).Rows(0) 
       Button1.Enabled = True 
       Button1.Text = "&Time Out" 
       TextBox4.Text = Format(dDR("time_timein"), "h:mm:ss tt") 
      Else 
       Button1.Enabled = True 
       Button1.Text = "&Time In" 
      End If 
      dDR = Nothing 
      dDS.Dispose() 
      dDA.Dispose() 
     Else 
      TextBox3.Clear() 
      TextBox4.Clear() 
      TextBox5.Clear() 
      Button1.Enabled = False 
     End If 
     eDR = Nothing 
     eDS.Dispose() 
     eDA.Dispose() 
     sConn.Close() 
    End Sub 

    Private Sub ReturnFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.GotFocus, TextBox4.GotFocus, TextBox5.GotFocus 
     TextBox1.Focus() 
    End Sub 

    Private Sub TextboxChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged 
     If (TextBox1.Text <> "" And TextBox2.Text <> "") Then 
      Check_Info() 
     End If 
    End Sub 

    Private Sub LoginGotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus 
     TextBox3.Clear() 
     TextBox4.Clear() 
     TextBox5.Clear() 
    End Sub 
#End Region 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     sConn = New SqlConnection("Data Source=(localdb)\Projects;Initial Catalog=StudInfo;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False") 
     Label2.Text = Format(Now, "MMMM d, yyyy h:mm:ss tt") 
     Timer1.Enabled = True 
    End Sub 

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
     Label2.Text = Format(Now, "MMMM d, yyyy h:mm:ss tt") 
    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     ' Time-in and Time-out button 
     Dim strSQL As String 
     sConn.Open() 
     If Button1.Text = "&Time In" Then 
      dDR = dDS.Tables(0).NewRow() 
      TextBox4.Text = Format(Now, "h:mm:ss tt") 
      strSQL = "INSERT INTO tblDTR (emp_idno, date_timein, time_timein) VALUES ('" & TextBox1.Text & "', #" & Format(Now, "MM/d/yyyy") & "#, #" & TextBox4.Text & "#)" 
      Button1.Text = "&Time Out" 
     Else 
      TextBox5.Text = Format(Now, "h:mm:ss tt") 
      strSQL = "UPDATE tblDTR SET time_timeout=#" & TextBox5.Text & "# WHERE emp_idno='" & TextBox1.Text & "' AND date_timein=#" & Format(Now, "MM/d/yyyy") & "# and time_timein=#" & TextBox4.Text & "#" 
      Button1.Text = "&Time In" 
     End If 
     Dim dCmd As SqlCommand = New SqlCommand(strSQL, sConn) 
     dCmd.ExecuteNonQuery() 
     dCmd.Dispose() 

     dDR = Nothing 
     dDS.Dispose() 
     dDA.Dispose() 
     sConn.Close() 
     Button1.Enabled = False 
     TextBox1.Clear() 
     TextBox2.Clear() 
    End Sub 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
     ' clear the form 
     TextBox1.Clear() 
     TextBox2.Clear() 
     TextBox3.Clear() 
     TextBox4.Clear() 
     TextBox5.Clear() 
     Button1.Text = "&Time In" 
     Button1.Enabled = False 
     TextBox1.Focus() 
    End Sub 

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 
     ' System Administration 
     bExitApplication = True 
     Dim f As Form 
     f = New UserLogin 
     Me.Close() 
     f.Show() 

    End Sub 

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing 
     If Not bExitApplication Then 
      MsgBox("You cannot close the system this way..." & vbCrLf & vbCrLf & "Please Quit the application in the System Administration Module or by clicking Exit in the bottom of this window.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error: No Administrative Privilage") 
      e.Cancel = True 
     End If 
    End Sub 

    Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click 

    End Sub 

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click 
     MsgBox("Thank you for using Attandance Monitoring System") 
     bExitApplication = True 
     Application.Exit() 
    End Sub 
End Class 
+3

yikes。不要將字符串從UI注入到SQL命令中。使用參數。 –

+0

減少所有一次性物品的範圍。儘可能添加使用語句。抓住sqlexception並檢查其所有值。 –

+0

你爲什麼要在你的約會周圍#我從來沒有見過,我無法找到記錄。可能無效的SQL。 –

回答

0

幾乎肯定來源於此:

...date_timein=#" & Format(Now, "MM/d/yyyy") & "#... 

你是不是產生VB代碼,你生成的SQL代碼,因此在字符串文本不使用#。正確的方法是輸入參數:

dDA.SelectCommand = New SqlCommand("SELECT * FROM tblDTR WHERE [email protected] AND [email protected] AND time_timeout IS NULL", sConn) 
cmd.Parameters.Add("@Today",DbType.DateTime).Value = DateTime.Now 
cmd.Parameters.Add("@EmpID",DbType.Int).Value = TextBox1.Text 
+0

嗨,先生@GarrGodfrey ive使用你的方法先生,但無濟於事。真的卡住了這個問題,但仍然在使用你給出的代碼玩。 –

+0

感謝您的幫助,您說得對!我刪除#並替換爲',它的作用就像魅力!很多! –

相關問題