title: How to solve vb.net database connection error try catch database?
body: <p>I try to register a user but when i want to save then the connection goes to 'catch' rather than 'try'.
  I can debug the program but the data not save in the database..can anyone tell me what is the problem? Thank you.</p>
  
  <pre><code>Public Class DTRCLASS
  Dim mDataPath As String
  Public Shared DataModule As DTRCLASS
  
  Private Function GetConnection() As OleDb.OleDbConnection
  
      Return New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" &amp; mDataPath &amp; "\dtrdb.accdb")
  
  End Function
  
  Public Overloads Function SaveMEM() As DataSet
      Return Me.SaveMEM("MEM_CODE")
  End Function
  
  Public Overloads Function SaveMEM(ByVal sortfield As String) As DataSet
      Dim a As String
      Dim b As String
      b = Form1.Label16.Text &amp; " " &amp; Form1.Label15.Text
  
      a = Form1.txtlast.Text &amp; ", " &amp; Form1.txtfirst.Text &amp; " " &amp; Form1.txtmi.Text &amp; "."
  
      Dim conn As OleDb.OleDbConnection = GetConnection()
      Dim SQL As String
  
      Try
          SQL = "INSERT INTO MEMBERS_PROF ( MEM_CODE, LOG_ID, NAME, COURSE_POSITION,GENDER," &amp; _
          "ENCODER, DATE_ENCODED )VALUES ( @MEM_CODE, @LOG_ID, @NAME, @COURSE_POSITION,@GENDER,@ENCODER, @DATE_ENCODED);"
  
  
          Dim cmd As New OleDb.OleDbCommand(SQL, conn)
          cmd.Parameters.Add(New OleDb.OleDbParameter("@MEM_CODE", Form1.txtid.Text))
          cmd.Parameters.Add(New OleDb.OleDbParameter("@LOG_ID", Form1.txtpin.Text))
          cmd.Parameters.Add(New OleDb.OleDbParameter("@NAME", a))
          cmd.Parameters.Add(New OleDb.OleDbParameter("@COURSE_POSITION", Form1.txtpos.Text))
          cmd.Parameters.Add(New OleDb.OleDbParameter("@GENDER", Form1.cbogen.SelectedItem))
          cmd.Parameters.Add(New OleDb.OleDbParameter("@ENCODER", Form1.GuestToolStripMenuItem.Text))
          cmd.Parameters.Add(New OleDb.OleDbParameter(" @DATE_ENCODED", b))
  </code></pre>
  
  <blockquote>
    <p>this <code>cmd.ExecuteNonQuery()</code> 
    when debug it always get the message "Error in connection, ID or PIN already exist" <a href="https://i.stack.imgur.com/KYls5.png" rel="nofollow noreferrer">error message when i try debug using the coding 'MsgBox(ex.ToString')</a></p>
  </blockquote>
  
  <pre><code>        conn.Open()
          cmd.ExecuteNonQuery()
  
  
  
          MessageBox.Show("New Member is added!")
          clear()
          'RefreshDGV()
  
  
      Catch ex As Exception
  
          MsgBox("Error in connection! ID or PIN already exist!")
      Finally
          conn.Close()
          conn.Dispose()
      End Try
  
  End Function
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ZP39z.png" rel="nofollow noreferrer">Line 91</a></p>