Alwyn Gardner
Alwyn Gardner

Reputation: 61

Problems with adding to SQL Server database on vb 2008

I am having a problem but don't know exactly what is wrong with it my code is below:

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

If you could help it would be much appreciated error message is as follows:

Method 'Private Sub btnAdd_Click(ByRef Success As String, sender As Object, e As System.EventArgs)' cannot handle event 'Public Event Click(sender As Object, e As System.EventArgs)' because they do not have a compatible signature. C:\Users\Alwyn\Desktop\Computing A2 Alwyn\Comp4ProjectRoomBookingSystem\WindowsApplication1\WindowsApplication1\FormAdd.vb 10 130 WindowsApplication1

Many thanks in advance.

Upvotes: 0

Views: 219

Answers (2)

Terramune
Terramune

Reputation: 31

If you want to just send success thru the signature, you can always do an optional call within the signature. (Ex:

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

If you use the optional keyword, and after sender and e, you should have no problems at all with your current way of doing things.

Upvotes: 0

Steve
Steve

Reputation: 216313

btnAdd_Click is an event handler, you can't change its predefined signature

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

remove the Success variable from the declaration and insert as local variable inside the event handler. Of course, if you need that variable also outside the click event, then you need to declare it at form global level

Upvotes: 1

Related Questions