Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am only a month old to VB.NET. I'm stuck with this problem which doesn't let me save the entry to the database. Previously, there was no problem, but the problem started when I added another column in the database. It saves the data on run-time,but when we restart the program, there is no data.I use MS Access 2010 as database. It is too late to Undo changes. How can I sort this out?

Here is part of the code:
VB
Imports System.Security.Cryptography
Public NotInheritable Class Form1


Private TripleDes As New TripleDESCryptoServiceProvider
    Private Function TruncateHash(
    ByVal key As String,
    ByVal length As Integer) As Byte()

        Dim sha1 As New SHA1CryptoServiceProvider

        ' Hash the key.
        Dim keyBytes() As Byte =
            System.Text.Encoding.Unicode.GetBytes(key)
        Dim hash() As Byte = sha1.ComputeHash(keyBytes)

        ' Truncate or pad the hash.
        ReDim Preserve hash(length - 1)
        Return hash
    End Function
    Public Sub New(ByVal key As String)
        ' Initialize the crypto provider.
        InitializeComponent()
        TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
        TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
    End Sub
    Public Function EncryptData(
    ByVal plaintext As String) As String

        ' Convert the plaintext string to a byte array.
        Dim plaintextBytes() As Byte =
            System.Text.Encoding.Unicode.GetBytes(plaintext)

        ' Create the stream.
        Dim ms As New System.IO.MemoryStream
        ' Create the encoder to write to the stream.
        Dim encStream As New CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)

        ' Use the crypto stream to write the byte array to the stream.
        encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
        encStream.FlushFinalBlock()

        ' Convert the encrypted stream to a printable string.
        Return Convert.ToBase64String(ms.ToArray)
    End Function
    Sub New()
        ' TODO: Complete member initialization 
        InitializeComponent()
    End Sub


    Private Sub Add_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'PassMgrDataSet.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.PassMgrDataSet.Table1)
        'TODO: This line of code loads data into the 'PassMgrDataSet1.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.PassMgrDataSet.Table1)
        'TODO: This line of code loads data into the 'PassMgrDataSet.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.PassMgrDataSet.Table1)

    End Sub

    Private Sub BtnAddEnc_Click(sender As Object, e As EventArgs) Handles BtnAddEnc.Click
        TestEncoding()
    End Sub
    Sub TestEncoding()
        Dim plainText As String = lblPassword.Text
        Dim wrapper As New Form1()
        Dim cipherText As String = wrapper.EncryptData(plainText)
        lblPassword.Text = cipherText
        My.Computer.FileSystem.WriteAllText(
            My.Computer.FileSystem.SpecialDirectories.MyDocuments &
            "\cipherText.txt", cipherText, False)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Table1TableAdapter.Fill(Me.PassMgrDataSet.Table1)
        Me.Table1TableAdapter.Fill(Me.PassMgrDataSet.Table1)
    End Sub

    Private Sub EncBtnEdit_Click(sender As Object, e As EventArgs) Handles EncBtnEdit.Click
        Edit_Window.Show()
    End Sub

    Private Sub btnaddnew_Click(sender As Object, e As EventArgs) Handles btnaddnew.Click
        Table1BindingSource.AddNew()
        AddAccName.Text = ""
        AddEID.Text = ""
        AddPass.Text = ""
        lblPassword.Text = ""
    End Sub

    Private Sub EncBtnDel_Click(sender As Object, e As EventArgs) Handles EncBtnDel.Click
        If Table1BindingSource.Count = vbEmpty Then
            MsgBox("No more fields to delete!", +vbInformation)
        Else
            Table1BindingSource.RemoveCurrent()
            Table1TableAdapter.Update(PassMgrDataSet.Table1)
        End If
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        EnDe.Show()
        Me.Hide()
    End Sub

    Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles btnsave.Click
        If lblPassword.Text = "" Then
            MsgBox("Your password requires Security! Please Re-Enter Data, Click Encrypt and then proceed.", +vbCritical)
            Table1BindingSource.RemoveCurrent()
            btnback.PerformClick()
        Else
            Table1BindingSource.EndEdit()
            Table1TableAdapter.Update(PassMgrDataSet.Table1)
            DataGridView1.Refresh()
        End If
    End Sub

End Class


For form preview image
http://stackoverflow.com/questions/23180354/unable-to-update-access-database-vb-net[^]
Posted
Updated 20-Apr-14 4:09am
v2

1 solution

Normally, this kind of problem is not in the code: it's that the access file is being overwritten when you rebuild your project. You don't show where you connect to your DB, so it's difficult to tell, but the chances are you have your DB in the "bin" folder with your application.

So check if your Access database in included in your project files, and if so, remove it.
Move the DB to a more sensible folder (the application binary folder is not a good place: it's generally read only in release versions and only works in development).

See here: Where should I store my data?[^] - the code is C#, but it's pretty simple and you should get the idea straight away.
 
Share this answer
 
Comments
Rodney Mathew 20-Apr-14 13:07pm    
Just changed the location of database. Now it works! Thank you for your valuable suggession
OriginalGriff 20-Apr-14 13:45pm    
You're welcome!

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