Finally I made it.
Hear is a solution.
protected void btnUpload_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
if (fUpload.FileName != null)
{
string fileLocation = Server.MapPath("~/Attachment/") + fUpload.FileName;
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
fUpload.SaveAs(fileLocation);
string excelConnectionString = string.Empty;
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ fileLocation + ";Extended Properties='Excel 12.0 Xml;HDR=YES'";
string fileExtension = System.IO.Path.GetExtension(fUpload.FileName);
if (fileExtension == ".xls")
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.4.0;Data Source="
+ fileLocation + ";Extended Properties='Excel 8.0 Xml;HDR=YES'";
}
else if (fileExtension == ".xlsx")
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ fileLocation + ";Extended Properties='Excel 12.0 Xml;HDR=YES'";
}
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
DataTable dt = new DataTable();
dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String[] excelSheets = new String[dt.Rows.Count];
int t = 0;
foreach (DataRow row in dt.Rows)
{
excelSheets[t] = row["TABLE_NAME"].ToString();
t++;
}
OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);
string query = string.Format("Select * from [{0}]", excelSheets[0]);
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
{
dataAdapter.Fill(ds);
}
excelConnection.Close();
excelConnection1.Close();
StartUploadEmails(ds);
}
else
{
lblErrro.Text = "Please select file to upload.";
}
}
public void StartUploadEmails(DataSet ds)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string EmpName = ds.Tables[0].Rows[i]["EmpName"].ToString();
string EmpCode = ds.Tables[0].Rows[i]["EmpCode"].ToString();
string Basic = ds.Tables[0].Rows[i]["EmpCode"].ToString();
string Allowance = ds.Tables[0].Rows[i]["Allowance"].ToString();
string Cony = ds.Tables[0].Rows[i]["Cony"].ToString();
string query = "";
for (int j = 0; j < 3; j++)
{
switch (j)
{
case 0:
query = "insert into GeetEmployee (EmpName, EmpCode, Amount, CompanyCode) values ('" + EmpName + "','" + EmpCode + "', '" + Convert.ToInt32(Basic) + "', '" + (long)TypeCode.Basic + "')";
ExecuteData(query);
break;
case 1:
query = "insert into GeetEmployee (EmpName, EmpCode, Amount, CompanyCode) values ('" + EmpName + "','" + EmpCode + "', '" + Convert.ToInt32(Allowance) + "', '" + (long)TypeCode.Allowance + "')";
ExecuteData(query);
break;
case 2:
query = "insert into GeetEmployee (EmpName, EmpCode, Amount, CompanyCode) values ('" + EmpName + "','" + EmpCode + "', '" + Convert.ToInt32(Cony) + "', '" + (long)TypeCode.Cony + "')";
ExecuteData(query);
break;
default:
break;
}
}
}
}
private void ExecuteData(string query)
{
try
{
cn.Open();
cmd = new SqlCommand(query, cn);
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
cn.Close();
}
}
public enum TypeCode
{
Basic = 10,
Allowance = 20,
Cony = 30
}