Link to home
Create AccountLog in
Avatar of goldsmithuni
goldsmithuni

asked on

Problem Inserting record into a Microsoft Access Database with VB.net

hi all,

im new to .net
and im using visual studio .net

IM having problems INSERTING text from 3 textbox  into MICROSOFT ACCESS DATABASE via vb.net.

im following this website tutorial-----  http://www.startvbdotnet.com/ado/msaccess.aspx

i created the DATABASE just like the tutorial.

However my problem is that my script does not recognise ( Inherits System.Windows.Forms.Form ) !!!!!!!
and hence does not recognise ( messabox.show ) !!!!!

what is more visual studio produces an extra line in my code witch i have to use in order for textboxes to be visible
( Inherits System.Web.UI.Page ) which is NOT USED BY THE WEBSITE EXAMPLE

heres my code


Imports System.Data.OleDb
Public Class WebForm1 Inherits System.Windows.Forms.Form -------------NOT RECOGNISED
    Inherits System.Web.UI.Page    ---- !!!!!!   EXTRA LINE OF CODE NOT IN THEIR TUTORIAL EXAMPLE BUT AUTOMATICALLY
                                                                 PRODUCED IN MY ONE BY VS.NET
    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim icount As Integer
    Dim str As String

    Protected WithEvents EmpNo As System.Web.UI.WebControls.TextBox
    Protected WithEvents EName As System.Web.UI.WebControls.TextBox
    Protected WithEvents Department As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\emp.mdb;")
            cn.Open()
            str = "insert into table1 values(" & CInt(EmpNo.Text) & ",'" & EName.Text & "','" & Department.Text & "')"
            'string stores the command and CInt is used to convert number to string
            cmd = New OleDbCommand(str, cn)
            icount = cmd.ExecuteNonQuery
            MessageBox.Show(icount)  ------------------NOT RECOGNISED
            'displays number of records inserted
        Catch
        End Try
        cn.Close()
    End Sub
End Class


please help
ASKER CERTIFIED SOLUTION
Avatar of bman9111
bman9111

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
By the looks of things, you are using  a web from not a win form

Hence this doesnt work
Inherits System.Windows.Forms.Form

and this line comes along
Inherits System.Web.UI.Page

and therefore, the is no messagebox.. Use something like Response.Write for testing???

The rest looks ok..

I would generally recomened you would add the field names to the SQL too

like

str = "insert into table1 ( EmpID, EmpName, EmpDepartment ) values(" & CInt(EmpNo.Text) & ",'" & EName.Text & "','" & Department.Text & "')"

Dave :-)
Avatar of amyhxu
amyhxu

Can't tell if you are developing a windows form or a web form.
If you want to develop a win form, you should select "Windows Application" when you create a new project and the auto generated code should be:
    Public Class Form1
            Inherits System.Windows.Forms.Form
            ...
and the problems should go away.

If you want to develop a web form, you should select  "ASP .NET Web Application" and the auto generated code is:
    Public Class WebForm1
            Inherits System.Web.UI.Page
            ...
and of course you cannot use MessageBox.Show to display a message like that in the win forms.