2

I have a problem when uploading Excel file to database there is a column in which i have a alpha-numeric values or only numeric values, When reading first row if there is numeric value in my column then it only stores numeric value through out the sheet it does not pick the alpha-numeric values, But if it gets alpha-numeric value in first row then it stores all values through out the sheet

This is My Excel Sheet:

enter image description here

AS you can see in Picture In TRAN DETAIL column ,it easily uploads value to database but when there is alpha numeric value like marked in image it save as null.

This is my code how i am uploading excel file to database,

  [HttpPost]
        public JsonResult UploadExcel(SBP_NEW users, HttpPostedFileBase FileUpload)
        {

            List<string> data = new List<string>();
            if (FileUpload != null)
            {
                // tdata.ExecuteCommand("truncate table OtherCompanyAssets");  
                if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                {


                    string filename = FileUpload.FileName;
                    string targetpath = Server.MapPath("~/Doc/");
                    FileUpload.SaveAs(targetpath + filename);
                    string pathToExcelFile = targetpath + filename;
                    var connectionString = "";
                    if (filename.EndsWith(".xls"))
                    {
                        connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", pathToExcelFile);
                    }
                    else if (filename.EndsWith(".xlsx"))
                    {
                        connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", pathToExcelFile);
                    }

                    var adapter = new OleDbDataAdapter("SELECT * FROM [SBP OST Entries$]", connectionString);
                    var ds = new DataSet();

                    adapter.Fill(ds, "ExcelTable");

                    DataTable dtable = ds.Tables["ExcelTable"];

                    string sheetName = "SBP OST Entries";

                    var excelFile = new ExcelQueryFactory(pathToExcelFile);
                    var artistAlbums = from a in excelFile.Worksheet<SBP_NEW>(sheetName) select a;

                    foreach (var a in artistAlbums)
                    {
                        try
                        {
                            if (a.SEQN_NUMB != "")
                            {
                                SBP_NEW TU = new SBP_NEW();
                                TU.SEQN_NUMB = a.SEQN_NUMB;
                                TU.VALU_DATE = a.VALU_DATE;
                                TU.DESCRIPTION = a.DESCRIPTION;
                                TU.TRAN_DETAIL = a.TRAN_DETAIL;
                                TU.TRAN_AMNT = a.TRAN_AMNT;
                                TU.OTST_AMNT = a.OTST_AMNT;
                                TU.BRAN_DEPT = a.BRAN_DEPT;
                                TU.OUR_THEIR = a.OUR_THEIR;
                                TU.REMARKS = a.REMARKS;
                                TU.INST_NUMB = a.INST_NUMB;
                                TU.TRAN_DATE = a.TRAN_DATE;
                                TU.RECON_MODE = a.RECON_MODE;
                                TU.USER_ID = a.USER_ID;
                                TU.SUPR_RQRD = a.SUPR_RQRD;
                                TU.SUPR_BY = a.SUPR_BY;
                                TU.SUPR_DATE = a.SUPR_DATE;

                                db.SBP_NEW.Add(TU);

                                db.SaveChanges();



                            }
                            else
                            {
                                //data.Add("<ul>");
                                //if (a.ID == 0 || a.ID == null) data.Add("<li> ID is required</li>");
                                //if (a.NAME == "" || a.NAME == null) data.Add("<li> Name is required</li>");


                                //data.Add("</ul>");
                                data.ToArray();
                                return Json(data, JsonRequestBehavior.AllowGet);
                            }
                        }

                        catch (DbEntityValidationException ex)
                        {
                            foreach (var entityValidationErrors in ex.EntityValidationErrors)
                            {

                                foreach (var validationError in entityValidationErrors.ValidationErrors)
                                {

                                    Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);

                                }

                            }
                        }
                    }
                    //deleting excel file from folder  
                    if ((System.IO.File.Exists(pathToExcelFile)))
                    {
                        System.IO.File.Delete(pathToExcelFile);
                    }
                    return Json("success", JsonRequestBehavior.AllowGet);
                }
                else
                {
                    //alert message for invalid file format  
                    data.Add("<ul>");
                    data.Add("<li>Only Excel file format is allowed</li>");
                    data.Add("</ul>");
                    data.ToArray();
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                data.Add("<ul>");
                if (FileUpload == null) data.Add("<li>Please choose Excel file</li>");
                data.Add("</ul>");
                data.ToArray();
                return Json(data, JsonRequestBehavior.AllowGet);
            }
        }
Mussaib Siddiqui
  • 199
  • 2
  • 15

1 Answers1

0

When you enter a long number, concatinate (') of the text , and then transfer it to excel

For example, type '1234567890123456789 and the quotation mark will not be displayed after you press ENTER.

Clik Here for more info

MMM
  • 3,132
  • 3
  • 20
  • 32
  • Actually it is uploading all values shown in image but only when it gets alphanumeric value in first row but when it get numeric value as shown in image in TRAN DETAIL column then it only upload numeric value only neglecting alpha-numeric value – Mussaib Siddiqui Oct 04 '16 at 12:27
  • @MussaibSiddiqui i can see u can upload but if u upload large number it will show in alphanumeric form only Just try by concatenating this will other columns values are showing because - is existing preceding to those numbers – MMM Oct 04 '16 at 12:38
  • ok,But i cant make changes in excel file it is coming from another source – Mussaib Siddiqui Oct 04 '16 at 12:40
  • if you are uploading data to sql using excel mean you can convert hexadecimal numeral system to long and save to your table http://stackoverflow.com/questions/1139957/c-sharp-convert-integer-to-hex-and-back-again – MMM Oct 04 '16 at 12:46
  • Ok last question,how will i access that column? and convert it – Mussaib Siddiqui Oct 04 '16 at 12:50
  • TU.TRAN_DETAIL =ConvertFromHexadecimal( a.TRAN_DETAIL);write a helper method like this – MMM Oct 04 '16 at 12:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124897/discussion-between-midhun-mundayadan-and-mussaib-siddiqui). – MMM Oct 04 '16 at 13:08