Click here to Skip to main content
14,430,378 members

Im having a problem with my coding

Wafiqque asked:

Open original thread
Hi, im new to VB.net, im trying to insert my data to the database but i seem to keep getting error. What did i do wrong?

Imports System.Data.OleDb

Public Class frmRegister

    Dim provider As String
    Dim datafile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection
    Dim gender As String
    Dim age As Int32

    Private Sub BtnReg_Click(sender As Object, e As EventArgs) Handles btnReg.Click
        If txtFName.Text = "" Or txtUsername.Text = "" Or txtPassword.Text = "" Or txtEmail.Text = "" Then
            MsgBox("Please fill in the info")
        Else

            If radFemale.Checked Then
                gender = "Female"
            ElseIf radMale.Checked Then
                gender = "Male"
            End If

            age = DateTime.Today.Year - dateAge.Value.Year

            provider = "Provider=Microsoft.JET.OLEDB.4.0;Data Source="
            datafile = "C:\Users\User\Desktop\Part 5\CSC 301\Project\Flight\test.mdb"
            connString = provider & datafile
            myConnection.ConnectionString = connString
            myConnection.Open()
            Dim str As String
            str = "Insert into User ([Username],[Password],[Full Name],[Email],[Gender],[Birthdate],[Age]) Values (?,?,?,?,?,?,?) "
            Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
            cmd.Parameters.Add(New OleDbParameter("Username", CType(txtUsername.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Password", CType(txtPassword.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Full Name", CType(txtFName.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Email", CType(txtEmail.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("Gender", CType(gender, String)))
            cmd.Parameters.Add(New OleDbParameter("Birthdate", CType(dateAge.Value, String)))
            cmd.Parameters.Add(New OleDbParameter("Age", CType(age, String)))

            Try
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                myConnection.Close()
                txtEmail.Clear()
                txtFName.Clear()
                txtPassword.Clear()
                txtUsername.Clear()
                MsgBox("create success")
            Catch ex As Exception
                MsgBox("Error")
            End Try

        End If
    End Sub
End Class


What I have tried:

I changed my coding from a simpler way to connect it to the database to these, but stills getting the error
Tags: VB.NET

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100