title: CheckIfExist. If exist don't insert the data, if not insert the data
body: <p>I have a problem on how to insert data into two different table. So my requirements is this.</p>
  
  <ul>
  <li>Under Group Details, The user need to click all the needed information on the dropdown menu and input on the textbox of the table grid view before clicking the ADD Link, after this the page will load displaying the Added Job Title and business group details. The user is allowed to input as many Job title as the user want.</li>
  </ul>
  
  <p>I already finished the table but I have problems in saving the data that I input.</p>
  
  <p>So my first table looks like this <a href="https://i.stack.imgur.com/oqJkV.png" rel="nofollow noreferrer">Before</a></p>
  
  <p>and I edit it and this is my table <a href="https://i.stack.imgur.com/jPDt2.png" rel="nofollow noreferrer">Now</a></p>
  
  <p>So my problem is this, in my database i have two table. One is <code>EMPGROUP_TBL</code> with columns SEQID, masterID, Business Unit, Division, Sub-Division etc. and the other is <code>EMP_MASTERTBL</code> with columns MasterID, Name, LastName, Jobtitle.</p>
  
  <p>Now everytime I click Add link the jobtitle will not be able to save in the EMP_MASTERTBL so I create a code in VB.Net that will update the EMP_MASTERTBL table when I click the add button under Group Details.</p>
  
  <p>Here's my codes.</p>
  
  <pre><code>If UpdateInsDelRecord("INSERT INTO EMPGROUP_TBL (MASTERID, BUSINESS_UNIT, " &amp; _
                    "DIVISION, SUB_DIVISION, CLASSIFICATION, SUB_CLASSIFICATION) VALUES " &amp; _
                    "('" &amp; HandleQuote(Me.lblval_Empid.Text) &amp; "', " &amp; _
                    "'" &amp; Me.ddl_BusinessUnit.SelectedValue.ToString() &amp; "' ," &amp; _
                    "'" &amp; val_division &amp; "' ," &amp; _
                    "'" &amp; val_subdivision &amp; "' ," &amp; _
                    "'" &amp; Me.ddl_Classification.SelectedValue.ToString() &amp; "' ," &amp; _
                    "'" &amp; Me.ddl_SubClassification.SelectedValue.ToString() &amp; "')" &amp; _
                    ";" &amp; _
                     "UPDATE EMP_MASTERTBL SET JOBTITLE = '" &amp; Me.txtJobtitle.Text &amp; "' " &amp; _
                     "WHERE MASTERID = '" &amp; Me.lblval_Empid.Text &amp; "'") = True Then
      Return True
      Response.Redirect("eHR_EmpMaintenance.aspx")
  Else
      Return False
  End If
  </code></pre>
  
  <p>But the user must be able to add as many as Jobtitle and EMPGROUP_TBL details as the user want. So I'm thinking that I'll just write another query for that? How can I add the Group Details and be able to add as many as Jobtitle as the user want?</p>
  
  <p><strong>CheckIfExist</strong></p>
  
  <p>I figured maybe I could use the CheckIfExist and if the employee has an existing data to the jobtitle, business unit, division, sub-division, classification and sub-classification similar to the one that you will add, the messagebox will show that the data already exist. If no data found then it will be able to add the details under the employee's group details. And if you input similar jobtitle but different business unit etc. the data will just be updated and vice versa.</p>
  
  <p>Here's what my code for this.</p>
  
  <pre><code>   Function SaveUserGroup() As Boolean
      Try
          Dim jobtitle As String = Me.txtJobtitle.Text
          Dim businessunit As String = Me.ddl_BusinessUnit.SelectedValue
          Dim division As String = Me.ddl_Division.SelectedValue
          Dim subdivision As String = Me.ddl_SubDivision.SelectedValue
          Dim classification As String = Me.ddl_Classification.SelectedValue
          Dim subclassification As String = Me.ddl_SubClassification.SelectedValue
          Dim CheckMasterTblIfExist As Boolean
          Dim CheckGroupTblIfExist As Boolean
          Dim insrtResult As Boolean
          Dim seqid As String = Me.lblSEQID.Text
          Dim emp_id As String = Request.QueryString("emp_id")
  
          If jobtitle &lt;&gt; "" And businessunit &lt;&gt; "Please Select" And division &lt;&gt; "Please Select" And subdivision &lt;&gt; "Please Select" And classification &lt;&gt; "Please Select" And subclassification &lt;&gt; "Please Select" Then
              CheckMasterTblIfExist = CheckRecord("SELECT MASTERID, JOBTITLE FROM EMP_MASTERTBL WHERE JOBTITLE = '" &amp; jobtitle &amp; "' AND MASTERID = '" &amp; emp_id &amp; "' ")
              CheckGroupTblIfExist = CheckRecord("SELECT * FROM EMPGROUP_TBL WHERE BUSINESS_UNIT = '" &amp; businessunit &amp; "' AND DIVISION = '" &amp; division &amp; "' AND SUB_DIVISION = '" &amp; subdivision &amp; "' AND CLASSIFICATION = '" &amp; classification &amp; "' AND SUB_CLASSIFICATION = '" &amp; subclassification &amp; "' AND MASTERID = '" &amp; emp_id &amp; "' AND SEQID = '" &amp; seqid &amp; "'")
              If Not CheckMasterTblIfExist And CheckGroupTblIfExist Then
                  insrtResult = UpdateInsDelRecord("UPDATE EMP_MASTERTBL SET JOBTITLE = '" &amp; jobtitle &amp; "' " &amp; _
                                                   "WHERE MASTERID = '" + Me.lblval_Empid.Text + "'" &amp; _
                                                   ";" &amp; _
                                                   "INSERT INTO EMPGROUP_TBL(MASTERID, BUSINESS_UNIT, " &amp; _
                                                   "DIVISION, SUB_DIVISION, CLASSIFICATION, SUB_CLASSIFICATION) VALUES " &amp; _
                                                   "('" &amp; HandleQuote(Me.lblval_Empid.Text) &amp; "', " &amp; _
                                                   "'" &amp; businessunit &amp; "' ," &amp; _
                                                   "'" &amp; division &amp; "' ," &amp; _
                                                   "'" &amp; subdivision &amp; "' ," &amp; _
                                                   "'" &amp; classification &amp; "' ," &amp; _
                                                   "'" &amp; subclassification &amp; "')")
  
                  If Not insrtResult Then
                      MessageBox("alert('Error Ocurred While Inserting a Data.')")
                  Else
                      MessageBox("alert('Successfully Added.')")
                  End If
              Else
                  MessageBox("alert('Data Already Exist.')")
              End If
          End If
      Catch ex As Exception
          MessageBox("Error Ocurred while Inserting a data")
          Throw
      End Try
  End Function
  </code></pre>
  
  <p>I haven't been completed the code yet. I'm in the adding if there's no data and my problem is that the messagebox keeps on telling me that the data already exist even if there's still no employee's group details that added. Please help me with this.</p>