i am just developing a library management system as one of my project. i using vb.net 2010 and the MS Access 2007 as back end. when i save some records into my database through my application it is correctly saved and i confirmed it through opening the relevant table in the database. but when i restart the application all the entered data were missing.. help me to solve this
Ayavan 0 Newbie Poster
Alexpap 1 Junior Poster
Can you provide us with the functionality that fetches the data from your database?
Mitja Bonca 557 Nearly a Posting Maven
So this means 2 things:
- or you dont retreive the right data
- or you dont have the correctly.
Can we see some of your code that does these two things?
Ayavan 0 Newbie Poster
Imports System.Data
Imports System.Data.OleDb
Public Class Form4
Public con As New OleDb.OleDbConnection
Public ada1 As New OleDb.OleDbDataAdapter
Public cmd1 As New OleDb.OleDbCommandBuilder
Public dt1 As New DataTable
Public row As DataRow
Shared r As Integer
Dim up As String
Dim found As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("Book No Should be entered", MsgBoxStyle.Information, "SLMS")
ElseIf TextBox2.Text = "" Then
MsgBox("Book Name Should be entered", MsgBoxStyle.Information, "SLMS")
ElseIf TextBox3.Text = "" Then
MsgBox("Author Name Should be entered", MsgBoxStyle.Information, "SLMS")
ElseIf TextBox4.Text = "" Then
MsgBox("Publisher Name Should be entered", MsgBoxStyle.Information, "SLMS")
ElseIf TextBox5.Text = "" Then
MsgBox("Published Year Should be entered", MsgBoxStyle.Information, "SLMS")
ElseIf ComboBox1.Text = "" Then
MsgBox("Category Name Should be entered", MsgBoxStyle.Information, "SLMS")
ElseIf TextBox8.Text = "" Then
MsgBox("Quantity Should be entered", MsgBoxStyle.Information, "SLMS")
Else
Try
ada1 = New OleDb.OleDbDataAdapter("UPDATE books1 SET BookNo='" & TextBox1.Text & "', BookName='" & TextBox2.Text & "', AuthorName='" & TextBox3.Text & "', PublisherName='" & TextBox4.Text & "', PublishedYear='" & TextBox5.Text & "', Price='" & Val(TextBox6.Text) & "', CategoryName='" & ComboBox1.Text & "', Quantity='" & Val(TextBox8.Text) & "', ISBNNumber='" & TextBox9.Text & "', ClassificationNo='" & TextBox10.Text & "' WHERE BookNo='" & TextBox1.Text & "'", con)
cmd1 = New OleDb.OleDbCommandBuilder(ada1)
ada1.Fill(dt1)
MsgBox("Successfully Updated", , "Login")
Catch ex As Exception
End Try
End If
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
ComboBox1.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox10.Text = ""
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'Update button
Button1.Enabled = True
Button2.Enabled = True
If TextBox1.Text = "" Then
MsgBox("Book No Should be entered", MsgBoxStyle.Information, "SLMS")
Else
Try
up = TextBox1.Text
found = False
r = 0
Do While (r < dt1.Rows.Count)
If (up = dt1.Rows(r)("BookNo")) Then
found = True
'bind
TextBox1.Text = dt1.Rows(r)("BookNo")
TextBox2.Text = dt1.Rows(r)("BookName")
TextBox3.Text = dt1.Rows(r)("AuthorName")
TextBox4.Text = dt1.Rows(r)("PublisherName")
TextBox5.Text = dt1.Rows(r)("PublishedYear")
TextBox6.Text = dt1.Rows(r)("Price")
ComboBox1.Text = dt1.Rows(r)("CategoryName")
TextBox8.Text = dt1.Rows(r)("Quantity")
TextBox9.Text = dt1.Rows(r)("ISBNNumber")
TextBox10.Text = dt1.Rows(r)("ClassificationNo")
Exit Do
End If
r = r + 1
Loop
If Not found Then
MsgBox("Book not found", , "SLMS")
End If
Catch ex As Exception
MsgBox(ex.Message, , "SLMS")
End Try
End If
End Sub
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Enabled = False
Button2.Enabled = False
Try
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=libraryDB.accdb"
con.Open()
ada1 = New OleDb.OleDbDataAdapter("select * from books1", con)
cmd1 = New OleDb.OleDbCommandBuilder(ada1)
ada1.Fill(dt1)
Catch ex As Exception
MsgBox(ex.Message, , "Libraray")
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
If found = True Then
dt1.Rows(r).Delete()
ada1.Update(dt1)
MsgBox("Successfully Deleted", , "SLMS")
End If
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
ComboBox1.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
TextBox10.Text = ""
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Close()
End Sub
End Class
Ayavan 0 Newbie Poster
That is the full code i uded in the form which is used to modify or delete the details of the book. it works correctly and making the changes in the DB too but after few minutes Those records are missing from the database
Mitja Bonca 557 Nearly a Posting Maven
One thing to mention before even starting looking into this "mess" of the code.
Use parameterized query. Check URL="http://blog.divergencehosting.com/2009/04/09/using-parameters-parameterized-queries-database-interactions-cshar-vbnet/"][/UhereRL] what this means.
It basicly means that you dont specify textBoxes values directly in the query, but you add them in the command.
Next, it would be better to get all the data from DB into a dataTable, and then use dataBinding for each textBox. Check here what dataBinding is.
Take your time. Its not so easy task to do, especially if you didnt do it before.
If you have any issues with C# code there, use C# to VB converter.
Hope it helps,
bye
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.