-2

button save in my project is not working properly, i always get information "your data is not yet complete" here is my syntax:

Dim tambah As String = "insert into Anggota values('" & Tno.Text & "','" & Tnis.Text & "','" & Tnama.Text & "','" & Tkelas.Text & "','" & CBjk.Text & "','" & Tt4lahir.Text & "','" & ttgllahir.Text & "')"
    Call koneksi() 
    cmd = New OleDbCommand(tambah, conn)
    Try
        cmd.ExecuteNonQuery()
        MsgBox("the data has been saved", MsgBoxStyle.Information, "INFORMASI")
        Call kosongkan()
    Catch ex As Exception
        MsgBox("your data is not yet complete!!!!", MsgBoxStyle.Information, "INFORMASI")
    End Try
    Call tampilkan()
    Tnis.Focus()

and here is my connection

Public Sub koneksi()
        str = "provider=microsoft.jet.oledb.4.0;data source=Database.mdb"
        conn = New OleDbConnection(str)
        ds = New DataSet
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If
    End Sub
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
MNFS
  • 305
  • 4
  • 17

1 Answers1

1

To determine what the actual error is, you should print out the Exception message:

Try
     .....
Catch ex As Exception
    MessageBox.Show("An exception occurred:" & Environment.NewLine & ex.Message)
End Try

It will usually be either permissions or an incorrect connection string, or a syntax error in your SQL command (which seems likely as you are concatenating values. Don't! Use a parameterised query)

BTW: as a matter of good practice you should not catch and swallow exceptions unless you are handling them in some sensible way. At a minimum you should log and re-throw. Also: you should catch specific exceptions (in this case SqlException)

Exception Handling

Best Practices for Exceptions

[Also: I note that you were warned about SQL injection attacks and NOT to concatenate values in your previous question: Syntax error in INSERT INTO statement (vb.net)

Community
  • 1
  • 1
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541