Guyz i have this insert function that is supposed to help me insert data but somehow i dont know how to actullay call it and get it to insert data

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        DataAccess.InsertNewRecord(txtName.Text, txtSurname.Text, txtID.Text)
    End Sub
Public Shared Function InsertNewRecord(ByVal item1 As String, ByVal item2 As String, ByVal item3 As String) As Boolean
        'If not using the Express Edition uncomment the next line
        Dim cnInsert As New SqlConnection("server=,;uid=;pwd=;database=")
        
        Dim cmdInsert As New SqlCommand
        Dim sSQL As New String("")
        Dim iSqlStatus As Integer

        'Set the stored procedure we're going to execute
        sSQL = "YourProcName"

        'Inline sql needs to be structured like so
        'sSQL = "INSERT INTO YourTable(column1,column2,column3) VALUES('" & item1 & "','" & item2 & "','" & item3 & "')"

        'Clear any parameters
        cmdInsert.Parameters.Clear()
        Try
            'Set the SqlCommand Object Properties
            With cmdInsert
                'Tell it what to execute
                .CommandText = sSQL 'Your sql statement
                'Tell it its a stored procedure (if using inline sql uncomment this line
                '.CommandType = CommandType.StoredProcedure 'CommandType.Text for inline sql
                'If you arent using a stored procedure uncomment the next line
                '.CommandType = CommandType.StoredProcedure 'For inline sql
                'If you are indeed using a stored procedure
                'the next 3 lines pertain to you
                'Now add the parameters to our procedure
                'NOTE: Replace @value1.... with your parameter names in your stored procedure
                'and add all your parameters in this fashion
                .Parameters.AddWithValue("@value1", item1)
                .Parameters.AddWithValue("@value2", item2)
                .Parameters.AddWithValue("@value3", item3)
                'Set the connection of the object
                .Connection = cnInsert
            End With

            'Now take care of the connection
            HandleConnection(cnInsert)

            'Set the iSqlStatus to the ExecuteNonQuery 
            'status of the insert (0 = success, 1 = failed)
            iSqlStatus = cmdInsert.ExecuteNonQuery

            'Now check the status
            If Not iSqlStatus = 0 Then
                'DO your failed messaging here
                Return False
            Else
                'Do your success work here
                Return True
            End If
        Catch ex As Exception
            MsgBox("ex.Message,MsgBoxStyle.OkOnly", "Error")
        Finally
            'Now close the connection
            HandleConnection(cnInsert)
        End Try
    End Function

Hi!

Add a CLASS in your project, name it "DataAccess".
Add this line at start of the class just created:

Imports System.Data.SqlClient

Paste the code block you have in the second attachement.

Then only this line may cause an error:
HandleConnection(cnInsert)

It should be a method in the same class I believe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.