条件表达式中的数据类型不匹配(cmd.ExecuteNonQuery)

时间:2014-02-26 13:26:25

标签: vb.net-2010

单击保存按钮时出现错误。 我得到的错误是(条件表达式中的数据类型不匹配) 请帮我修理一下 谢谢! http://www.vbforums.com/showthread.php?755515-Data-Type-Mismatch-in-criteria-expression 这是我的代码;

Imports System.Data.OleDb
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Public Class Staff_Editor
Dim rdr As OleDbDataReader = Nothing
Dim dtable As New DataTable
Dim con As OleDbConnection = Nothing
Dim adp As OleDbDataAdapter
Dim ds As DataSet
Dim cmd As OleDbCommand = Nothing
Dim dt As New DataTable
Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Payroll_BackEnd.accdb"

   Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeCodeTextBox2.Validating
    Dim textbox As TextBox = DirectCast(sender, TextBox)

    If Not textbox.Text Like "NB###" Then
        With textbox
            .Focus()
            .SelectAll()
            MessageBox.Show("Invalid Format!")
        End With
    End If
End Sub
Private Sub AutoGenerate()
Dim numbers = String.Join("", Enumerable.Range(0, 9).OrderBy(Function(c) Guid.NewGuid).Take(3))
Me.EmployeeCodeTextBox2.Text = "NB" & numbers
End Sub
Private Sub Staff_Editor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub PlaceButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlaceButton1.Click
    Department_CodeTextBox2.Text = Department_CodeTextBox1.Text
