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
Table

Attachments Table.JPG 29.22 KB Table.JPG 29.22 KB

did you try just passing the string as a second parameter as shown in the exmaple here

query.Parameters.AddWithValue("@EMP_ID",TXTEMPID.Text)

Edited 3 Years Ago by Ancient Dragon

Cut your line at line 19 and place it at line 34.... it is trying to run data it has not yet received, so in the end either you are entring in null data or just nothing at all

Actually i have the database file in Project folder. The same file copy in Debug folder. i attach these two files in SQL Server. File in project folder table contains Null value in all field. But there is data in the file attached from Debug folder.

So try out my idea... It would help you get the data entered into the Database... you are entering null data from the code you have written...

This article has been dead for over six months. Start a new discussion instead.