C # Use session for login

Source: Internet
Author: User

<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<% @ Import Namespace = "System. Data. SqlClient" %>
<% @ Import Namespace = "System. Data" %>
<HTML>
<HEAD>
<TITLE> logon example </TITLE>
<Meta name = "Generator" CONTENT = "EditPlus">
<Meta name = "Author" CONTENT = "">
<Meta name = "Keywords" CONTENT = "">
<Meta name = "Description" CONTENT = "">
<Script Language = "C #" runat = "server">
Void Add_Click (Object sender, EventArgs e)
{
String str_id = Tel1.Text;
String str_name = Tel2.Text;
// Session ["id"] = str_name;
// Session ["UserName"] = str_name;
SqlCommand myCommand;
String myselect;
String myConnString = @ "server = localhost; uid = sa; pwd =; database = test ";
SqlConnection myConnection = new SqlConnection (myConnString );
MyConnection. Open ();
Myselect = "Select * from test where id =" + Tel1.Text + "and name =" + Tel2.Text + "";
MyCommand = new SqlCommand (myselect, myConnection );
SqlDataReader reader = myCommand. ExecuteReader ();
// SqlDataReader reader = myCommand. ExecuteReader ();
If (Tel1.Text = "" | Tel2.Text = "")
{
Label1.Text = "student ID name cannot be blank ";
}
Else if (Session ["UserName"]! = Null | Session ["id"]! = Null)
{
Label1.Text = "you have logged on ";
}
Else if (! Reader. Read ())
{
Label1.Text = "the database has no information! ";
Reader. Close ();
}
Else
{
Label1.Text = "Login successful ";
// Label. Text = Session ["Username"]. ToString ();
// Reader. Close ();
Tel1.Text = "";
Tel2.Text = "";
Session ["UserName"] = str_name ;;
Session ["id"] = str_id;
// Response. Redirect ("first3.aspx ");
}
MyConnection. Close ();
}

</Script>
</HEAD>

<BODY>
<Form runat = "server">
Student id: <asp: TextBox id = "Tel1" size = "20" runat = "server"/>
<P>
Name: <asp: TextBox id = "Tel2" size = "20" runat = "server"/>
<P>
<Asp: Button id = "Add" onClick = "Add_Click" runat = "server" Text = "login"> </asp: Button>
<P>
<Asp: Label id = "Label1" runat = "server"/>
</Form>
</BODY>
</HTML>

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.