0

Hey , am currently doing my school project of coming up with school reports of learners but when I tried to connect my database with the ms vb it gave me an insert into error whil I was trying to register learners .Please help.

2
Contributors
3
Replies
20
Views
2 Weeks
Discussion Span
Last Post by Reverend Jim
0
Imports System.Data.OleDb

Public Class Registetration_of_learners
    Dim provider As String
    Dim dataFile As String
    Dim connString As String
    Public myConnection As OleDbConnection = New OleDbConnection
    Public dr As OleDbDataReader

    Dim learnername As String
    Dim learnersurname As String
    Dim dateofbirth As Date
    Dim gender As String
    Dim studentno As Integer
    Dim grade As String

    Private Sub Registration_of_learners_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
        dataFile = "C:\Users\nnashipili\Documents\Visual Studio 2010\Projects\Reports Management System\WindowsApplication3\Podheni_Matete.accdb"
        connString = provider & dataFile
        myConnection.ConnectionString = connString
    End Sub

    Private Sub RRegisterButton_Click(sender As Object, e As System.EventArgs) Handles RRegisterButton.Click

        myConnection.Open()
        Dim str As String
        str = "INSERT INTO Learners ([Registration_number], [Learner's_name], [Learner's_surname], [Date_Of_Birth], [Grade],[Gender],) values ([studentno], [learnername], [learnersurname], [dateofbirth], [grade],[Gender])"

        Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)

        cmd.Parameters.Add(New OleDbParameter("studentno", CType(RStudentNo_TextBox.Text, Integer)))
        cmd.Parameters.Add(New OleDbParameter("learnername", CType(RNameTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("learnersurname", CType(RSurnameTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("dateofbirth", CType(RDOBTextBox.Text, Date)))
        cmd.Parameters.Add(New OleDbParameter("grade", CType(RGradeTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("Gender", CType(RGenderComboBox.SelectedItem, String)))

        Try
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            MessageBox.Show("Successfully Registered!")
            RNameTextBox.Clear()
            RSurnameTextBox.Clear()
            RGradeTextBox.Clear()
            RGenderComboBox.Text = String.Empty
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        myConnection.Close()
    End Sub

    Private Sub RGenderComboBox_Click(sender As Object, e As System.EventArgs) Handles RGenderComboBox.Click
        RGenderComboBox.Items.Clear()
        RGenderComboBox.Items.Add("Male")
        RGenderComboBox.Items.Add("Female")
    End Sub
    Private Sub RBackButton_Click(sender As Object, e As System.EventArgs) Handles RBackButton.Click
        Activity_selection.Show()
        Me.Close()
    End Sub

End Class
1

Typically, when you do an INSERT you include some values to go with the field names. For example

INSERT INTO mytable (LastName,FirstName,Age) VALUES('Smith','George',27)

Your query didn't do this.

I'll also add that you should be using Parameterized Queries.

Edited by Reverend Jim

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.