时间:2018-06-08 11:53:13

标签: vb.net ms-access

我正在尝试插入代码,当文本框为空时显示弹出窗口“请填写您的凭据”但似乎没有任何结果。这是我的代码:

Imports System.Data.OleDb
Public Class AddNewStudent
    Dim cnnOLEDB As New OleDbConnection
    Dim cmdInsert As New OleDbCommand
    Dim con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\ASSIGNMENT\vbnassignmentfinal.accdb;"
    Dim cmdDelete As New OleDbCommand
    Dim cmdUpdate As New OleDbCommand
    Dim cmdSearch As New OleDbCommand
    Private Sub AddNewStudentSD_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cnnOLEDB.ConnectionString = con
        cnnOLEDB.Open()
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

        txtsem.ResetText()
        txtsfn.ResetText()
        txtsid.ResetText()
        txtsln.ResetText()
        txtsph.ResetText()
        txtint.ResetText()
        cmbgen.ResetText()


    End Sub

    Private Sub btnHome_Click(sender As Object, e As EventArgs) Handles btnhome.Click
        UserHomepage.Show()
        Me.Hide()
    End Sub

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
        cmdInsert.CommandText = "Insert into student Values(" + txtsid.Text + ",'" + txtint.Text + "','" + txtsfn.Text + "','" + txtsln.Text + "', '" + cmbgen.Text + "', " + txtsph.Text + ", '" + txtsem.Text + "');"
        cmdInsert.CommandType = CommandType.Text
        cmdInsert.Connection = cnnOLEDB


        If txtsid.Text = vbNullString Then
            MsgBox("Please fill in your credentials ", MsgBoxStyle.Information, "Verify")
            Me.Show()

        End If

        cmdInsert.ExecuteNonQuery()

        MessageBox.Show("Are you sure you want to proceed?", "Proceed", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If DialogResult.Yes Then
            AccomodationStatus.Show()
            Me.Hide()


        ElseIf DialogResult.No Then
            Me.Show()
            AccomodationStatus.Hide()
        End If


    End Sub

End Class

我在哪里做错了?

这是我解决后的当前问题:

error box shows up

this is the error showing after pressing ok

1 个答案:

答案 0 :(得分:0)

您尚未正确连接CommandText。

试试这个:

cmdInsert.CommandText = "INSERT INTO [student] Values('" & txtsid.Text & "','" & txtint.Text & "','" & txtsfn.Text & "','" & txtsln.Text & "','" & cmbgen.Text & "','" & txtsph.Text & "','" & txtsem.Text & "')"

由于您没有提供列名称,因此检查表格的列数与插入值的列数一样多。