Insert into table on button press

Hi everyone!

I want to insert in a table called Noms in specified columns FirstName and LastName a row(the table has no rows is created in sql server using create table command).

I want to implement a button that sends values typed in textboxes of gui( i'm using visual basic 2008) into a sql server table called Noms.


2 textboxes

Label=FirstName associated Textbox1 (I/user type here mike)

Label=LastName associated Textbox1 (I type here keane)

Press button send the the 2 values into the 2 columns (id and name) of Noms table

Initially the table is: (create table only in sql server)

FirstName LastName

So the output in my Noms table of sql server should be: (on button press insert of win form

FirstName LastName

mike keane

Here's the code tried in the button_click insert:

Public Shared Function InsertNewRecord(ByVal item1 As String, ByVal item2 As String) As Boolean

'YourConnName is username from sql server authentification?

Dim cnInsert As New OleDbConnection(GetConnectionString("YourConnName"))
Dim cmdInsert As New OleDbCommand
Dim query As String = "INSERT INTO Noms (FirstName,LastName) VALUES(@item1,@item2)"

Dim iSqlStatus As Integer



With cmdInsert

.Parameters.AddWithValue("FirstName", item1)
.Parameters.AddWithValue("LastName", item2)

.Connection = cnInsert
End With


'iSqlStatus 0=Failed 1-Done

iSqlStatus = cmdInsert.ExecuteNonQuery

If Not iSqlStatus = 0 Then
ShowMessage('Cannot connect to database')
Return False
ShowMessage("Connected to database")
Return True
End If
Catch ex As Exception
MsgBox(ex.Message, "Error")

End Try
End Function

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
End Sub


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!