Quantcast
Channel: Everyday I'm coding » vb.net
Viewing all articles
Browse latest Browse all 10

Problems with adding to SQL Server database on vb 2008

$
0
0

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.


Solution:

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


Viewing all articles
Browse latest Browse all 10