...................................Database......................
create database ranjeet_db
create table reg
(
id int,
name varchar(50),
pass varchar(24)
)
insert into reg values('1','ranjeet','1')
select * from reg
...............................Register.aspx.......................................................................
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Table ID="Table1" runat="server">
<asp:TableHeaderRow>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>Emp Id</asp:TableCell><asp:TableCell>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Name</asp:TableCell><asp:TableCell>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Password</asp:TableCell><asp:TableCell>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell></asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label runat="server" ID="error" Text="Label"></asp:Label>
</asp:Content>
..................................C# coding (Register.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.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=RANJEETKUMAR-PC\\SQLEXPRESS; Initial Catalog=ranjeet_db; User Id=sa; Password=ranjeet");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//con.Open();
//if (con.State.ToString() == "Open")
//{
// error.Text = "open Connection";
//}
try
{
string str = "insert into reg values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"') ";
//string str = "insert into [reg] (id,name,pass) values(@id,@name,@password)";
SqlCommand cmd = new SqlCommand(str, con);
//cmd.Parameters.Add(new SqlParameter("@id", (object)TextBox1.Text));
//cmd.Parameters.Add(new SqlParameter("@name", (object)TextBox2.Text));
//cmd.Parameters.Add(new SqlParameter("@password", (object)TextBox3.Text));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
error.Text = "Sucessfuly Inserted";
}
catch(Exception ex)
{
error.Text = "Error "+ex;
}
}
}
....................................................Login.aspx..........................................
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Table ID="Table1" runat="server">
<asp:TableHeaderRow>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>Emp Id</asp:TableCell><asp:TableCell>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Password</asp:TableCell><asp:TableCell>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Label runat="server" ID="error" Text="Label"></asp:Label>
</asp:Content>
.........................................C#(Login.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.SqlClient;
public partial class Login : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=RANJEETKUMAR-PC\\SQLEXPRESS; Initial Catalog=ranjeet_db; User Id=sa; Password=ranjeet");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string str = " select * from reg where id='"+TextBox1.Text+"' AND pass='"+TextBox3.Text+"'";
SqlCommand cmd = new SqlCommand(str, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if ((dr.Read()))
{
error.Text = "Sucessfully Login";
con.Close();
}
else
{
error.Text = "Wroung Password";
}
}
catch (Exception ex)
{
error.Text = "Error" + ex;
}
}
}
create database ranjeet_db
create table reg
(
id int,
name varchar(50),
pass varchar(24)
)
insert into reg values('1','ranjeet','1')
select * from reg
...............................Register.aspx.......................................................................
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Table ID="Table1" runat="server">
<asp:TableHeaderRow>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>Emp Id</asp:TableCell><asp:TableCell>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Name</asp:TableCell><asp:TableCell>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Password</asp:TableCell><asp:TableCell>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell></asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label runat="server" ID="error" Text="Label"></asp:Label>
</asp:Content>
..................................C# coding (Register.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.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=RANJEETKUMAR-PC\\SQLEXPRESS; Initial Catalog=ranjeet_db; User Id=sa; Password=ranjeet");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//con.Open();
//if (con.State.ToString() == "Open")
//{
// error.Text = "open Connection";
//}
try
{
string str = "insert into reg values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"') ";
//string str = "insert into [reg] (id,name,pass) values(@id,@name,@password)";
SqlCommand cmd = new SqlCommand(str, con);
//cmd.Parameters.Add(new SqlParameter("@id", (object)TextBox1.Text));
//cmd.Parameters.Add(new SqlParameter("@name", (object)TextBox2.Text));
//cmd.Parameters.Add(new SqlParameter("@password", (object)TextBox3.Text));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
error.Text = "Sucessfuly Inserted";
}
catch(Exception ex)
{
error.Text = "Error "+ex;
}
}
}
....................................................Login.aspx..........................................
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Table ID="Table1" runat="server">
<asp:TableHeaderRow>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>Emp Id</asp:TableCell><asp:TableCell>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Password</asp:TableCell><asp:TableCell>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Label runat="server" ID="error" Text="Label"></asp:Label>
</asp:Content>
.........................................C#(Login.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.SqlClient;
public partial class Login : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=RANJEETKUMAR-PC\\SQLEXPRESS; Initial Catalog=ranjeet_db; User Id=sa; Password=ranjeet");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string str = " select * from reg where id='"+TextBox1.Text+"' AND pass='"+TextBox3.Text+"'";
SqlCommand cmd = new SqlCommand(str, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if ((dr.Read()))
{
error.Text = "Sucessfully Login";
con.Close();
}
else
{
error.Text = "Wroung Password";
}
}
catch (Exception ex)
{
error.Text = "Error" + ex;
}
}
}
No comments:
Post a Comment