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(vb.net i'm using visual basic 2008) into a sql server table called Noms.
Example:
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 vb.net):
FirstName LastName
mike keane
Here's the code tried in the button_click insert:
[code]
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
cmdInsert.Parameters.Clear()
Try
With cmdInsert
.Parameters.AddWithValue("FirstName", item1)
.Parameters.AddWithValue("LastName", item2)
.Connection = cnInsert
End With
HandleConnection(cnInsert)
'iSqlStatus 0=Failed 1-Done
iSqlStatus = cmdInsert.ExecuteNonQuery
If Not iSqlStatus = 0 Then
ShowMessage('Cannot connect to database')
Return False
Else
ShowMessage("Connected to database")
Return True
End If
Catch ex As Exception
MsgBox(ex.Message, "Error")
Finally
HandleConnection(cnInsert)
End Try
End Function
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
InsertNewRecord(item1,item2)
End Sub
[/code]