2 Replies - 134 Views - Last Post: 31 January 2018 - 12:33 AM Rate Topic: -----

#1 Andre'  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-January 18

i have a vb.net access database problem when inserting data please hel

Posted 30 January 2018 - 09:37 PM

Imports System.Data.OleDb
Public Class Form2
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim provider As String
        Dim DataFile As String
        Dim Conn As String
        Dim MyConn As OleDbConnection = New OleDbConnection
        Dim UserID As Integer = TextBox4.Text
        Dim FName As String = TextBox1.Text
        Dim MName As String = TextBox2.Text
        Dim LName As String = TextBox3.Text

        Try
            provider = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source ="
            DataFile = "C:\Users\Eng.Drew\Documents\systemDB2.mdb"
            Conn = provider & DataFile
            MyConn.ConnectionString = Conn
            MyConn.Open()


            MyConn.Close()
            Dim InsertQuery As String
        InsertQuery = "INSERT INTO User (UserID,Fname,MName,LName,Gender) VALUES ('"UserID"','"&TextBox1.Text &"','"&TextBox2.Text &"','"&TextBox3.Text &"','"&RadioBox1.Checked&"') "

            MyConn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(InsertQuery, MyConn)

            cmd.Parameters.AddWithValue("@UserID", Data.SqlDbType.NVarChar).Value = TextBox4.Text()
            cmd.Parameters.AddWithValue("@FName", Data.SqlDbType.NVarChar).Value = TextBox1.Text()
            cmd.Parameters.AddWithValue("@MName", Data.SqlDbType.NVarChar).Value = TextBox2.Text()
            cmd.Parameters.AddWithValue("@LName", Data.SqlDbType.NVarChar).Value = TextBox3.Text()
            cmd.Parameters.AddWithValue("@Gender", Data.SqlDbType.NVarChar).Value = RadioButton1.Checked()
            'cmd.Parameters.AddWithValue("@course", CourseTextBox.Text)
            'cmd.Parameters.AddWithValue("@section", SectionTextBox.Text)

            cmd.ExecuteNonQuery()
            'MyConn.Close()
            MessageBox.Show("SAVED.")
            'Form3.Show()
        Catch ex As Exception
            MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
        Finally
            MyConn.Close()
        End Try


    End Sub
End Class

This post has been edited by andrewsw: 30 January 2018 - 10:35 PM
Reason for edit:: added missing [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: i have a vb.net access database problem when inserting data please hel

#2 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6495
  • View blog
  • Posts: 26,279
  • Joined: 12-December 12

Re: i have a vb.net access database problem when inserting data please hel

Posted 30 January 2018 - 10:36 PM

What problem? Where? What is your question?

Please wrap your code in code tags, I have added them for you this time.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • blow up my boots
  • member icon

Reputation: 6495
  • View blog
  • Posts: 26,279
  • Joined: 12-December 12

Re: i have a vb.net access database problem when inserting data please hel

Posted 31 January 2018 - 12:33 AM

In the meantime, you attempt to use parameters, but your SQL statement ignores them and embeds variables directly. Investigate how to use the parameters.

Also, User is a reserved word in Access. Surround it with square brackets or, preferably, change the field-name.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1