INSERT MICROSOFT ACCESS TABLE USING VB.NET

The following example illustrates how to insert access table using vb.net

 

Use the following code to import namespace

 

<%@ Import namespace="System.Data" %>

<%@ Import namespace="System.Data.OleDb" %>

 

Use the following code to add web controls such as labels and textboxes.

 

<html>

  <head>

    <title>Validating a Field</title>

  </head>

  <body>

    <form id="Form1" method="post" runat="server">

      <table id="Table1"

             style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px"

             cellSpacing="0" cellPadding="0" width="300" border="0">

        <tr>

          <td style="WIDTH: 115px">

            <asp:Label id="Label1" runat="server">EMPNO</asp:Label>

          </td>

          <td>

            <asp:TextBox id="txtEMPNO" runat="server" width="193" />

          </td>

        </tr>

        <tr>

          <td style="WIDTH: 115px">

            <asp:Label id="Label2" runat="server">NAME</asp:Label>

          </td>

          <td>

            <asp:TextBox id="txtNAME" runat="server" width="193" />

          </td>

        </tr>

        <tr>

          <td style="WIDTH: 115px" colSpan="2">

            <asp:Button id="btnInsert" runat="server"

                 OnClick="btnInsert_Click" width="298" text="INSERT!" />

          </td>

        </tr>

      </table>

      <asp:RequiredFieldValidator id="valEMPNO" runat="server"

          style="Z-INDEX: 102; LEFT: 316px; POSITION: absolute; TOP: 14px"

          ErrorMessage="Please insert the new EMPNO"

          ControlToValidate="txtEMPNO" />

    </form>

  </body>

</html>

 

Use the following code to connect access database

 

<script language="VB" runat="server">

Sub btnInsert_Click(Sender As Object, E As EventArgs)

Dim objConnection As OleDbConnection

objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & server.mappath("emp.mdb"))

Dim strSQL As String = "INSERT INTO Emp(EMPNO, NAME) VALUES (txtEMPNO.Text, txtNAME.Text)"

Dim dbComm As New OleDbCommand(strSQL, objConnection)

dbComm.Parameters.Add("EMPNO", OleDbType.VarChar, 40, "EMPNO")

dbComm.Parameters.Add("NAME", OleDbType.VarChar, 40, "NAME")

dbComm.Parameters("EMPNO").Value = txtEMPNO.Text

dbComm.Parameters("NAME").Value = txtNAME.Text

 

Use the following code to execute the command

 

Try

      objConnection.Open()

      dbComm.ExecuteNonQuery()

    Catch ex As Exception

      Response.Write(ex.Message)

      Response.End

    Finally

      If objConnection.State = ConnectionState.Open Then

        objConnection.Close()

      End If

    End Try

  

The following code indicate that new record is added

 

    Response.Write("A new record has been added")

    Response.End

  

End Sub

</script>

          

‘COMPLETE CODE IS AS FOLLOWS:

 

      <%@ Import namespace="System.Data" %>

<%@ Import namespace="System.Data.OleDb" %>

 

<html>

  <head>

    <title>Validating a Field</title>

  </head>

  <body>

    <form id="Form1" method="post" runat="server">

      <table id="Table1"

             style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px"

             cellSpacing="0" cellPadding="0" width="300" border="0">

        <tr>

          <td style="WIDTH: 115px">

            <asp:Label id="Label1" runat="server">EMPNO</asp:Label>

          </td>

          <td>

            <asp:TextBox id="txtEMPNO" runat="server" width="193" />

          </td>

        </tr>

        <tr>

          <td style="WIDTH: 115px">

            <asp:Label id="Label2" runat="server">NAME</asp:Label>

          </td>

          <td>

            <asp:TextBox id="txtNAME" runat="server" width="193" />

          </td>

        </tr>

        <tr>

          <td style="WIDTH: 115px" colSpan="2">

            <asp:Button id="btnInsert" runat="server"

                 OnClick="btnInsert_Click" width="298" text="INSERT!" />

          </td>

        </tr>

      </table>

      <asp:RequiredFieldValidator id="valEMPNO" runat="server"

          style="Z-INDEX: 102; LEFT: 316px; POSITION: absolute; TOP: 14px"

          ErrorMessage="Please insert the new EMPNO"

          ControlToValidate="txtEMPNO" />

    </form>

  </body>

</html>

 

<script language="VB" runat="server">

 

 

Sub btnInsert_Click(Sender As Object, E As EventArgs)

Dim objConnection As OleDbConnection

objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & server.mappath("emp.mdb"))

Dim strSQL As String = "INSERT INTO Emp(EMPNO, NAME) VALUES (txtEMPNO.Text, txtNAME.Text)"

Dim dbComm As New OleDbCommand(strSQL, objConnection)

dbComm.Parameters.Add("EMPNO", OleDbType.VarChar, 40, "EMPNO")

dbComm.Parameters.Add("NAME", OleDbType.VarChar, 40, "NAME")

dbComm.Parameters("EMPNO").Value = txtEMPNO.Text

dbComm.Parameters("NAME").Value = txtNAME.Text

Try

      objConnection.Open()

      dbComm.ExecuteNonQuery()

    Catch ex As Exception

      Response.Write(ex.Message)

      Response.End

    Finally

      If objConnection.State = ConnectionState.Open Then

        objConnection.Close()

      End If

    End Try

  

 

    Response.Write("A new record has been added")

    Response.End

  

End Sub

</script>