Simple ASP.NET Code To Put Data Into Your MS Access DB

An asp.net Web Code For You to take user inputs and store it on to your Ms Access Database!Be Sure to Create it before and take care when you form the ConnectionString !!! That’s Very Important!



Default.aspx:

<%@ Page Title=”Home Page” Language=”C#” MasterPageFile=”~/Site.master” AutoEventWireup=”true”

CodeBehind=”Default.aspx.cs” Inherits=”WebApplication1._Default” %>

<asp:Content ID=”HeaderContent” runat=”server” ContentPlaceHolderID=”HeadContent”>

</asp:Content>

<asp:Content ID=”BodyContent” runat=”server” ContentPlaceHolderID=”MainContent”>

<h2>

Submission Place</h2>

<p>

<asp:Label ID=”Label1″ runat=”server” Text=”ID#”></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:TextBox ID=”t1″ runat=”server”></asp:TextBox>

</p>

<p>

Name:&nbsp;&nbsp;&nbsp;&nbsp;

<asp:TextBox ID=”t2″ runat=”server”></asp:TextBox>

</p>

<p>

Number:&nbsp;

<asp:TextBox ID=”t3″ runat=”server”></asp:TextBox>

</p>

<p>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:Button ID=”submit” runat=”server” onclick=”Button1_Click” Text=”Submit”

Width=”87px” />

</p>

<p>

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server”

ConnectionString=”<%$ ConnectionStrings:dbConnectionString %>”

ProviderName=”<%$ ConnectionStrings:dbConnectionString.ProviderName %>” SelectCommand=”SELECT * FROM [db1]”></asp:SqlDataSource>

</p>

<p>

Rahul Reddy M</p>

<p>

&nbsp;</p>

</asp:Content>

Default.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.OleDb;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Web.Util;

using System.Data;

namespace WebApplication1

{

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

int id = Int32.Parse(t1.Text);

String names = t2.Text;

int number = Int32.Parse(t3.Text);

String constr = ConfigurationManager.ConnectionStrings[“dbConnectionString”].ConnectionString;

OleDbConnection con = new OleDbConnection(constr);

con.Open();

OleDbCommand c = new OleDbCommand(“insert into db1 values(‘” + id + “‘,'” + names + “‘,'” + number + “‘)”);

c.Connection = con;

c.CommandType = CommandType.Text;

int i = c.ExecuteNonQuery();

if (i == 1)

{ Response.Write(“Your Data is Stored!”); }

//Reading Contents From Database

OleDbCommand c1 = new OleDbCommand(“select * from db1”);

c1.Connection = con;

c1.CommandType = CommandType.Text;

OleDbDataReader dr = c1.ExecuteReader();

while (dr.Read())

{

Response.Write(dr.GetInt32(0));

Response.Write(dr.GetString(1));

Response.Write(dr.GetInt32(2));

}

}

protected void AccessDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)

{

}

}

}

Web.config

<?xml version=”1.0″?>

<!–

For more information on how to configure your ASP.NET application, please visit

http://go.microsoft.com/fwlink/?LinkId=169433

–>

<configuration>

<connectionStrings>

<add name=”ApplicationServices” connectionString=”data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true”

providerName=”System.Data.SqlClient” />

<add name=”dbConnectionString” connectionString=”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Rahul\Documents\db.mdb”

providerName=”System.Data.OleDb” />

</connectionStrings>

<system.web>

<compilation debug=”true” targetFramework=”4.0″ />

<authentication mode=”Forms”>

<forms loginUrl=”~/Account/Login.aspx” timeout=”2880″ />

</authentication>

<membership>

<providers>

<clear/>

<add name=”AspNetSqlMembershipProvider” type=”System.Web.Security.SqlMembershipProvider” connectionStringName=”ApplicationServices”

enablePasswordRetrieval=”false” enablePasswordReset=”true” requiresQuestionAndAnswer=”false” requiresUniqueEmail=”false”

maxInvalidPasswordAttempts=”5″ minRequiredPasswordLength=”6″ minRequiredNonalphanumericCharacters=”0″ passwordAttemptWindow=”10″

applicationName=”/” />

</providers>

</membership>

<profile>

<providers>

<clear/>

<add name=”AspNetSqlProfileProvider” type=”System.Web.Profile.SqlProfileProvider” connectionStringName=”ApplicationServices” applicationName=”/”/>

</providers>

</profile>

<roleManager enabled=”false”>

<providers>

<clear/>

<add name=”AspNetSqlRoleProvider” type=”System.Web.Security.SqlRoleProvider” connectionStringName=”ApplicationServices” applicationName=”/” />

<add name=”AspNetWindowsTokenRoleProvider” type=”System.Web.Security.WindowsTokenRoleProvider” applicationName=”/” />

</providers>

</roleManager>

</system.web>

<system.webServer>

<modules runAllManagedModulesForAllRequests=”true”/>

</system.webServer>

</configuration>

Output:

Submitting Content to the Database:





Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s