I try to insert data from VB.NET form to SQL Database Table. When i press insert Button. It shows Successfully Added. But there is no data in database table. My code is given below.
Private Sub BTNSAVE_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTNSAVE.Click
CheckMyControls()
If totflag = False Then
MessageBox.Show("Give Complete Data!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
coloring()
ElseIf totflag = True Then
Dim gend As String
gend = ""
If RBMALE.Checked = True And RBFEMALE.Checked = False Then
gend = RBMALE.Text
ElseIf RBFEMALE.Checked = True And RBMALE.Checked = False Then
gend = RBFEMALE.Text
End If
Try
getConnect()
Dim query As SqlCommand
Dim strSQL As String
strSQL = "INSERT INTO EMPLOYEE _(EMP_ID,EMP_NAME,EMP_FNAME,EMP_GENDER,EMP_DOB,EMP_CAST,EMP_DEPART,EMP_DESIG,EMP_DOJ,EMP_SALARY,EMP_PF_ESI,EMP_BRANCH,EMP_CONTACT,EMP_ADDRESS)VALUES(@EMP_ID,@EMP_NAME,@EMP_FNAME,@EMP_GENDER,@EMP_DOB,@EMP_CAST,@EMP_DEPART,@EMP_DESIG,@EMP_DOJ,@EMP_SALARY,@EMP_PF_ESI,@EMP_BRANCH,@EMP_CONTACT,@EMP_ADDRESS)"
query = New SqlCommand(strSQL, Conn)
query.Parameters.AddWithValue("@EMP_ID", SqlDbType.Decimal).Value = TXTEMPID.Text
query.Parameters.AddWithValue("@EMP_NAME", SqlDbType.NVarChar).Value = TXTNAME.Text
query.Parameters.AddWithValue("@EMP_FNAME", SqlDbType.NVarChar).Value = TXTFNAME.Text
query.Parameters.AddWithValue("@EMP_GENDER", SqlDbType.Char).Value = gend
query.Parameters.AddWithValue("@EMP_DOB", SqlDbType.DateTime).Value = DTPEMPDOB.Value
query.Parameters.AddWithValue("@EMP_CAST", SqlDbType.NVarChar).Value = TXTCASTE.Text
query.Parameters.AddWithValue("@EMP_DEPART", SqlDbType.NVarChar).Value = CMBDEPT.Text
query.Parameters.AddWithValue("@EMP_DESIG", SqlDbType.NVarChar).Value = CMBDESIG.Text
query.Parameters.AddWithValue("@EMP_DOJ", SqlDbType.DateTime).Value = DTPEMPDOJ.Value
query.Parameters.AddWithValue("@EMP_SALARY", SqlDbType.Money).Value = MTXTSAL.Text
query.Parameters.AddWithValue("@EMP_PF_ESI", SqlDbType.Money).Value = MTXTPFESI.Text
query.Parameters.AddWithValue("@EMP_BRANCH", SqlDbType.NVarChar).Value = TXTBRANCH.Text
query.Parameters.AddWithValue("@EMP_CONTACT", SqlDbType.Decimal).Value = MTXTCONTACT.Text
query.Parameters.AddWithValue("@EMP_ADDRESS", SqlDbType.Text).Value = RTXTADDRESS.Text
Conn.Open()
Dim numAffected = query.ExecuteNonQuery()
Conn.Close()
If numAffected > 0 Then
MessageBox.Show("Successfully Added", "Add", MessageBoxButtons.OK, MessageBoxIcon.Information)
BTNCLEAR.PerformClick()
Else
MsgBox("No record was inserted")
End If
Catch ex As Exception
MsgBox("ERROR: " + ex.Message, MsgBoxStyle.Information, "Add")
End Try
End If
End Sub
Check this code and give me a solution. I am using DateTime Picker for date. I attached Database table Screenshot below