ASP. NET 2.0 writes a simple login using the stored procedure!

Source: Internet
Author: User

ASP. NET 2.0 writes a simple login using the stored procedure! Hope it will be helpful for beginners!
Posting time: 15:33:28 Author: li520will
Using system;
Using system. Data;
Using system. configuration;
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. Data. sqlclient;

Public partial class _ default: system. Web. UI. Page
{
Private readonly string sqlcon = configurationmanager. etettings ["constring"]. tostring ();
Protected void page_load (Object sender, eventargs E)
{

}
Protected void button#click (Object sender, eventargs E)
{
If (textbox1.text. Trim () = "" | textbox2.text. Trim () = "")
{
Response. Write (@ "<script language = 'javascript '> alert ('user name and password cannot be blank! ') </SCRIPT> ");
}
Else
{
Sqlconnection con = new sqlconnection (sqlcon );
Sqlcommand comm = con. createcommand ();
Comm. commandtype = commandtype. storedprocedure;
Comm. commandtext = "validatelogin ";
Comm. Parameters. addwithvalue ("@ username", textbox1.text. Trim ());
Comm. Parameters. addwithvalue ("@ password", textbox2.text. Trim ());
Sqlparameter sqlpal = comm. Parameters. addwithvalue ("@ rerurn_value ","");
Sqlpal. Direction = parameterdirection. returnvalue;
Con. open ();
Comm. executenonquery ();
Con. Close ();
Int iret = (INT) Comm. Parameters ["@ rerurn_value"]. value;
If (iret = 1)
{
Response. Write (@ "<script language = 'javascript '> alert ('login successful! Click "OK"); </SCRIPT> ");
Formsauthentication. redirectfromloginpage (textbox1.text. Trim (), true );
Response. Redirect ("default2.aspx ");
}
Else if (iret = 2)
{
Response. Write (@ "<script language = 'javascript '> alert ('your user name and password are incorrect. Please try again! '); </SCRIPT> ");
}
Else if (iret = 3)
{
Response. Write (@ "<script language = 'javascript '> alert (' sorry, you have not registered yet! '); </SCRIPT> ");
}

}
}
}
Certificate ------------------------------------------------------------------------------------------------------------------------------------------

Create a table with one ID, username, and password for each of the three fields.

The following is the stored procedure!

Alter procedure DBO. validatelogin
@ Username varchar (50 ),
@ Password varchar (50)
As
 
If @ username in (select Username
From logininfo)
Begin
If exists (select ID, username, password
From logininfo
Where (username = @ username) and (Password = @ password ))
Begin
Print 'login successful! '
Return 1
End
Print 'your username and password are incorrect. Please try again! '
Return 2
End
Print 'You have not registered yet! '
Return 3
Return
Certificate ---------------------------------------------------------------------------------------------------------------------------------------------
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.