use file uploader and the excel heads should be same as table heads.
try following code
// namespace used
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.OleDb;
using System.IO;
string FileName;
string FilePath;
protected string valid(OleDbDataReader myreader, int stval)//if any columns are found null then they are replaced by zero
{
object val = myreader[stval];
if (val != DBNull.Value)
return val.ToString();
else
return Convert.ToString(0);
}
// on button click
protected void Button1_Click(object sender, EventArgs e)
{
FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FilePath = Server.MapPath("Files/") + FileName;
this.FileUpload1.PostedFile.SaveAs(FilePath);
OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=Excel 8.0");
try
{
OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", oconn);
oconn.Open();
OleDbDataReader odr = ocmd.ExecuteReader();
// i am use 3 layer concept thats why use following code , u can replace it by your code
while (odr.Read())
{
BCobj.Customer_Name = valid(odr, 0);
BCobj.Mobile_No = valid(odr, 1);
BCobj.City = valid(odr, 2);
BCobj.Mode = 0;
BCobj.ID = 0;
RowEffected = BCobj.Customer_Info_Insert_BL();
if (RowEffected > 0)
{
Label1.Text = "Data Insert Successfully...!";
}
}
}
catch (Exception)
{
throw;
}
}