1 TIER ARCHITECTURE
.aspx code
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Email"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtemai" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="Cell"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtcell" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnsubmit" runat="server" Text="Submit"
onclick="btnsubmit_Click" style="height: 26px" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Label ID="lblerror" runat="server"></asp:Label>
</td>
</tr>
</table>
</asp:Content>
c# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection cat = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
try
{
cat.Open();
string sql = "insert into admin([name],[cell],[email])values('" + txtname.Text + "','" + txtcell.Text + "','" + txtemai.Text + "')";
SqlCommand cmd = new SqlCommand(sql, cat);
int id=(cmd.ExecuteNonQuery());
if (id == 1)
{
lblerror.Text = "data inserted";
}
else
{
lblerror.Text = "try again";
}
}
catch (Exception ex)
{
lblerror.Text = ex.Message;
}
finally
{
cat.Close();
}
}
}
DATABASE
create table admin ( id int identity(1000,1),name varchar(200),cell varchar(200),email varchar(200))
Webconfig
<add name="con" connectionString="Data Source=.;Initial Catalog=1tirer;Integrated Security=true;"/>
OUTPUT
OUTPUT
No comments:
Post a Comment