Efficient way to Save or Insert TextBox Value into MS Access DataBase Table In Asp.net Using C#.Net VB.Net

We can use MS Access Database for small scale applications. Asp.net Ado.Net provides us an easy, efficient way to develop Database driven application using MS Access as back end. To start an application here I have described “How to Insert or Save TextBox Data into MS Access Database Table in an efficient way“. Hope this example will help you to go further.

To do the Example create the below Table:
Save or Insert TextBox Value into MS Access DataBase Table In Asp.net Using Csharp 3

Design the aspx page like below:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table>
    <tr><td><b>Product Name:</b></td><td><asp:TextBox ID="txtProductName" runat="server"></asp:TextBox></td></tr>
    <tr><td><b>Product Description:</b></td><td><asp:TextBox ID="txtProductDescription" runat="server"></asp:TextBox></td></tr>
    <tr><td colspan="2"><asp:Label ID="lblInfo" runat="server" Font-Bold="True"></asp:Label></td></tr>
    <tr><td><asp:Button ID="cmdSave" runat="server" Text="Save" onclick="cmdSave_Click" /></td><td></td></tr>
</table>
</asp:Content>

Save or Insert TextBox Value into MS Access Table:
C# Code:

    protected void cmdSave_Click(object sender, EventArgs e)
    {
        string sFilePath = Server.MapPath("Database3.accdb");
        OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
        string insertCmd = "INSERT INTO Product_Info(Name,Description) VALUES (@Name,@Description)";
        using (Conn)
        {
            Conn.Open();
            OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn);
            // Create parameters for the SqlCommand object
            // initialize with input-form field values
            myCommand.Parameters.AddWithValue("@Name", txtProductName.Text);
            myCommand.Parameters.AddWithValue("@Description", txtProductDescription.Text);
            myCommand.ExecuteNonQuery();
            lblInfo.Text = "Data Inserted or Saved Successfully !!";
            lblInfo.ForeColor = System.Drawing.Color.Green;            
        }
    }

Note: Don’t forget to add “System.Data.SqlClient” and “System.Configuration” namespaces.

VB.Net Code:

    Protected Sub cmdSave_Click(sender As Object, e As EventArgs)
        Dim sFilePath As String = Server.MapPath("Database3.accdb")
        Dim Conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sFilePath & ";Persist Security Info=False;")
        Dim insertCmd As String = "INSERT INTO Product_Info(Name,Description) VALUES (@Name,@Description)"
        Using Conn
            Conn.Open()
            Dim myCommand As New OleDbCommand(insertCmd, Conn)
            ' Create parameters for the SqlCommand object
            ' initialize with input-form field values
            myCommand.Parameters.AddWithValue("@Name", txtProductName.Text)
            myCommand.Parameters.AddWithValue("@Description", txtProductDescription.Text)
            myCommand.ExecuteNonQuery()
            lblInfo.Text = "Data Inserted or Saved Successfully !!"
            lblInfo.ForeColor = System.Drawing.Color.Green
        End Using
    End Sub

Note: Don’t forget to add “System.Data.SqlClient” and “System.Configuration” namespaces.

Output:
Save or Insert TextBox Value into MS Access DataBase Table In Asp.net Using Csharp

Check the MS Access DB:
Save or Insert TextBox Value into MS Access DataBase Table In Asp.net Using Csharp 2

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code