Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've some problem in execute the record in vb.net. I'm using microsoft access 2007 as database.

I run my project and insert a record, I press the button "save" and my record shown in gridview, then I stop running my project.
When I run the project again, the record that I just insert in gridview was gone.

What's the problem??

This my syntax:

Imports System.Data
Imports System.Data.OleDb
Public Class OlahData
    Dim con As OleDbConnection
    Dim cmd As OleDbCommand
    Dim reader As OleDbDataReader
    Dim constring As String
    Dim SqlString As String
    Dim numAffected As Integer
    Private Sub OlahData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Application.Info.DirectoryPath & "/FKIP.mdb ;Persist Security Info=True"
        con = New OleDbConnection(constring)
        con.Open()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SqlString = "Insert Into MataKuliah values ('" & id.Text.Trim & "','" & nama.Text.Trim & "','" & jurusan.Text.Trim & "','" & Val(sks.Text) & "')"
        cmd = New OleDbCommand(SqlString, con)
        numAffected = cmd.ExecuteNonQuery
        MessageBox.Show(numAffected.ToString & "record berhasil di simpan")
    End Sub
    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Application.Info.DirectoryPath & "/FKIP.mdb;"
        Dim Conn As OleDbConnection
        Dim Ds As DataSet
        Dim Da As OleDbDataAdapter
        Dim SQL As String = "Select * From MataKuliah"
        Try
            Conn = New OleDbConnection(ConnStr)
            Ds = New DataSet
            Da = New OleDbDataAdapter(SQL, Conn)
            Da.Fill(Ds, "MataKuliah")

            gridview.DataSource = Ds.Tables("MataKuliah")
            gridview.Dock = DockStyle.Fill
        Catch
            MessageBox.Show("tabel tidak dapat ditampilkan....!", "Database error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Me.Close()
        End Try
    End Sub    
End Class

[edit]Code block added, "Ignore HTML..." option deselected - OriginalGriff[/edit]
Posted
Updated 28-Jan-11 0:29am
v2
Comments
Sunasara Imdadhusen 28-Jan-11 6:39am    
I am not sure but try to store your .mdb file at any location except your project directory. e.g. (C:\program file\test.mdb).
Abdi tombang 28-Jan-11 6:43am    
Hmm.. I have try that way, but it still not work :(

If you look at my code, did you feel my code was right??
Or, did I miss something?
Rupa1 28-Jan-11 7:00am    
may be u r giving connection wrong coding insert everything f9
Rupa1 28-Jan-11 7:02am    
string connectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\serversmsdb.accdb"; try this connection string
Abdi tombang 28-Jan-11 7:38am    
oke, I'll try :D

Do you know about genetic algorithm in vb.net??

When you stop and start a project, you'll lose the view state data on your gridview.

You need to load up the data again, which it loks like you're doing when you click Button2_Click.

Add the following to perform the databind.

gridview.DataSource = Ds.Tables("MataKuliah")
gridview.DataBind();


http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx[^]

Click your button2, is the data loading OK now?

If you want it to autoload, you'll need to call this code when your screen loads


Other than this, I can see lots of other problems with your code!
 
Share this answer
 
Comments
Abdi tombang 28-Jan-11 6:52am    
I've try this one.
But, the record wasn't saving in database(FKIP.mdb)
And the gridview just show the record once (when I insert the record)
for the secong time, the record that I just insert was gone (not saving in FKIB.mdb)

what's the problem?
If you have included your mdb file in project, make sure that (in properties) Copy to Output Directory is not set to Copy Always.
Copy if newer seems a good option there.

If you use Copy Always, it will copy the database from project directory (which is possibly empty) to output directory.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900