Sunday, March 1, 2015

Create Session on Login page using C# and redirect it to next page also expire session

  1. Create 2 Web form pages named: Login , named: Secondpage
  2. Now create two text boxes and one Login button
  3. on Button click event of Login page add this code: 
  4.  if (TextBox1.Text == "abc" && TextBox2.Text == "abc")
                {
                    Session["login"] = TextBox1.Text;
                    Session["password"] = TextBox2.Text;
                    Response.Redirect("Secondpage.aspx");// redirect page to Secondpage.aspx
  5. Now on Secondpage Page_Load event add below code
  6. if (Session["Login"] != null && Session["password"]!=null)
                {
                    Response.Write(Session["login"].ToString());
                    Response.Write(Session["password"].ToString());
                }
                else
                {
                    Response.Write("Session has expired");
                }
  7. To expire session add below code into Web.config. Session will be expired after 1 minute
  8. <sessionState mode="InProc" cookieless="false" timeout="1" />

Saturday, February 28, 2015

Save Exceptions into Sql Database using C#

  1. Create  Database named: ExceptionDatabase into sql an than create ExceptionDetails Table in it
  2. Create a new class named: Exceptionfunction and add below function in it
  3.  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();
            }
  4. Now create a new Webform and add text boxes and a button
  5. On buttom click add function to do Sum of two integers from textboxes
  6. 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);
                }}
  7. 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();