I know this question is repeated but problem with me is I have tried almost all the link for solution and I didnt get the success. So here is the scenario: I have a input type = file & when I click on submit button, it POST the file.
here is the code:
[HttpPost]
public ActionResult Index(HttpPostedFileBase excel_File)
{
if (excel_File != null)
{
//Save the uploaded file to the disc.
string saved_FileName = Path.Combine(@ConfigurationManager.AppSettings["excel_file_storage_path"] + excel_File.FileName); // Taking file upload location from web.config
excel_File.SaveAs(saved_FileName);
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0 Xml;HDR=YES", saved_FileName);
//Fill the dataset with information from the Sheet1 worksheet.
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds); // I get error here.
DataTable data = ds.Tables[0];
var obj_Table = new DataTable("Sample_Upload_Expense");
obj_Table.Columns.Add("Id", typeof(Int16));
obj_Table.Columns.Add("Name", typeof(string));
for (int i = 0; i < data.Rows.Count - 1; i++)
{
obj_Table.Rows.Add(data.Rows[i].Field<double>("Id"), data.Rows[i].Field<string>("Name"));
}
var arrParam = new List<SqlParameter>();
var para = new SqlParameter();
para.ParameterName = "@RowList";
para.SqlDbType = System.Data.SqlDbType.Structured;
para.Value = obj_Table;
var pNumRowsChanged = new SqlParameter("@NumRowsChanged", SqlDbType.Int);
pNumRowsChanged.Value = 0;
arrParam.Add(para);
arrParam.Add(pNumRowsChanged);
string proc = "SPInsertExpense";
int no = DataProvider.ProcRetunAffectedRows(proc, arrParam);
return View();
}
Help me. I am stuck from 2 days and I am not able to find the solution.
file path in web.config:
<appSettings>
<add key="excel_file_storage_path" value="D:\webapps\uploads\UploadedExcelDocuments\" />
</appSettings>
I have tried:
- I installed 2007 Office System Driver - Data Connectivity Components to solve if there is any connectivity issue but it doesnt work.
- Tried putting single quotes around the data source.
- I put static path in connection string but that also dont work.
- Also this, though it is not related with my issue.
Finally it worked. I tried this, & it worked. Actuality in connection string, it needs two ; (semicolons) at the end. So my connectionString must be like follow: