C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET WebForms »

Login form in Asp.net


Posted Date: 24 Jun 2009    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: Apul VyasMember Level: Gold    
Rating: 1 out of 5Points: 5



Abstract:


This code shows how we can create a login form. It will check whether the password is matching for the specified username or not. If its matching then it will check the role of the user and redirect the page as per the user role.

Here is the code:



< %@ Page Language="C#" % >

< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< %@ Import Namespace = "System.Data.SqlClient" % >
< script runat="server" >
protected void Page_Load(object sender, EventArgs e)
{
Session["id"] = "m";
}

protected void Button1_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=ANKIT-PC;Initial Catalog=marble;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select id,password,role from login where id='" + username.Text + "'", con);

SqlDataReader dr;
con.Open();
dr = cmd.ExecuteReader();
string var1;
while (dr.Read())
{
var1 = dr["password"].ToString();

if (password.Text == var1)
{
Session["id"] = dr["role"].ToString();
if (dr["role"].ToString() == "admin_outlet")
{
Response.Redirect("admin_outlet.aspx");
}
else if (dr["role"].ToString() == "admin_godown")
{
Response.Redirect("admin_stock.aspx");
}
else if (dr["role"].ToString() == "admin_mining")
{
Response.Redirect("admin_mining.aspx");
}

else
{
Response.Redirect("guest.aspx");
}
}

else
{
message.text = "Incorrect username or password";
password.Text = "";
password.Focus();
}
}
}
< /script >

< html xmlns="http://www.w3.org/1999/xhtml" >
< head runat="server" >
< title > Untitled Page< /title >
< /head >
< body >
< form id="Form1" runat="server" >
< table border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#FFFFFF" >
< tr >
< td width="50%" > < /td >
< td valign="top" colspan="4" >
< table border="0" width="970" cellspacing="0" cellpadding="0" background="images/fon_top.gif" >
< tr >
< td valign="top" > < /td >
< td valign="top" align="right" style="padding-top: 42" > < table border="0" cellspacing="0" cellpadding="0" style="padding-bottom: 10" >

< tr >
< td style="padding-right: 15" > < /td >

< td style="padding-left: 15; padding-right: 60" > < /td >
< /tr >
< /table > < /td >
< /tr >
< /table > < /td >
< td width="50%" > < /td >
< /tr >
< tr >
< td width="50%" > < /td >
< td background="images/fon_but.gif" colspan="4" width="770" > < a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image4','','images/but1r.gif',1)" > < /a > < a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image5','','images/but2r.gif',1)" > < /a > < a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image6','','images/but3r.gif',1)" > < /a > < a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image7','','images/but4r.gif',1)" > < /a > < a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image8','','images/but5r.gif',1)" > < /a > < /td >
< td width="50%" > < /td >
< /tr >
< tr >
< td width="50%" rowspan="5" > < /td >
< td background="images/fon.gif" valign="top" width="153" rowspan="5" height="100%" > < br/ > < a href="home.aspx" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image10','','images/but1_2.gif',1)" > < /a > < br/ > < a href="aboutus.aspx" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image11','','images/but2_2.gif',1)" > < /a > < br/ > < a href="contactus.aspx" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image13','','images/but4_2.gif',1)" > < /a > < br/ > < a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image14','','images/but5_2.gif',1)" > < /a > < br/ > < br/ > < a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image15','','images/but6_2.gif',1)" > < /a > < br/ > < a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image16','','images/but7_2.gif',1)" > < /a > < br/ > < a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image17','','images/but8_2.gif',1)" > < /a > < br/ > < a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image18','','images/but9_2.gif',1)" > < /a > < br/ > < br/ >
< div style="padding-left: 23; padding-right: 17; padding-top: 10" > < b >
< /b >
< br/ >
< br/ > < /div > < /td >
< td valign="middle" colspan="2" width="620" height="30" align="center" >







< script language="javascript1.2" type="text/javascript" >
var head=''
var top=0
var maxheight
var finished=false
var tickers=new Array()

tickers[0]='< font color="ff6600" font size="4px" font family="Copperplate Gothic Bold" > Sun Shine Marbles< /font > '
tickers[1]='< font color="ff6600" font size="4px" font family="Copperplate Gothic Bold" > Welcome to our website < /font > '
tickers[2]='< font color="ff6600" font size="4px" > The era of natural Stones< /font > '
tickers[3]='< font color="ff6600" font size="4px" > We just dont deliver marbles... we deliver your DREAM too!! < /font > '
tickers[4]='< font color="ff6600" font size="4px" > Where stone is given form & shapes that shell enthral you forever< /font > '
tickers[5]='< font color="ff6600" font size="4px" > We strive to offer highest quality products by undertaking stringent measures < /font > '
tickers[6]='< font color="ff6600" font size="4px" > We strive to offer highest quality products running checks on regular basis on our products< /font > '



