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

>Adding an entry to a database

To add a record into a database you need to use insert query.

Private Sub regBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles regBtn.Click
    ....
Dim conStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                       "Data Source=School1.accdb"

Dim cn as new OleDbConnection(conStr)
Dim cmd as new OleDbCommand("select [username] from [Users] where [username]=@username",cn)

cmd.Parameters.AddWithValue("@username",usernameText.Text)

cn.Open()
Dim obj as Object=cmd.ExecuteScalar()
cn.Close()

If IsNothing(obj) Then 'Username is not found in the database
     cmd=new OledbCommand("INSERT INTO [Users] values (@id,@username,@password,@type)",cn)
    cmd.Parameters.AddWithValue("@id",somevalue.Text)
    cmd.Parameters.AddWithValue("@username",somevalue1.Text)
    cmd.Parameters.AddWithValue("@password",somevalue2.Text)
    cmd.Parameters.AddWithValue("@type",somevalue3.Text)

    cn.Open()
    cmd.ExecuteNonQuery()
    cn.Close()
else
   MsgBox("There is already a user with that name")
End if
....
End Sub
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.