if (FlUpload1.HasFile == true)
{
string str = "";
try
{
lblMsg.Text = "";
lblCount.Text = "";
lblError.Text = "";
grdShow.DataSource = null;
grdShow.DataBind();
DataTable ds = new DataTable();
if (FlUpload1.HasFile == true && new clsExtChk().CheckExt(FlUpload1.PostedFile.FileName, FlUpload1.PostedFile.ContentLength))
{
str = Server.MapPath(Path.GetFileName(FlUpload1.PostedFile.FileName));
FlUpload1.PostedFile.SaveAs(str);
string path = System.IO.Path.GetFullPath(str);
}
int flag = 0;
if (Path.GetExtension(str) == ".xls")
{
oledbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + str + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
flag = 1;
}
else if (Path.GetExtension(str) == ".xlsx")
{
oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + str + ";Extended Properties='Excel 8.0;HDR=YES';");
flag = 1;
}
if (flag == 1)
{
oledbConn.Open();
string SpreadSheetName = "";
string query = null;
DataTable ExcelSheets = oledbConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
SpreadSheetName = ExcelSheets.Rows[0]["TABLE_NAME"].ToString();
query = "SELECT UID, Title, First_Name, Last_Name,Organisation,Designation,Address1,Address2,Address3,City,PIN,District,State,Country,WorkPhone,HomePhone,Fax,Email,Website,OrgType,NatureofBusiness,DomainSpecialization,Offerings,StartofSubscription,MagazineName,SubsID,SubscriptionType,dead_alive,ConferenceName,ConferenceYear,Delegate_Type,delegateNumber,RefID,RegistrationDate,Remark FROM [" + SpreadSheetName + "]"; //,StartofSubscription,Magazine,SubsID,SubscriptionType,dead_alive,ConferenceName,ConferenceYear,Delegate_Type,delegateNumber,RefID,RegistrationDate,
OleDbDataAdapter oleda = new OleDbDataAdapter(query, oledbConn);
oleda.Fill(ds);
oledbConn.Close();
lblMsg.Text = ds.Rows.Count.ToString();
if (ds.Rows.Count > 0)
{
grdShow.DataSource = ds;
grdShow.DataBind();
for (int i = 0; i <= grdShow.Rows.Count - 1; i++)
{
if (grdShow.Rows[i].Cells[4].Text.Trim().Replace(" ", "") == "" || grdShow.Rows[i].Cells[13].Text.Trim().Replace(" ", "") == "")
{
grdShow.Rows[i].ForeColor = System.Drawing.Color.Red;
}
else if (grdShow.Rows[i].Cells[2].Text.Trim().Replace(" ", "") == "" && grdShow.Rows[i].Cells[3].Text.Trim().Replace(" ", "") == "")
{
grdShow.Rows[i].ForeColor = System.Drawing.Color.Red;
}
else if (grdShow.Rows[i].Cells[0].Text.Trim().Replace(" ", "") == "" && grdShow.Rows[i].Cells[17].Text.Trim().Replace(" ", "") == "")
{
grdShow.Rows[i].ForeColor = System.Drawing.Color.Red;
}
else if (grdShow.Rows[i].Cells[0].Text.Trim().Replace(" ", "") == "" && grdShow.Rows[i].Cells[17].Text.Trim().Replace(" ", "") != "")
{
grdShow.Rows[i].Cells[0].Text = grdShow.Rows[i].Cells[17].Text;
}
}
}
else
{
lblMsg.Text = "No Record Found";
}
File.Delete(str);
oleda.Dispose();
ds.Dispose();
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Please select Excel file');", true);
}
}
catch (Exception ex)
{
oledbConn.Close();
File.Delete(str);
lblMsg.Text = ex.Message;
}
finally
{
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Please select file');", true);
}