- Create 2 Web form pages named: Login , named: Secondpage
- Now create two text boxes and one Login button
- on Button click event of Login page add this code:
- if (TextBox1.Text == "abc" && TextBox2.Text == "abc")
{
Session["login"] = TextBox1.Text;
Session["password"] = TextBox2.Text;
Response.Redirect("Secondpage.aspx");// redirect page to Secondpage.aspx - Now on Secondpage Page_Load event add below code
- if (Session["Login"] != null && Session["password"]!=null)
{
Response.Write(Session["login"].ToString());
Response.Write(Session["password"].ToString());
}
else
{
Response.Write("Session has expired");
} - To expire session add below code into Web.config. Session will be expired after 1 minute
- <sessionState mode="InProc" cookieless="false" timeout="1" />
Sunday, March 1, 2015
Create Session on Login page using C# and redirect it to next page also expire session
Labels:
.net,
C#,
create,
expire,
expire session,
login page,
page,
redirect,
Session
Saturday, February 28, 2015
Save Exceptions into Sql Database using C#
- Create Database named: ExceptionDatabase into sql an than create ExceptionDetails Table in it
- Create a new class named: Exceptionfunction and add below function in it
- public static void connecttion(Exception exdb)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=MCS000009-PC\SQL;Initial Catalog=ExceptionDatabase; Trusted_Connection=True;";
string query1 = "insert into ExceptionDetails (ExceptionMsg,ExceptionType) values( '" + exdb.Message.ToString() + "', '" + exdb.GetType().Name.ToString() + "')";
SqlCommand cmd1 = new SqlCommand(query1, con);
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
} - Now create a new Webform and add text boxes and a button
- On buttom click add function to do Sum of two integers from textboxes
- protected void Button1_Click(object sender, EventArgs e)
{
try
{
int a = Convert.ToInt32(TextBox1.Text);
int b = Convert.ToInt32(TextBox2.Text);
int c = a + b;
Response.Write(c);
}
catch (Exception ex)
{
ExceptionFunction.connecttion(ex);
}} - In catch add call function of Exceptionfunction class.
Connect to Sql Database using C#
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=MCS000009-PC\SQL;
InitialCatalog=ExceptionDatabase; Trusted_Connection=True;";
string query1 = "insert into ExceptionsTb(MSg,Type)
values( '" + exdb.Message.ToString() + "', '" + exdb.GetType().Name.ToString() + "')";
SqlCommand cmd1 = new SqlCommand(query1, con);
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
con.ConnectionString = @"Data Source=MCS000009-PC\SQL;
InitialCatalog=ExceptionDatabase; Trusted_Connection=True;";
string query1 = "insert into ExceptionsTb(MSg,Type)
values( '" + exdb.Message.ToString() + "', '" + exdb.GetType().Name.ToString() + "')";
SqlCommand cmd1 = new SqlCommand(query1, con);
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
Subscribe to:
Comments (Atom)