Hello Everyone... Can someone tell me what am not doing right here? Because am trying to insert a record into a database through a VB.net 2012 windows form into a MSsql satabase.
Problems:
When I write the codes without the insert statement, it will work well and open the connection. But when I put the insert statement (i.e. cmd=New SqlCommand), it will open the connection and then still go to the catch part by not adding any column at all into the table.
Imports System.Data.SqlClient
Imports System.Data
Public Class Corpers
Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles BtnConnect.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim constr As String
constr = "server=(local)\SQLEXPRESS; initial catalog=master; integrated security=True"
con = New SqlConnection(constr)
Try
con.Open()
MsgBox("Connection Opened!")
cmd = New SqlCommand("Insert into tblCorper values ('" + txtFname.Text + "','" + txtMname.Text + "','" + txtLName.Text + "','" + txtAge.Text + "','" + cboSex.Text + "','" + cboState.Text + "','" + cboDept.Text + "')", con)
cmd.ExecuteNonQuery()
MsgBox("Record Added Successfully")
Catch ex As Exception
MsgBox("Connection did not open!")
Finally
con.Close()
End Try
End Sub