Hi I'm doing a project where I need to have people register by entering a username and password. When they click register I want them to be added to the database, but I'm having trouble doing this. Can anyone help? This is my first time using VB.
This is the code to connect to the database:
Private Sub RegisterForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim conStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ "Data Source=School1.accdb" Dim sqlStr As String = "SELECT * FROM Users" Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, conStr) dataAdapter.Fill(dbTable) dataAdapter.Dispose() End Sub
This is the bit I'm stuck with. The message boxes are just there so I can see what's going on:
Private Sub regBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles regBtn.Click Dim user As String = usernameTxt.Text Dim found As Boolean Try For i As Integer = 0 To (dbTable.Rows.Count - 1) If CStr(dbTable.Rows(i)("Username")) = user Then found = True MsgBox("There is already a user with that name") Else Dim dbTable As DataTable = New DataTable("Users") Dim row As DataRow Dim dataSet As DataSet dataSet = New DataSet dataSet.Tables.Add(dbTable) row = dbTable.NewRow() row("ID") = dbTable.Rows.Count row("Username") = user row("Password") = password1Txt.Text row("Type") = "Student" dbTable.Rows.Add(row) End If Next MsgBox("Added") Catch MsgBox("Not Added") End Try End Sub