title: Importing data from Excel to database using asp.net
body: <p>I want to import data from Excel to database using asp.net. I'm using VS-2012 to do this.</p>
  
  <p>This is my code for the click event. But when I upload the file and click on the button the page shows "Page not found" error.</p>
  
  <pre><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 12.0;");
              OleDbCommand cmd = new OleDbCommand("select * from [Sheet$]", OleDbcon);
              OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
  
              OleDbcon.Open();
              DbDataReader dr = cmd.ExecuteReader();
              string con_str = "Gems1ConnectionString1";
  
              SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
              bulkInsert.DestinationTableName = "tbl_energy_report";
              bulkInsert.WriteToServer(dr);
              Array.ForEach(Directory.GetFiles((Server.MapPath("~/temp/"))), File.Delete);
              Label1.ForeColor = Color.Green;
              Label1.Text = "Succefully Imported the File";
          }
          else
          {
              Label1.ForeColor = Color.Red;
              Label1.Text = "Please Select A File";
          }
      }
  </code></pre>
  
  <p>Please help.</p>