How to insert Details in access database in asp.net

Leave a Comment
 Create an Application which stores user information to the database.

HTML SOURCE CODE 

 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>    <script language="javascript">        function validateNo(oSrc, args) {            args.IsValid = (args.Value % 5 == 0);        }    </script></head><body>    <form id="form1" runat="server">    <div>    <asp:Label ID="l1" runat="server" Style="z-index: 100; left: 272px; position: absolute;
            top: 168px; bottom: 300px;" Text="First Name"></asp:Label>        <asp:Label ID="l2" runat="server" Style="z-index: 101; left: 272px; position: absolute;
            top: 218px" Text="Last Name"></asp:Label>        <asp:Label ID="l3" runat="server" Style="z-index: 102; left: 272px; position: absolute;
            top: 268px" Text="Address"></asp:Label>        <asp:Label ID="l6" runat="server" Style="z-index: 103; left: 272px; position: absolute;
            top: 418px" Text="Age"></asp:Label>        <asp:Label ID="l5" runat="server" Style="z-index: 104; left: 272px; position: absolute;
            top: 368px" Text="Gender"></asp:Label>        <asp:Label ID="l7" runat="server" Style="z-index: 105; left: 272px; position: absolute;
            top: 468px" Text="Phone No"></asp:Label>        <asp:Label ID="l8" runat="server" Style="z-index: 106; left: 272px; position: absolute;
            top: 517px; height: 19px;" Text="Email "></asp:Label>        <asp:TextBox ID="t1" runat="server" Style="z-index: 107; left: 396px; position: absolute;
            top: 167px; bottom: 313px;" TabIndex="1" Width="155px"></asp:TextBox>        <asp:TextBox ID="t2" runat="server" Style="z-index: 108; left: 396px; position: absolute;
            top: 216px; bottom: 382px;" TabIndex="2" Width="155px"></asp:TextBox>        <asp:TextBox ID="t3" runat="server" Style="z-index: 109; left: 396px; position: absolute;
            top: 266px" TabIndex="6" Width="155px" TextMode="MultiLine"></asp:TextBox>        <asp:TextBox ID="t4" runat="server" Style="z-index: 110; left: 396px; position: absolute;
            top: 416px; bottom: 178px; right: 734px;" TabIndex="9" Width="155px"></asp:TextBox>        <asp:RadioButton ID="RadioButton1" runat="server" GroupName="Gender" Style="z-index: 111;
            left: 396px; position: absolute; top: 366px" TabIndex="7" Text="Male" />        <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Gender" Style="z-index: 112;
            left: 470px; position: absolute; top: 366px; height: 21px;" TabIndex="8"             Text="Female" />        <asp:TextBox ID="t5" runat="server" Style="z-index: 113; left: 396px; position: absolute;
            top: 465px" TabIndex="12" Width="155px"></asp:TextBox>        <asp:TextBox ID="t6" runat="server" Style="z-index: 114; left: 396px; position: absolute;
            top: 508px" TabIndex="13" Width="155px"></asp:TextBox>        <asp:Button ID="b1" runat="server" OnClick="b1_Click" Style="z-index: 121;
            left: 367px; position: absolute; top: 556px" TabIndex="14" Text="Add"             Width="100px" />        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" BorderStyle="None"            ControlToValidate="t1" ErrorMessage="Enter Valid First Name" Style="z-index: 125;
            left: 566px; position: absolute; top: 176px; bottom: 344px; width: 14px;">*</asp:RequiredFieldValidator>        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="t2"            ErrorMessage="Enter Valid Last Name" Height="19px" Style="z-index: 126; left: 565px;
            position: absolute; top: 215px" Width="16px">*</asp:RequiredFieldValidator>        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="t3"            ErrorMessage="Enter Valid Address" Height="19px" Style="z-index: 129; left: 565px;
            position: absolute; top: 274px; width: 16px;">*</asp:RequiredFieldValidator>        <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="t4"            ErrorMessage="Range should be 18-25" MaximumValue="25" MinimumValue="18" Style="z-index: 130;
            left: 584px; position: absolute; top: 415px"></asp:RangeValidator>                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="t5"            ErrorMessage="Enter Valid Phone No" Style="z-index: 133; left: 565px; position: absolute;
            top: 468px; right: 712px;">*</asp:RequiredFieldValidator>        <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="t6"            ErrorMessage="Enter Valid Email" Style="z-index: 134; left: 565px; position: absolute;
            top: 509px">*</asp:RequiredFieldValidator>        <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="t4"            ErrorMessage="Enter Valid Age" Style="z-index: 135; left: 565px; position: absolute;
            top: 415px">*</asp:RequiredFieldValidator>        <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="validateNo"            ErrorMessage="Enter Valid no" Style="z-index: 136; left: 587px; position: absolute;
            top: 467px">*</asp:CustomValidator>        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="t6"            ErrorMessage="Enter Valid Email id" Style="z-index: 137; left: 593px; position: absolute;
            top: 508px"            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>    </div>    <asp:DropDownList ID="d1" runat="server"        style="top: 324px; left: 396px; position: absolute; height: 22px; width: 155px">        <asp:ListItem>Viramgam</asp:ListItem>        <asp:ListItem>Nadiad</asp:ListItem>        <asp:ListItem>Surat</asp:ListItem>        <asp:ListItem>Ahmedabad</asp:ListItem>        <asp:ListItem>Baroda</asp:ListItem>        <asp:ListItem>Surendranagar</asp:ListItem>        <asp:ListItem>Mahesana</asp:ListItem>        <asp:ListItem>Bhavnagar</asp:ListItem>        <asp:ListItem>Rajkot</asp:ListItem>    </asp:DropDownList>    <asp:Label ID="l4" runat="server"        style="top: 324px; left: 270px; position: absolute; height: 19px; width: 34px"        Text="City"></asp:Label>            <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server"            ControlToValidate="d1"            style="top: 322px; left: 566px; position: absolute; height: 19px; width: 17px">*</asp:RequiredFieldValidator>    </div>    </form></body></html>

CS CODE

 using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;
 public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {     }    protected void b1_Click(object sender, EventArgs e)    {        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=C:\\Users\\MULSANIYA\\Desktop\\LAB92\\App_Data\\as.mdb;");        OleDbCommand cmd = new OleDbCommand();        cmd.CommandType = CommandType.Text;        cmd.CommandText = "insert into tn  values ('" + t1.Text + "','" + t2.Text + "','" + t3.Text + "','" + d1.SelectedItem.Text + "','" + RadioButton1.Text + "','" + t4.Text + "',  '"+t5.Text + "','" + t6.Text + "')";        cmd.Connection = con;        con.Open();        cmd.ExecuteNonQuery();        con.Close();      }}

 output::



0 comments:

Post a Comment