You are on page 1of 5

Login example

role password name


admin 1111 aiman
customer 1234 ali (users table)
customer 6666 emad

Use varchar type for columns


default page Button 1 click event
string pass = "pppp" ,rol = "rrrr" , sql ;
SqlConnection conn = new SqlConnection("Data Source=localhost\\SqlExpress;Initial Catalog=dad;Integrated
Security=True");
sql = "SELECT * FROM users where name ='" + TextBox1.Text + "' ";
SqlCommand comm = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
{ pass = (string)reader["password"] ;
rol = (string)reader["role"] ; }
else
{ Label1.Text = "no user found "; }
reader.Close();
conn.Close();
if (TextBox2.Text == pass) {
Session["Authencated"] = rol;
if (rol == "admin")
Server.Transfer("Default2.aspx");
else
Server.Transfer("Default3.aspx");
}
else
Label1.Text = "wrong password";
Default2 page
protected void Page_Load(object sender, EventArgs e)
{
string id = (string)Session["Authencated"];
if (PreviousPage != null && id == "admin" )
 
 
Label1.Text = "welocome Mr Admin";
else
Server.Transfer("Default.aspx");

}
 

Chapter 11 – Slide 3
Buying
1- update the quantity when a buy is done for example
sql =“ UPDATE book SET quantity = quantity - 1 where (title ='" +
TextBox1.Text + "' )";
2- search and get and print the price
string sql, sql2;
sql = "UPDATE book SET quantity = quantity - 1 where (title ='" + TextBox1.Text + "' )";
SqlConnection conn = new SqlConnection("Data Source=localhost\\SqlExpress;Initial
Catalog=dad;Integrated Security=True");

SqlCommand comm = new SqlCommand(sql, conn);


conn.Open();
comm.ExecuteNonQuery();
sql2 = "SELECT * FROM book where title ='" + TextBox1.Text + "' ";
SqlCommand comm2 = new SqlCommand(sql2, conn);
SqlDataReader reader = comm2.ExecuteReader();

if (reader.Read())
{ Label1.Text = " you must pay<br />" + (int)reader["price"]; }
else
{ Label1.Text = "no user found "; }
reader.Close();
conn.Close();

You might also like