美好的一天......我很难弄清楚代码上的问题是什么
我的添加记录按钮按钮实际上正常工作," BUT"问题是
•在文本上放入错误的数据后,即使我已经输入了正确的数据,也会反复出现错误..
Imports System.Data.OleDb
Imports System.IO
Public Class Form1
Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Charles\Documents\Database1.accdb")
Dim cmd As New OleDb.OleDbCommand
Dim dt As DataTable
Dim dr As OleDbDataReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
fillListView()
End Sub
Private Sub fillListView()
conn.Open()
With cmd
.Connection = conn
.CommandText = "Select * From Employee"
End With
ListView1.Items.Clear()
dr = cmd.ExecuteReader
While dr.Read
With ListView1
.Items.Add(dr.Item(0))
With .Items(.Items.Count - 1).SubItems
.Add(dr(1))
.Add(dr(2))
.Add(dr(3))
.Add(dr(4))
.Add(dr(5))
End With
End With
End While
conn.Close()
End Sub
Public Sub clearAll()
txtName.Clear()
txtSSS.Clear()
txtTIN.Clear()
txtSalary.Clear()
End Sub
Private Sub ListView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.Click
txtName.Text = ListView1.SelectedItems.Item(0).SubItems(1).Text
txtSSS.Text = ListView1.SelectedItems.Item(0).SubItems(2).Text
txtTIN.Text = ListView1.SelectedItems.Item(0).SubItems(3).Text
txtTIN.Text = ListView1.SelectedItems.Item(0).SubItems(5).Text
End Sub
Public Sub enableTxtBox()
txtName.Enabled = True
txtSSS.Enabled = True
txtTIN.Enabled = True
dtpED.Enabled = True
txtSalary.Enabled = True
End Sub
Public Sub disableTxtBox()
txtName.Enabled = False
txtSSS.Enabled = False
txtTIN.Enabled = False
dtpED.Enabled = False
txtSalary.Enabled = False
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
enableTxtBox()
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Dim Response As Integer
Response = MessageBox.Show("Do you really want to exit?", "", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Response = vbYes Then
Me.Close()
End If
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
conn.Open()
cmd.CommandText = "INSERT INTO Employee(EmployeeName,SSS,TIN,ED,Salary) Values(@EmployeeName,@SSS,@TIN,@ED,@Salary)"
cmd.Parameters.AddWithValue("@EmployeeName", txtName.Text)
cmd.Parameters.AddWithValue("@SSS", txtSSS.Text)
cmd.Parameters.AddWithValue("@TIN", txtTIN.Text)
cmd.Parameters.AddWithValue("@ED", dtpED.Text)
cmd.Parameters.AddWithValue("@Salary", txtSalary.Text)
cmd.ExecuteNonQuery()
conn.Close()
fillListView()
clearAll()
Catch ex As Exception
MsgBox("Error")
End Try
End Sub
End Sub
End Class
答案 0 :(得分:1)
您没有看到类似错误消息的原因是因为您根本没有使用Exceptions
。
在这部分代码中:
Catch ex As Exception
MsgBox("Error")
End Try
您已宣布Exception
,但实际上您从未使用过它
要调用Exception
,你应该写下这部分代码:
Catch ex As Exception
MsgBox(ex.message)
End Try
现在您可以看到,如果您进入Catch
,MsgBox
将显示异常,您将能够阅读实际错误。
答案 1 :(得分:0)
谢谢大家我刚刚解决了我的问题...
修复了我的fillListiew()
代码Private Sub fillListView()
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter("SELECT * FROM Employee", conn)
da.Fill(dt)
ListView1.Items.Clear()
Dim newRow As DataRow
For Each newRow In dt.Rows
ListView1.Items.Add(newRow.Item(0))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newRow(1))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newRow(2))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newRow(3))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newRow(4))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newRow(5))
Next
End Sub