for (i=1;i< =tickers.length-1;i++){

if (tickers[i].length > =tickers[top].length)
top=i
}
if (document.all)
document.write('< span id="whatsnew" style="width:100%;height=30;filter:revealTrans(duration=2,transition=19)" class="style1" > < font color="red" > '+tickers[0]+'< /font > < /span > ')

function change(){
whatsnew.innerHTML=''
whatsnew.filters.revealTrans.apply()
whatsnew.filters.revealTrans.play()
whatsnew.innerHTML=tickers[c]
if (c==tickers.length-1)
c=0
else
c+=1
setTimeout("change()",2000)
}
if (document.all)
document.write('< span id=ns style="display:none;" > ')
//-- >
< /script >
< !--Insert alternate HTML codes here
that will be seen by browsers OTHER
than IE 4.x-- > < span id="temp" style="left: 25px; position: absolute; top: 0px;" >
< script type="text/javascript" >
< !--
if (document.all){
temp.style.width=whatsnew.style.width
document.write(tickers[top])
}
begin()
function IFRAME1_onclick() {

}

//-- >
< /script > < /span > < /td >
< td valign="top" width="183" > < /td >
< td width="50%" > < /td >
< /tr >
< tr >
< td valign="top" colspan="2" rowspan="2" >
< div style="padding-left: 25; padding-right: 25; padding-top: 25; padding-bottom: 25" >
< table border="0" width="635" cellspacing="0" cellpadding="0" bgcolor="white" height="285" >
< tr >
< td >
< table width="315" height="285" >
< tr >
< td width="315" height="100" bgcolor="white" >
 < /td >
< /tr >
< tr >
< td >
< /td >
< /tr >
< /table >
< /td >
< td width="320" height="285" bgcolor="white" valign="top" style="font-family: Georgia" >
< font size="2px", color="#954215" > < /font >

< /td >
< /tr >

< /table >
< /div >
< /td >
< td valign="top" background="images/fon.gif" height="140" > < table border="0" cellspacing="0" cellpadding="0" >
< tr >
< td style="width: 157px" > < /td >
< /tr > < tr >
< td style="padding-left: 20; width: 157px; height: 24px;" >
< asp:TextBox ID="username" runat="server" Width="161px" ForeColor="Black" > < /asp:TextBox > < /td >
< /tr >
< tr >
< td style="width: 157px" > < /td >
< /tr >
< tr >
< td style="padding-left: 20; padding-bottom: 7; width: 157px; height: 24px;" >
< asp:TextBox ID="password" runat="server" TextMode="Password" Width="162px" > < /asp:TextBox > < /td >
< /tr >
< tr >
< td style="padding-left: 17; width: 157px;" > < input type="checkbox" name="C1" value="ON"/ > remember me< /td >
< /tr >
< tr >
< td align="right" style="width: 157px" >
< asp:Button ID="Button1" runat="server" Style="left: -39px; background-image: url(images/sub.gif);
position: relative; top: 7px" Text="Submit" OnClick="Button1_Click1" / > < br/ > < /td >
< /tr >

< /table >
< asp:Label ID="message" runat="server" Text="Label" Width="146px" > < /asp:Label > < td width="50%" > < /td >
< /tr >
< tr >
< td valign="top" background="images/fon.gif" width="153" height="140" > < div style="padding-left: 15; padding-right: 5; padding-top: 10" > < hr width="100%"/ > < div >
< /div >
< /div >
< /td >
< td width="50%" > < /td >
< /tr >
< tr >
< td valign="top" colspan="2" > < img border="0" src="images/m2.gif" width="635" height="20" alt=""/ > < /td >
< td valign="top" background="images/fon.gif" > < /td >

< /tr >
< tr >
< td valign="top" width="1" > < /td >
< td valign="top"
style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; width: 820px;"
height="80" >  < marquee scrollamount="3" scrolldelay="2" direction="left" behavior="alternate"
width="620" bgcolor="ffcc99" height="130" >


< /td >
< td valign="top" background="images/fon.gif" height="100%" > < /td >
< td width="50%" > < /td >
< /tr >
< tr >
< td width="50%" > < /td >
< td colspan="4" background="images/fon_bot.gif" height="72" valign="middle" align="center" >    

< p style="font-size: 10px; margin-top: 7" > < font color="#FFFFFF" > Designed by Apul Vyas < /font > < /p > < /td >
< td width="50%" > < /td >
< /tr >
< /table >
< /form >

< /body >
< /html >


I have attached the aspx file with the C# code.

Attachments

  • Login form (29578-24818-Default6.aspx.txt)


  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Username  .  Update  .  Session  .  Password  .  Login  .  Insert  .  Gridview  .  Disconnected  .  Datagrid  .  C#.net  .  Asp.net  .  Apul  .  Ado.net  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Uploading images into picture library in sharepoint using c#
    Previous Resource: Date Comparison in C#
    Return to Discussion Resource Index
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use