Thursday, November 10, 2016

Monday, July 4, 2016

Import Excel File to SQL using ASP.net application

1. Add File Upload item toolbox
2. Add submit button to import
3. Add Label to show message after import
4. On button click event insert below code

 protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string path = string.Concat((Server.MapPath("~/temp/" + FileUpload1.FileName)));
                FileUpload1.PostedFile.SaveAs(path);
                //OleDbConnection oleDBcon = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source" + path + ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";");
                OleDbConnection oleDBcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"");
               
                OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]",oleDBcon);
                OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);

                oleDBcon.Open();
                DbDataReader dr = cmd.ExecuteReader();
                string con_str = @"Data Source=.;Initial Catalog=Posts;Integrated security=True";
                SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
                bulkInsert.DestinationTableName = "EmployeeDetails";
                bulkInsert.WriteToServer(dr);
                oleDBcon.Close();
                Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))),File.Delete);
                Label1.ForeColor = Color.Green;
                Label1.Text = "Inserted";
            }
            else
            {
                Label1.ForeColor = Color.Red;
                Label1.Text = "Please insert";
            }
        }

Also include below Dlls:
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Drawing;
using System.Data.Common;
using System;

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();