Quantcast
Channel: Validate Excel Data, If failed show error
Viewing all articles
Browse latest Browse all 2

Validate Excel Data, If failed show error

0
0

I am trying to find solution for below case, Can any one suggest me solutions/ ideas.....

I have Excel Template like below shared link

http://1drv.ms/1GvXxQE

I need to Import Excel Sheet data and i need to make validate all headers, If any errors I would like to show error message with particular Number and Header to User

Example to validate fields :

Name : Required,ValidString

Date_of_Birth : ValidDate

Age : ValidInteger

Debit : ValidFloat

Amount : ValidCurrency,Required

I have a solution which reads excel file and convert it to Data table...

Created using Visual Studio 2013

http://1drv.ms/1GvXhB6

 public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(HttpPostedFileBase fileName)
        {
            OleDbConnection con;
            string connectionString;
            string[] workSheetNames;

            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName.FileName + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";

            con = new OleDbConnection(connectionString);
            con.Open();

            //get all the available sheets
            System.Data.DataTable dataSet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            //get the number of sheets in the file
            workSheetNames = new String[dataSet.Rows.Count];
            int i = 0;
            foreach (DataRow row in dataSet.Rows)
            {
                //insert the sheet's name in the current element of the array
                //and remove the $ sign at the end
                workSheetNames[i] = row["TABLE_NAME"].ToString().Trim(new[] { '$' });
                i++;
            }
            OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter("select * from [" + workSheetNames[0] + "$]", con);

            DataSet excelDataSet = new DataSet();
            cmd.Fill(excelDataSet);
            con.Close();

            if (con != null)
            {
                con.Close();
                con.Dispose();
            }

            if (dataSet != null)
                dataSet.Dispose();

            string isSucess = ValidateDataTable(excelDataSet.Tables[0]);



            return new EmptyResult();
        }

        private string ValidateDataTable(DataTable dataTable)
        {
            throw new NotImplementedException();
        }
    }

Thanks for your help


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images