upload excel file in asp.net c# to sql server.after user confirmation
make table tblPersons
-----------------------------------------html file start------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="exca_to_grid.aspx.cs" Inherits="exca_to_grid" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Reading Excel data</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<asp:Label ID="Label1" runat="server">SpreadSheetContents:</asp:Label></p>
<asp:FileUpload ID="fileupload1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Show excl data" OnClick="btnSubmit_Click" />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
<asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click"
Text="Insert all records" />
<br />
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="PersonId" HeaderText="PersonId" SortExpression="PersonId" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Salary" HeaderText="Salary" SortExpression="Salary" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>" ProviderName="<%$ ConnectionStrings:constr.ProviderName %>" SelectCommand="SELECT [PersonId], [Name], [Salary] FROM [tblPersons]"></asp:SqlDataSource>
</div>
</div>
</form>
</body>
</html>
------------------------------------------------html end---------------------------------
-----------------------------------------code start------------------
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class exca_to_grid : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// strConnection = "ConnectionString";
string connectionString = "";
if (fileupload1.HasFile)
{
string fileName = Path.GetFileName(fileupload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(fileupload1.PostedFile.FileName);
string fileLocation = Server.MapPath("~/Files/" + fileName);
fileupload1.SaveAs(fileLocation);
if (fileExtension == ".xls")
{
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (fileExtension == ".xlsx")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
GridView1.DataSource = dtExcelRecords;
GridView1.DataBind();
}
}
protected void btn_insert_Click(object sender, EventArgs e)
{
foreach (GridViewRow g1 in GridView1.Rows)
{
SqlConnection con = new SqlConnection(connStr);
com = new SqlCommand("insert into tblPersons(PersonId,Name,Salary) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
Label1.Text = "Records inserted successfully";
GridView2.DataBind();
}
}
----------------------------------------code end---------------------------------