0

我遇到了问题,但不知道到底有什么问题,我的代码如下:

Imports System.Data.SqlClient
Imports System.Data
Public Class FormAdd

Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
    FormAdmin.Show()
    Me.Hide()
End Sub

Private Sub btnAdd_Click(ByRef Success As String, ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    Call AddUser(Success)
    MsgBox(" " & Success & " ")
End Sub
Private Sub AddUser(ByRef Success As String)
    lblAdmin.Text = Str(chkAdminAccount.Checked)
    Dim con As New SqlConnection
    Dim lrd As SqlDataReader
    Dim inscmd As New SqlCommand

    inscmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim())
    txtUsername.Text = txtUsername.Text
    inscmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim)
    txtPassword.Text = txtPassword.Text
    inscmd.Parameters.AddWithValue("@Name", (txtName.Text.Trim))
    txtName.Text = txtName.Text
    inscmd.Parameters.AddWithValue("@AdminAccount", lblAdmin.Text.Trim)
    lblAdmin = lblAdmin
    con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Alwyn\Desktop\Computing A2 Alwyn\Comp4ProjectRoomBookingSystem\WindowsApplication1\WindowsApplication1\Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
    Try
        con.Open()
        inscmd.CommandText = "INSERT INTO Admin (Username, Password, Name, AdminAccount) VALUES (@Username, @Password, @Name, @AdminAccount)"
        inscmd.Connection = con
        inscmd.ExecuteNonQuery()

    Catch ex As Exception
        MsgBox("Could not add User" & ex.Message & " ")
    Finally
        con.Close()
        inscmd.Parameters.Clear()
        Success = "User has been added"
    End Try

End Sub

End Class

如果您能提供帮助,将不胜感激错误消息如下:

方法“Private Sub btnAdd_Click(ByRef Success As String, sender As Object, e As System.EventArgs)”无法处理事件“Public Event Click(sender As Object, e As System.EventArgs)”,因为它们没有兼容的签名。C:\Users\Alwyn\Desktop\Computing A2 Alwyn\Comp4ProjectRoomBookingSystem\WindowsApplication1\WindowsApplication1\FormAdd.vb 10 130 WindowsApplication1

提前谢谢了。

4

2 回答 2

1

btnAdd_Click 是一个事件处理程序,您不能更改其预定义的签名

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles btnAdd.Click
    Dim Success as string
    Call AddUser(Success)
    MsgBox(" " & Success & " ")
End Sub

从声明中删除 Success 变量并作为局部变量插入到事件处理程序中。当然,如果您在点击事件之外也需要该变量,那么您需要在表单全局级别声明它

于 2013-03-29T15:17:58.703 回答
0

如果您只想通过签名发送成功,您可以随时在签名中进行可选调用。(前任:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs, Optional ByRef Success As String = Nothing ) Handles btnAdd.Click
    If Success IsNot Nothing then 
            Call AddUser(Success)
            MsgBox(" " & Success & " ")
    End If
End Sub

如果你使用 optional 关键字,并且在 sender 和 e 之后,你现在的做事方式应该完全没有问题。

于 2013-03-29T18:52:44.543 回答