End Sub
Sub Reset()
    Employee_CodeTextBox.Text = ""
    Employee_NameTextBox.Text = ""
    Last_NameTextBox.Text = ""
    GenderComboBox.Text = ""
    Birth_DatePicker.Text = ""
    Contact_NumberTextBox.Text = ""
    AddressTextBox.Text = ""
    EmailTextBox.Text = ""
    DesignationComboBox.Text = ""
    Date_EmployedPicker.Text = ""
    Basic_PayTextBox.Text = ""
    StatusComboBox.Text = ""
    UpdateButton1.Enabled = False
    DeleteButton1.Enabled = False
    SaveButton1.Enabled = True
    Employee_NameTextBox.Focus()
 AutoGenerate()
 End Sub
 Private Sub Add_NewButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add_NewButton1.Click
    Dim promptQuestion As DialogResult = MessageBox.Show("Are you sure you want to add a new record?", "Add New Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If promptQuestion = Windows.Forms.DialogResult.No Then`enter code here`
        Exit Sub
    End If
    Reset ()
End Sub

Private Sub SaveButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton1.Click
    Dim promptQuestion As DialogResult = MessageBox.Show("Are you sure you want to save this record?", "Save Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If promptQuestion = Windows.Forms.DialogResult.No Then
        Exit Sub
    End If
    If Len(Trim(Employee_NameTextBox.Text)) = 0 Then
        MessageBox.Show("Please enter employee name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Employee_NameTextBox.Focus()
    End If
    If Len(Trim(Last_NameTextBox.Text)) = 0 Then
        MessageBox.Show("Please enter last name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Last_NameTextBox.Focus()
    End If
    If Len(Trim(GenderComboBox.Text)) = 0 Then
        MessageBox.Show("Please enter Gender", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        GenderComboBox.Focus()
    End If
    If Len(Trim(Birth_DatePicker.Value)) = 0 Then
        MessageBox.Show("Please enter Birth Date", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Birth_DatePicker.Focus()
    End If
    If Len(Trim(Contact_NumberTextBox.Text)) = 0 Then
        MessageBox.Show("Please enter Contact Number", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Contact_NumberTextBox.Focus()
    End If
    If Len(Trim(AddressTextBox.Text)) = 0 Then
        MessageBox.Show("Please enter Address", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        AddressTextBox.Focus()
    End If
    If Len(Trim(EmailTextBox.Text)) = 0 Then
        MessageBox.Show("Please enter Email", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        EmailTextBox.Focus()
    End If
    If Len(Trim(DesignationComboBox.Text)) = 0 Then
        MessageBox.Show("Please enter Designation", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        DesignationComboBox.Focus()
    End If
    If Len(Trim(Date_EmployedPicker.Value)) = 0 Then
        MessageBox.Show("Please enter Date Employed", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Date_EmployedPicker.Focus()
    End If
    If Len(Trim(Basic_PayTextBox.Text)) = 0 Then
        MessageBox.Show("Please enter Basic Salary", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Basic_PayTextBox.Focus()
    End If
    If Len(Trim(StatusComboBox.Text)) = 0 Then
        MessageBox.Show("Please enter Status", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        StatusComboBox.Focus()
    End If

    AutoGenerate()

    con = New OleDbConnection(cs)
    con.Open()

    Dim cb As String = "insert into Staff_Details(DepartmentCode,EmployeeCode,EmployeeName,LastName,Gender,BirthDate,ContactNumber,Address,Email,Designation,DateEmployed,BasicSalary,Status) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13)"

    cmd = New OleDbCommand(cb)
    cmd.Connection = con

    cmd.Parameters.Add(New OleDbParameter("@d1", OleDbType.VarChar, 6, "DepartmentCode"))
    cmd.Parameters.Add(New OleDbParameter("@d2", OleDbType.VarChar, 5, "EmployeeCode"))
    cmd.Parameters.Add(New OleDbParameter("@d3", OleDbType.VarChar, 20, "EmployeeName"))
    cmd.Parameters.Add(New OleDbParameter("@d4", OleDbType.VarChar, 20, "LastName"))
    cmd.Parameters.Add(New OleDbParameter("@d5", OleDbType.VarChar, 6, "Gender"))
    cmd.Parameters.Add(New OleDbParameter("@d6", OleDbType.VarChar, 30, "BirthDate"))
    cmd.Parameters.Add(New OleDbParameter("@d7", OleDbType.VarChar, 11, "ContactNumber"))
    cmd.Parameters.Add(New OleDbParameter("@d8", OleDbType.VarChar, 100, "Address"))
    cmd.Parameters.Add(New OleDbParameter("@d9", OleDbType.VarChar, 50, "Email"))
    cmd.Parameters.Add(New OleDbParameter("@d10", OleDbType.VarChar, 15, "Designation"))
    cmd.Parameters.Add(New OleDbParameter("@d11", OleDbType.VarChar, 30, "DateEmployed"))
    cmd.Parameters.Add(New OleDbParameter("@d12", OleDbType.VarChar, 10, "BasicSalary"))
    cmd.Parameters.Add(New OleDbParameter("@d13", OleDbType.VarChar, 15, "Status"))

    cmd.Parameters("@d1").Value = Department_CodeTextBox2.Text
    cmd.Parameters("@d2").Value = Employee_CodeTextBox.Text
    cmd.Parameters("@d3").Value = Employee_NameTextBox.Text
    cmd.Parameters("@d4").Value = Last_NameTextBox.Text
    cmd.Parameters("@d5").Value = GenderComboBox.Text
    cmd.Parameters("@d6").Value = Birth_DatePicker.Value
    cmd.Parameters("@d7").Value = Contact_NumberTextBox.Text
    cmd.Parameters("@d8").Value = AddressTextBox.Text
    cmd.Parameters("@d9").Value = EmailTextBox.Text
    cmd.Parameters("@d10").Value = DesignationComboBox.Text
    cmd.Parameters("@d11").Value = Date_EmployedPicker.Value
    cmd.Parameters("@d12").Value = Basic_PayTextBox.Text
    cmd.Parameters("@d13").Value = StatusComboBox.Text

    cmd.ExecuteNonQuery()
    MessageBox.Show("Record successfully saved.")
    SaveButton1.Enabled = False
    Employee_CodeTextBox.Focus()
    con.Close()
End Sub
Private Sub Basic_PayTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Basic_PayTextBox.KeyPress
    Dim keyChar = e.KeyChar

    If Char.IsControl(keyChar) Then
        'Allow all control characters.
    ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
        Dim text = Me.Basic_PayTextBox.Text
        Dim selectionStart = Me.Basic_PayTextBox.SelectionStart
        Dim selectionLength = Me.Basic_PayTextBox.SelectionLength

        text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)

        If Integer.TryParse(text, New Integer) AndAlso text.Length > 16 Then
            'Reject an integer that is longer than 16 digits.
            e.Handled = True
        ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
            'Reject a real number with two many decimal places.
            e.Handled = False
        End If
    Else
        'Reject all other characters.
        e.Handled = True
    End If
End Sub
Private Sub Employee_CodeTextBox_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ListControlConvertEventArgs)
    If e.DesiredType Is GetType(String) Then
        e.Value = e.Value.ToString.Trim
    End If
End Sub
 Private Sub Employee_NameTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Employee_NameTextBox.KeyPress
  If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
  Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
  And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
  Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
        'space accepted
        If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
            e.Handled = True
        End If
    End If
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then

        e.Handled = False
    End If
End Sub
Private Sub EmailTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles EmailTextBox.Validating
    Dim rEMail As New System.Text.RegularExpressions.Regex("^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")
    If EmailTextBox.Text.Length > 0 Then
        If Not rEMail.IsMatch(EmailTextBox.Text) Then
            MessageBox.Show("invalid email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            EmailTextBox.SelectAll()
            e.Cancel = True
        End If
    End If
End Sub
Private Sub Employee_CodeTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim selectionStart As Integer = Me.Employee_CodeTextBox.SelectionStart

    Me.Employee_CodeTextBox.Text = Me.Employee_CodeTextBox.Text.ToUpper()
    Me.Employee_CodeTextBox.SelectionStart = selectionStart
End Sub

    Private Sub UpdateButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton1.Click
    If Len(Trim(Employee_CodeTextBox.Text)) = 0 Then
        MessageBox.Show("Please select employee Code", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Employee_CodeTextBox.Focus()
        Exit Sub
    End If
    con = New OleDbConnection(cs)
    con.Open()

    Dim cb As String = "Update Staff_Details set EmployeeName='" & Employee_NameTextBox.Text & "',LastName='" & Last_NameTextBox.Text & "',Gender='" & GenderComboBox.Text & "',BirthDate='" & Birth_DatePicker.Text & "',ContactNumber='" & Contact_NumberTextBox.Text & "',Address='" & AddressTextBox.Text & "',Email='" & EmailTextBox.Text & "',Designation='" & DesignationComboBox.Text & "',DateEmployed='" & Date_EmployedPicker.Text & "',BasicSalary='" & Basic_PayTextBox.Text & "',Status='" & StatusComboBox.Text & "' where EmployeeCode='" & Employee_CodeTextBox.Text & "'"

    cmd = New OleDbCommand(cb)

    cmd.Connection = con

    If MessageBox.Show("Are you sure want to update the record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

        cmd.ExecuteReader()
        MessageBox.Show("Successfully updated", "Employee Details", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Employee_CodeTextBox.Focus()
        UpdateButton1.Enabled = False

    End If
    If con.State = ConnectionState.Open Then
        con.Close()
    End If

    con.Close()
End Sub
End Class

0 个答案:

没有答案