2015-09-16 38 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

感谢您的帮助,您说得对!我删除#并替换为',它的作用就像魅力!很多! –

相关问题