"错误:没有为类型'String'和'System.Windows.Forms.TextBox'定义运算符'&'."

sha*_*zaz 3 vb.net

我在.CommandText中的SAVEUPDATE按钮中收到错误

"Error: Operator '&' is not defined for types 'String' and 'System.Windows.Forms.TextBox'."
Run Code Online (Sandbox Code Playgroud)

我不知道SAVE和UPDATE按钮中的错误是什么.

这是我的代码:

Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
    Try
        With cm
            .Connection = cn
            .CommandText = "insert into [edit$]values('" & TxtId.Text & "','" & TxtFamilyname.Text & "','" & TxtGivenname.Text & "','" & TxtGender.Text & "','" & TxtDob.Text & "','" & TxtExamdate.Text & "','" & TxtExamtime.Text & "','" & TxtStreet.Text & "','" & TxtHouse.Text & "','" & TxtPlz.Text & "','" & TxtCity & "' )"
            .ExecuteNonQueryAsync()
        End With
        FillDataGridView("select * from [edit$]")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
        Return
    End Try
    MsgBox("succefully Saved!", MsgBoxStyle.Information, Text)
End Sub

Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles Btnupdate.Click
    Try
        With cm
            .Connection = cn
            .CommandText = "Update [edit$] set [Family Name] = '" & TxtFamilyname.Text & "' where id ='" & TxtId.Text & "' and Given Name = '" & TxtGivenname.Text & "'and Gender = '" & TxtGender.Text & "'and DOB = '" & TxtDob.Text & "'and Exam Date'" & TxtExamdate.Text & "'and Exam Time = '" & TxtExamtime.Text & "'and Street Name = '" & TxtStreet.Text & "'and House Nr = '" & TxtHouse.Text & "'and PLZ = '" & TxtPlz.Text & "'and CITY = '" & TxtCity & "'"
            .ExecuteNonQuery()
        End With
        FillDataGridView("select * from [edit$]")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Information, Text)
        Return
    End Try
    MsgBox("Succesfully updated!", MsgBoxStyle.Information, Text)
End Sub
Run Code Online (Sandbox Code Playgroud)

Ram*_*Now 6

答案可能很简单:

Text每次使用TextBox值时都没有调用-Property.

看看TxtCity变量:

"'and CITY = '" & TxtCity & "'"
Run Code Online (Sandbox Code Playgroud)

应该

"'and CITY = '" & TxtCity.Text & "'"
Run Code Online (Sandbox Code Playgroud)