Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to import excel data into sql server using c# and I am getting conflicts with data set column names which are having dots (like Lev. 1 Fam.,Lev. 1 Fam. Description). Can anyone please suggest me how to overcome from this with any solution asap.

OP code from comment (as is)
[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Upload(HttpPostedFileBase uploadFile)
        {
            StringBuilder strValidations = new StringBuilder(string.Empty);
            try
            {
                if (uploadFile.ContentLength > 0)
                {
                    string tempDirectory = "/TempImportedData"; // your code goes here

                    bool isExists = System.IO.Directory.Exists(Server.MapPath(tempDirectory));
                    if (!isExists)
                        System.IO.Directory.CreateDirectory(Server.MapPath(tempDirectory));

                    string filePath = Path.Combine(HttpContext.Server.MapPath(tempDirectory),
                    Path.GetFileName(uploadFile.FileName));
                    uploadFile.SaveAs(filePath);
                    DataSet ds = new DataSet();
                    //A 32-bit provider which enables the use of

                    string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;";
                    using (OleDbConnection conn = new System.Data.OleDb.OleDbConnection(ConnectionString))
                    {
                        conn.Open();
                        using (DataTable dtExcelSchema = conn.GetSchema("Tables"))
                        {
                            string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                            string query = "SELECT * FROM [" + sheetName + "]";
                            OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
                            //DataSet ds = new DataSet();
                            adapter.Fill(ds, "ProductImportDetails");
                            if (ds.Tables.Count > 0)
                            {
                                if (ds.Tables[0].Rows.Count > 0)
                                {
                                    for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
                                    {
                                        string cmmfCode = ds.Tables[0].Rows[i]["CMMF Code"].ToString();
                                        ProductImportDetails ImportDetails = _productService.GetByCmmfCode(cmmfCode);
                                        if (ImportDetails == null)
                                        {
                                            ProductImportDetails newImportDetails = new ProductImportDetails();
                                            //newImportDetails.ProductId = 1;
                                            newImportDetails.CmmfCode = ds.Tables[0].Rows[i]["CMMF Code"].ToString();
                                            Product product = _productService.GetByDispatchCode(ds.Tables[0].Rows[i]["CMMF Code"].ToString());
                                            if (product != null)
                                            {
                                                newImportDetails.ProductId = product.Id;
                                            }
                                            else if (product == null)
                                            {
                                                continue;
                                            }
                                            newImportDetails.CmmfDescription = ds.Tables[0].Rows[i]["CMMF description (EN)"].ToString();
                                            newImportDetails.CmmfOwnerDesc = ds.Tables[0].Rows[i]["CMMF Description (Owner)"].ToString();
                                            newImportDetails.CommercialCode = ds.Tables[0].Rows[i]["Commercial Code"].ToString();
                                            newImportDetails.SimplifiedCode = ds.Tables[0].Rows[i]["Simplified Commercial code"].ToString();
                                            newImportDetails.IndustrialCode = ds.Tables[0].Rows[i]["Industrial Code"].ToString();
                                            newImportDetails.CmmfType = ds.Tables[0].Rows[i]["CMMF Type"].ToString();
                                            if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["Product Owner"].ToString()))
                                            {
                                                newImportDetails.ProductOwner = int.Parse(ds.Tables[0].Rows[i]["Product Owner"].ToString());
                                            }
                                            newImportDetails.ProductLine = ds.Tables[0].Rows[i]["Product line"].ToString();
                                            if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["General Status"].ToString()))
                                            {
                                                newImportDetails.GeneralStatus = int.Parse(ds.Tables[0].Rows[i]["General Status"].ToString());
                                            }
                                            newImportDetails.Unit = ds.Tables[0].Rows[i]["Unit"].ToString();

                                            if (ds.Tables[0].Rows[i]["Reporting Flag"].ToString() == "Y")
                                            {
                                                newImportDetails.ReportingFlag = true;
                                            }
                                            else if (ds.Tables[0].Rows[i]["Reporting Flag"].ToString() == "N")
                                            {
                                                newImportDetails.ReportingFlag = false;
                                            }
                                            else
                                            {
                                                newImportDetails.ReportingFlag = false;
                                            }
                                            if (ds.Tables[0].Rows[i]["Sourced"].ToString() == "N")
                                            {
                                                newImportDetails.Sourced = false;
                                            }
                                            else if (ds.Tables[0].Rows[i]["Sourced"].ToString() == "Y")
                                            {
                                                newImportDetails.Sourced = true;
                                            }
                                            else
                                            {
                                                newImportDetails.Sourced = false;
                                            }
                                            if (ds.Tables[0].Rows[i]["Set Split"].ToString() == "N")
                                            {
                                                newImportDetails.SetSplit = false;
                                            }
                                            else if (ds.Tables[0].Rows[i]["Set Split"].ToString() == "Y")
                                            {
                                                newImportDetails.SetSplit = true;
                                            }
                                            else
                                            {
                                                newImportDetails.SetSplit = false;
                                            }
                                            newImportDetails.Model = ds.Tables[0].Rows[i]["Model"].ToString();
                                            newImportDetails.ModelDescription = ds.Tables[0].Rows[i]["Model description"].ToString();
                                            newImportDetails.Range = ds.Tables[0].Rows[i]["Range"].ToString();
                                            newImportDetails.RangeDescription = ds.Tables[0].Rows[i]["Range description"].ToString();
                                            newImportDetails.RRI = ds.Tables[0].Rows[i]["RRI"].ToString();
                                            newImportDetails.RRIDescription = ds.Tables[0].Rows[i]["RRI description"].ToString();
                                            newImportDetails.ProdSite = ds.Tables[0].Rows[i]["Prod Site"].ToString();
                                            newImportDetails.ProdSiteDescription = ds.Tables[0].Rows[i]["Prod Site Description"].ToString();
                                            newImportDetails.LevelOneFam = ds.Tables[0].Rows[i]["Lev. 1 Fam."].ToString();
                                            newImportDetails.LevelOneFamDescripiton = ds.Tables[0].Rows[i]["Lev. 1 Fam. Description"].ToString();
                                            newImportDetails.LevelTwoFam = ds.Tables[0].Rows[i]["Lev. 2 Fam."].ToString();
                                            newImportDetails.LevelTwoFamDescription = ds.Tables[0].Rows[i]["Lev. 2 Fam. Description"].ToString();
                                            newImportDetails.ValidationStatus = ds.Tables[0].Rows[i]["Validation status"].ToString();
                                            newImportDetails.Origin = ds.Tables[0].Rows[i]["Origin"].ToString();
                                            if (ds.Tables[0].Rows[i]["Product with packaging composition"].ToString() == "N")
                                            {
                                                newImportDetails.PackagingComposition = false;
                                            }
                                            else if (ds.Tables[0].Rows[i]["Product with packaging composition"].ToString() == "Y")
                                            {
                                                newImportDetails.PackagingComposition = true;
                                            }
                                            else
                                            {
                                                newImportDetails.PackagingComposition = false;
                                            }
                                            if (ds.Tables[0].Rows[i]["Product with sales"].ToString() == "N")
                                            {
                                                newImportDetails.ProductWithSales = false;
                                            }
                                            else if (ds.Tables[0].Rows[i]["Product with sales"].ToString() == "Y")
                                            {
                                                newImportDetails.ProductWithSales = true;
                                            }
                                            else
                                            {
                                                newImportDetails.ProductWithSales = false;
                                            }
                                            newImportDetails.MaterialCode = ds.Tables[0].Rows[i]["Material Code"].ToString();
                                            newImportDetails.MaterialDescription = ds.Tables[0].Rows[i]["Material Description"].ToString();
                                            // newImportDetails.ComponentWeight = null ? 0 : int.Parse(ds.Tables[0].Rows[i]["Component Weight"].ToString());
                                            if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["Component Weight"].ToString()))
                                            {
                                                newImportDetails.ComponentWeight = Decimal.Parse(ds.Tables[0].Rows[i]["Component Weight"].ToString());
                                            }
                                            if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["Packaging Level"].ToString()))
                                            {
                                                newImportDetails.PackagingLevel = int.Parse(ds.Tables[0].Rows[i]["Packaging Level"].ToString());
                                            }
                                            if (ds.Tables[0].Rows[i]["Without Level 1"].ToString() == "N")
                                            {
                                                newImportDetails.WithoutLevelOne = false;
                                            }
                                            else if (ds.Tables[0].Rows[i]["Without Level 1"].ToString() == "Y")
                                            {
                                                newImportDetails.WithoutLevelOne = true;
                                            }
                                            else
                                            {
                                                newImportDetails.WithoutLevelOne = false;
                                            }
                                            if (ds.Tables[0].Rows[i]["Without Level 2"].ToString() == "N")
                                            {
                                                newImportDetails.WithoutLevelTwo = false;
                                            }
                                            else if (ds.Tables[0].Rows[i]["Without Level 2"].ToString() == "Y")
                                            {
                                                newImportDetails.WithoutLevelTwo = true;
                                            }
                                            else
                                            {
                                                newImportDetails.WithoutLevelTwo = false;
                                            }

                                            _productService.AddImportDetails(newImportDetails, User.Identity.Name);
                                        }
                                        else
                                        {
                                            if (ImportDetails != null)
                                            {
                                                ImportDetails = new ProductImportDetails();
                                                //ImportDetails.ProductId = 1;
                                                ImportDetails.CmmfCode = ds.Tables[0].Rows[i]["CMMF Code"].ToString();
                                                Product product = _productService.GetByDispatchCode(ImportDetails.CmmfCode);
                                                if (product != null)
                                                {
                                                    ImportDetails.ProductId = product.Id;
                                                }
                                                else if (product == null)
                                                {
                                                    continue;
                                                }
                                                ImportDetails.CmmfDescription = ds.Tables[0].Rows[i]["CMMF description (EN)"].ToString();
                                                ImportDetails.CmmfOwnerDesc = ds.Tables[0].Rows[i]["CMMF Description (Owner)"].ToString();
                                                ImportDetails.CommercialCode = ds.Tables[0].Rows[i]["Commercial Code"].ToString();
                                                ImportDetails.SimplifiedCode = ds.Tables[0].Rows[i]["Simplified Commercial code"].ToString();
                                                ImportDetails.IndustrialCode = ds.Tables[0].Rows[i]["Industrial Code"].ToString();
                                                ImportDetails.CmmfType = ds.Tables[0].Rows[i]["CMMF Type"].ToString();
                                                if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["Product Owner"].ToString()))
                                                {
                                                    ImportDetails.ProductOwner = int.Parse(ds.Tables[0].Rows[i]["Product Owner"].ToString());
                                                }
                                                ImportDetails.ProductLine = ds.Tables[0].Rows[i]["Product line"].ToString();
                                                if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["General Status"].ToString()))
                                                {
                                                    ImportDetails.GeneralStatus = int.Parse(ds.Tables[0].Rows[i]["General Status"].ToString());
                                                }
                                                ImportDetails.Unit = ds.Tables[0].Rows[i]["Unit"].ToString();

                                                if (ds.Tables[0].Rows[i]["Reporting Flag"].ToString() == "Y")
                                                {
                                                    ImportDetails.ReportingFlag = true;
                                                }
                                                else if (ds.Tables[0].Rows[i]["Reporting Flag"].ToString() == "N")
                                                {
                                                    ImportDetails.ReportingFlag = false;
                                                }
                                                else
                                                {
                                                    ImportDetails.ReportingFlag = false;
                                                }
                                                if (ds.Tables[0].Rows[i]["Sourced"].ToString() == "N")
                                                {
                                                    ImportDetails.Sourced = false;
                                                }
                                                else if (ds.Tables[0].Rows[i]["Sourced"].ToString() == "Y")
                                                {
                                                    ImportDetails.Sourced = true;
                                                }
                                                else
                                                {
                                                    ImportDetails.Sourced = false;
                                                }
                                                if (ds.Tables[0].Rows[i]["Set Split"].ToString() == "N")
                                                {
                                                    ImportDetails.SetSplit = false;
                                                }
                                                else if (ds.Tables[0].Rows[i]["Set Split"].ToString() == "Y")
                                                {
                                                    ImportDetails.SetSplit = true;
                                                }
                                                else
                                                {
                                                    ImportDetails.SetSplit = false;
                                                }
                                                ImportDetails.Model = ds.Tables[0].Rows[i]["Model"].ToString();
                                                ImportDetails.ModelDescription = ds.Tables[0].Rows[i]["Model description"].ToString();
                                                ImportDetails.Range = ds.Tables[0].Rows[i]["Range"].ToString();
                                                ImportDetails.RangeDescription = ds.Tables[0].Rows[i]["Range description"].ToString();
                                                ImportDetails.RRI = ds.Tables[0].Rows[i]["RRI"].ToString();
                                                ImportDetails.RRIDescription = ds.Tables[0].Rows[i]["RRI description"].ToString();
                                                ImportDetails.ProdSite = ds.Tables[0].Rows[i]["Prod Site"].ToString();
                                                ImportDetails.ProdSiteDescription = ds.Tables[0].Rows[i]["Prod Site Description"].ToString();
                                                ImportDetails.LevelOneFam = ds.Tables[0].Rows[i]["LevelOneFam"].ToString();
                                                ImportDetails.LevelOneFamDescripiton = ds.Tables[0].Rows[i]["LevelOneFamDescripiton"].ToString();
                                                ImportDetails.LevelTwoFam = ds.Tables[0].Rows[i]["LevelTwoFam"].ToString();
                                                ImportDetails.LevelTwoFamDescription = ds.Tables[0].Rows[i]["LevelTwoFamDescription"].ToString();
                                                ImportDetails.ValidationStatus = ds.Tables[0].Rows[i]["Validation status"].ToString();
                                                ImportDetails.Origin = ds.Tables[0].Rows[i]["Origin"].ToString();
                                                if (ds.Tables[0].Rows[i]["Product with packaging composition"].ToString() == "N")
                                                {
                                                    ImportDetails.PackagingComposition = false;
                                                }
                                                else if (ds.Tables[0].Rows[i]["Product with packaging composition"].ToString() == "Y")
                                                {
                                                    ImportDetails.PackagingComposition = true;
                                                }
                                                else
                                                {
                                                    ImportDetails.PackagingComposition = false;
                                                }
                                                if (ds.Tables[0].Rows[i]["Product with sales"].ToString() == "N")
                                                {
                                                    ImportDetails.ProductWithSales = false;
                                                }
                                                else if (ds.Tables[0].Rows[i]["Product with sales"].ToString() == "Y")
                                                {
                                                    ImportDetails.ProductWithSales = true;
                                                }
                                                else
                                                {
                                                    ImportDetails.ProductWithSales = false;
                                                }
                                                ImportDetails.MaterialCode = ds.Tables[0].Rows[i]["Material Code"].ToString();
                                                ImportDetails.MaterialDescription = ds.Tables[0].Rows[i]["Material Description"].ToString();
                                                // newImportDetails.ComponentWeight = null ? 0 : int.Parse(ds.Tables[0].Rows[i]["Component Weight"].ToString());
                                                if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["Component Weight"].ToString()))
                                                {
                                                    ImportDetails.ComponentWeight = Decimal.Parse(ds.Tables[0].Rows[i]["Component Weight"].ToString());
                                                }
                                                if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["Packaging Level"].ToString()))
                                                {
                                                    ImportDetails.PackagingLevel = int.Parse(ds.Tables[0].Rows[i]["Packaging Level"].ToString());
                                                }
                                                if (ds.Tables[0].Rows[i]["Without Level 1"].ToString() == "N")
                                                {
                                                    ImportDetails.WithoutLevelOne = false;
                                                }
                                                else if (ds.Tables[0].Rows[i]["Without Level 1"].ToString() == "Y")
                                                {
                                                    ImportDetails.WithoutLevelOne = true;
                                                }
                                                else
                                                {
                                                    ImportDetails.WithoutLevelOne = false;
                                                }
                                                if (ds.Tables[0].Rows[i]["Without Level 2"].ToString() == "N")
                                                {
                                                    ImportDetails.WithoutLevelTwo = false;
                                                }
                                                else if (ds.Tables[0].Rows[i]["Without Level 2"].ToString() == "Y")
                                                {
                                                    ImportDetails.WithoutLevelTwo = true;
                                                }
                                                else
                                                {
                                                    ImportDetails.WithoutLevelTwo = false;
                                                }
                                                _productService.AddImportDetails(ImportDetails, User.Identity.Name);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TempData["ImportMessage"] = "Data import failed." + ex.ToString();
                return RedirectToAction("Index");
            }
            TempData["ImportMessage"] = "Data has been imported successfully.";
            return RedirectToAction("Index");
        }
    }
}
Posted
Updated 3-Mar-15 1:04am
v3
Comments
Mehdi Gholam 3-Mar-15 6:09am    
Decide what you want to do with the dots.
CHill60 3-Mar-15 6:11am    
You haven't described how you are doing the import but you could also surround the column names with square brackets [] - or choose more sensible titles
CHill60 3-Mar-15 6:44am    
And where is the error raised? I am able to have column names of "Lev.1 Fam" and "Lev.1 Fam. Description" in a datatable
Sinisa Hajnal 3-Mar-15 8:48am    
Please move this code into the question and format it properly. Thank you.
Solai Raja 3-Mar-15 7:05am    
1. Make sure the datatypes between both database and excel sheets values are same.
2. Also try to remove undefined characters from your string.
3. check datatype ranges like varchar of columns. that string can accept unlimited chars but for DB varchar you should give a range (Varchar(250))

1 solution

Hi, Now you can see the line which contains this dataset names and there is the error raised while importing. The error is "The column name does not exist in the current table."
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900