i can't save my data into access using vb.net

Dim SQL As String

        DB.Open()
        Dim Ad As New OleDbDataAdapter("Select * From candidate", DB)
        Dim Check_dataset As DataSet
        Check_dataset = New DataSet("check")
        Ad.Fill(Check_dataset, "check")
        If Check_dataset.Tables("Check").Rows.Count <> 0 Then
            MsgBox("Certificate already exist.!!", MsgBoxStyle.Information, "")
            DB.Close()
            Exit Sub
        End If

        SQL = "INSERT INTO candidate(Cert_No, Cert_Exp_Date, Name, IC_Number, Company, State, Phone_no) values(" _
            & "'" & certNoText.Text & "', " _
            & "'" & DateTimePicker1.Text & "', " _
            & "'" & textName.Text & "', " _
            & "'" & icText.Text & "', " _
            & "'" & comText.Text & "', " _
            & "'" & AddText.Text & "', " _
            & "'" & PhoneText.Text & "')"

        Dc = New OleDbCommand(SQL, DB)
        MessageBox.Show("All data have been save.", " ", MessageBoxButtons.OK, MessageBoxIcon.Information)
        DB.Close()

*when i ru this program, MsgBox("Certificate already exist.!!", MsgBoxStyle.Information, "") will appear.. i don'r where is wrong

Recommended Answers

All 6 Replies

>i can't save my data into access using vb.net

I can not see any code that insert row into a database.

>when i ru this program, MsgBox("Certificate already exist.!!

Clarify in which circumstances you want to show this message.

Your IF statement is returning that message box if you have any records in your dataset table at all. Second your insert command is never even set to execute anything to the database. You really should start by learning how to step thru your code

Private Sub COnnect()

        Dim Nextb As Long
        Dim PrevB As Long
        Nextb = 0
        PrevB = 0
        Dim STrPath = System.IO.Directory.GetCurrentDirectory & "\zonehirarc.mdb"

        Try
            DB = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;data source='" & STrPath & "'")
            Dc = New OleDbCommand("Select * from candidate where Cert_No <> null")
            DB.Open()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "")
            End
        End Try
        Dc.Connection = DB
        DB.Close()
    End Sub

    Private Sub save()
        Dim SQL As String

        DB.Open()
        Dim Ad As New OleDbDataAdapter("Select * From candidate", DB)
        Dim Check_dataset As DataSet
        Check_dataset = New DataSet("check")
        Ad.Fill(Check_dataset, "check")
        If Check_dataset.Tables("Check").Rows.Count <> 0 Then
            MsgBox("Certificate already exist.!!", MsgBoxStyle.Information, "")
            DB.Close()
            Exit Sub
        End If

        SQL = "INSERT INTO candidate(Cert_No, Cert_Exp_Date, Name, IC_Number, Company, State, Phone_no) values(" _
            & "'" & certNoText.Text & "', " _
            & "'" & DateTimePicker1.Text & "', " _
            & "'" & textName.Text & "', " _
            & "'" & icText.Text & "', " _
            & "'" & comText.Text & "', " _
            & "'" & AddText.Text & "', " _
            & "'" & PhoneText.Text & "')"

        Dc = New OleDbCommand(SQL, DB)
        MessageBox.Show("All data have been save.", " ", MessageBoxButtons.OK, MessageBoxIcon.Information)
        DB.Close()
        COnnect()

    End Sub

Use parametrized query to check whether a certi_no is present in candidate table.

..
  Dim STrPath = System.IO.Directory.GetCurrentDirectory & "\zonehirarc.mdb"
  
  Dim DB as OleDbConnection
  Dim Cmd as New OleDBCommand
  
  DB = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;data source='" & STrPath & "'")

  cmd.CommandText="Select  Cert_no From candidate where Cert_no=@certno"
  cmd.Connection=DB
  cmd.Parameters.AddWithValue("@certno",certNoText.Text)
  
 Dim obj as Object 
 DB.Open()
 obj=cmd.ExecuteScalar()
 DB.Close()

 If IsNothing(obj)=false Then
       MsgBox("Certificate already exist.!!", MsgBoxStyle.Information, "")
       Exit Sub
 End If
..

'addWithValue is not a member of system?

>addWithValue is not a member of system?

It is not a member of System but it is a member (method) of OleDbCommand class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.