0

I am making a program in VB.NET that uses a database for a login system. I can create an account using the program and it appears in the database. I can log in to it as well. But if I close the program and re-run it, the data is gone from the database.

This is form 1 - the first form that the user sees. It's the login screen with 2 text boxes, a login button and a create account button.

Imports System.Data.OleDb

Public Class Form1
    Public connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=ProgramDatabase.accdb"
    Public conn As New OleDbConnection(connstring)

    Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
        FormCreate.Show()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        conn.Open()
        Dim SqlQuery As String = "SELECT * FROM tblLogin"
        Dim Da As OleDbDataAdapter = New OleDbDataAdapter(SqlQuery, conn)
        Dim ds As DataSet = New DataSet
        Da.Fill(ds, "LoginData")
        Dim Dt As DataTable = ds.Tables("LoginData")

        For Each row As DataRow In Dt.Rows
            If row.Item(1) = txtUserName.Text And row.Item(2) = txtPassword.Text Then
                MsgBox("Welcome.")
                Exit Sub
            End If
        Next
        MsgBox("Wrong.")
        conn.Close()
    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
        FormForgot.Show()
    End Sub
End Class

This is the code for the creating an account form.

Blackwood
  • 4,504
  • 16
  • 32
  • 41
Joepowell567
  • 11
  • 2
  • 4
  • 1
    Where is the code you posted located? This code does NOT create anything, and if 'FormCreate' is where your code is, you need to show that. Another suggestion: don't open the recordset of tblLogin and scroll thru until you find a match - use a query to select a record that matches your "txtUserName" then check if EOF, then check password....and while I'm being picky, it's a great idea to use descriptive names for objects/controls (i.e. cmdCheckSecurity instead of "Button1_Click" – Wayne G. Dunn Oct 12 '17 at 20:26
  • I didn't include the create account form because it couldn't paste correctly. However I set the database property to "copy if newer" from "copy always" and this seems to work. About the way my program searches for a match, this was the way a tutorial did it so being a beginner I stuck to that method though. – Joepowell567 Oct 12 '17 at 21:00
  • When you run your project in the IDE, this is expected behavior. The data WILL NOT persist in the database. – Prescott Chartier Oct 13 '17 at 16:40

0 Answers0