Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my Project i have to upload data from excel to sql server.


I am using LinqToSql
this is my code
C#
public ActionResult UploadDataToSql()
        {
            CustomerAccount account = new CustomerAccount();
            string fileName = @"C:\Users\MR Right\Desktop\Customers.xlsx";
            var excel = new ExcelQueryFactory(fileName);
            var dataContent = from c in excel.Worksheet<CustomerSheet>("Customers")
                              select c;
            excel.AddMapping<CustomerSheet>(x => x.ID, account.SFId);
            excel.AddMapping<CustomerSheet>(x => x.Name, account.AccountName);
            excel.AddMapping<CustomerSheet>(x => x.BillingStreet, account.StreetAddress1);
            excel.AddMapping<CustomerSheet>(x => x.BillingState, account.State.ToString());
            excel.AddMapping<CustomerSheet>(x => x.BillingPostal, account.ZipCode);
            excel.AddMapping<CustomerSheet>(x => x.Phone, account.Country);
            excel.AddMapping<CustomerSheet>(x => x.CreatedDate, account.CreatedDate.ToString());
            excel.AddMapping<CustomerSheet>(x => x.LastModifiedDate, account.LastModifiedDate.ToString());
            //Checking the datacontent before accessing
            if (dataContent != null)
            {
                //reading content from the excel comes here
                foreach (var a in dataContent)
                {
                    //all logics and save to database
                }

            }
            return View("~/Views/Admin/UploadData.cshtml");
        }

In Line
C#
foreach (var a in dataContent) 

in a i am getting null values
means with this code i am unable to read excel
can some one help
Thanks
Posted
Updated 21-May-14 0:19am
v3

1 solution

 
Share this answer
 
Comments
VICK 27-May-14 0:00am    
OP,s requirement is using LINQtoSQL and not simple asp.net or C#.

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