The excel file is getting stored on server but the database and hence the grid is supposed to get updated but here why it is not getting updated? Please tell me the solution thanks...!
private String uploadAttachmentOnServer(FileUpload fileUpload)
{
String fileName = null;
int filesize = 0;
try
{
fileName = fileUpload.FileName;
filesize = fileUpload.FileBytes.Length;
String fileType;
String fileSize;
String destinationPath = Server.MapPath("./Files/");
String extension = Uploader.fileUpload(fileUpload, destinationPath, out fileType, out fileSize);
String fileVirtualPath = null;
if (extension != null)
{
fileVirtualPath = "../Files/" + fileUpload.FileName;
return destinationPath + extension;
}
return null;
}
catch (Exception ex)
{
throw ex;
}
return null;
}
protected void Upload(object sender, System.EventArgs e)
{
String excelPath = uploadAttachmentOnServer(FileUpload1);
FileUpload1.SaveAs(excelPath);
string conString = string.Empty;
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
switch (extension)
{
case ".xls":
conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx":
conString = ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString;
break;
}
conString = string.Format(conString, excelPath);
using (OleDbConnection excel_con = new OleDbConnection(conString))
{
excel_con.Open();
string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
DataTable dtExcelData = new DataTable();
dtExcelData.Columns.AddRange(new DataColumn[19] { new DataColumn("NGOName", typeof(string)),
new DataColumn("RegiID", typeof(int)),
new DataColumn("ExName", typeof(string)),
new DataColumn("Designation", typeof(string)),
new DataColumn("Skill", typeof(string)),
new DataColumn("Membership", typeof(string)),
new DataColumn("Address ", typeof(string)),
new DataColumn("City ", typeof(string)),
new DataColumn("Pin ", typeof(int)),
new DataColumn("District ", typeof(string)),
new DataColumn("State ", typeof(string)),
new DataColumn("Country ", typeof(string)),
new DataColumn("Landline ", typeof(int)),
new DataColumn("Con1", typeof(string)),
new DataColumn("Con2 ", typeof(string)),
new DataColumn("Email1 ", typeof(string)),
new DataColumn("Email2 ", typeof(string)),
new DataColumn("Website ", typeof(string)),
new DataColumn("Description", typeof(string)),
});
using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
{
oda.Fill(dtExcelData);
}
excel_con.Close();
string consString = ConfigurationManager.ConnectionStrings["CS2"].ConnectionString;
using (SqlConnection con = new SqlConnection(consString))
{
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
{
sqlBulkCopy.DestinationTableName = "dbo.ORG_Tb";
sqlBulkCopy.ColumnMappings.Add("NGOName", "NGOName");
sqlBulkCopy.ColumnMappings.Add("RegiID", "RegiID");
sqlBulkCopy.ColumnMappings.Add("ExName", "ExName");
sqlBulkCopy.ColumnMappings.Add("Designation", "Designation");
sqlBulkCopy.ColumnMappings.Add("Skill", "Skill");
sqlBulkCopy.ColumnMappings.Add("Membership", "Membership");
sqlBulkCopy.ColumnMappings.Add("Address", "Address");
sqlBulkCopy.ColumnMappings.Add("City", "City");
sqlBulkCopy.ColumnMappings.Add("Pin", "Pin");
sqlBulkCopy.ColumnMappings.Add("District", "District");
sqlBulkCopy.ColumnMappings.Add("State", "State");
sqlBulkCopy.ColumnMappings.Add("Country", "Country");
sqlBulkCopy.ColumnMappings.Add("Landline", "Landline");
sqlBulkCopy.ColumnMappings.Add("Con1", "Con1");
sqlBulkCopy.ColumnMappings.Add("Con2", "Con2");
sqlBulkCopy.ColumnMappings.Add("Email1", "Email1");
sqlBulkCopy.ColumnMappings.Add("Email2", "Email2");
sqlBulkCopy.ColumnMappings.Add("Website", "Website");
sqlBulkCopy.ColumnMappings.Add("Description", "Description");
con.Open();
sqlBulkCopy.WriteToServer(dtExcelData);
con.Close();
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Message", "alert('Record Added Sucessfully')", true);
}
}
}
}