mscorlib.dll中发生'System.FormatException' - DateTime

时间:2016-03-22 08:55:55

标签: vb.net datetime error-handling

还出现以下疑难解答提示...... “将String转换为DateTime时,解析String以在将每个变量放入DateTime对象之前获取数据”

我无法弄清楚为什么我的程序试图将其转换为DateTime? 我的访问数据库(已连接)严格来说是自动编号,数字和数字。短文。

Dim objConnection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= InfoSystem.accdb")
Dim objAdminDA As New OleDb.OleDbDataAdapter("Select *from tblStudents", objConnection)
Dim objAdminCB As New OleDb.OleDbCommandBuilder(objAdminDA)
Dim objDataSet As New DataSet()

Private Sub frmAdmin_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'Clears DataSet of any existing data
    objDataSet.Clear()
    'Fills schema - adds table structure information to DataSet
    objAdminDA.FillSchema(objDataSet, SchemaType.Source, "tblStudents")
    'Fills DataSet with info from the DataAdapter
    objAdminDA.Fill(objDataSet, "tblStudents")
    'Fill the DataSet with info from the Admin table
    objAdminDA.FillSchema(objDataSet, SchemaType.Source, "tblStudents")
    objAdminDA.Fill(objDataSet, "tblStudents")

    Dim objRow As DataRow
    objRow = objDataSet.Tables("tblStudents").Rows.Find("StudentNo")

    txtAdminID2.Text = objRow.Item("StudentName")
    txtAdminName2.Text = objRow.Item("AdminName")
    txtDept2.Text = objRow.Item("Department")
    txtTitle2.Text = objRow.Item("Title")

以下代码根除了以前表单中的问题,但似乎无法在此处操作它。 (TextBox而不是ComboBox)。第一次在VB.net上使用数据库。

Dim objRow As DataRow
    objRow = objDataSet.Tables("tblAdmins").Rows.Find(cboxAdminID.SelectedItem.ToString)
    txtAdminName.Text = objRow.Item("AdminName")
    txtDept.Text = objRow.Item("Department")
    txtTitle.Text = objRow.Item("Title")

1 个答案:

答案 0 :(得分:0)

我会更改你的代码,因为你正在填充数据集两次:

Dim objConnection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= InfoSystem.accdb")
Dim objAdminDA As New OleDb.OleDbDataAdapter("Select * from tblStudents", objConnection)
Dim objAdminCB As New OleDb.OleDbCommandBuilder(objAdminDA)
Dim objDataSet As New DataSet()

Private Sub frmAdmin_Load(sender As Object, e As EventArgs) Handles MyBase.Load

'Clears DataSet of any existing data
objDataSet.Clear()
'Fills DataSet with info from the DataAdapter
objAdminDA.Fill(objDataSet, "tblStudents")

' Get the row with the matching StudentNo
Dim objRow As DataRow
objRow = objDataSet.Tables("tblStudents").Rows.Find("StudentNo")

If objRow.count = 1 Then
    txtAdminID2.Text = objRow.Item("StudentName").ToString
    txtAdminName2.Text = objRow.Item("AdminName").ToString
    txtDept2.Text = objRow.Item("Department").ToString
    txtTitle2.Text = objRow.Item("Title").ToString
End-if