Import an Excel 2007 spreadsheet into a GridView in ASP.Net

I had a request earlier today for an existing web-based solution written in C# ASP.Net 2.0 to be able to upload an Excel 2007 spreadsheet and have the site immediately read from that file and put the results into a GridView control on the same page for editing, review, etc. I had never done this before, all of my data access was typically SQL, Access, XML, etc. So I did some light research and ended up writing the following code to accomplish this:

This code assumes you have already added a FileUpload control called “Fileupload1” and a GridView control called “GridView1”

if (Fileupload1.HasFile)
   {
      Fileupload1.SaveAs(Server.MapPath(Fileupload1.FileName));
      string filename = Fileupload1.FileName;
      string serverfilename = Server.MapPath(filename);
      string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + serverfilename + ";Extended Properties="Excel 12.0 Xml;HDR=NO;IMEX=1"";
      OleDbDataAdapter cmd = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);

      DataSet ds = new DataSet();
      cmd.Fill(ds, "XL");
      GridView1.DataSource = ds.Tables["XL"].DefaultView;
      GridView1.DataBind();
   }

This did the trick, worked perfectly for me. If anyone has a better method of accomplishing the same task I welcome your suggestions.

The following ASP.Net forums posting helped a lot: http://forums.asp.net/t/1169005.aspx

Hope this helps someone!

Eric

Eric Oszakiewski is a professional software developer based in Scottsdale, AZ with over 35 years of IT experience, and 19 years Native American Gaming experience. He is currently working as a Sr .Net/SharePoint Developer for General Motors, and also as a consultant.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

STM and .Net 4.0 Beta 1

I installed STM.Net this past week (Software Transactional Memory) as a experimental trial from DevLabs. Although I’m not sure how this environment will help my current enterprise I work for, I’m still curious about learning something new. I haven’t had a chance to try out the code yet this week, but hopefully next week after I set up my new dev environment at work I can install it there too and give it a whirl. I’ll post something here once I have some time to mess with it. If anyone who’s tried this already has any feedback or comments please feel free to post them below.

Eric

Eric Oszakiewski is a professional software developer based in Scottsdale, AZ with over 35 years of IT experience, and 19 years Native American Gaming experience. He is currently working as a Sr .Net/SharePoint Developer for General Motors, and also as a consultant.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

Great upcoming webcasts and seminars

It seems like September through November is heavy on developer and security webcasts. I’m including as many as I can in this entry, if anyone notices others feel free to post them in the comments below.

24 Hours of PASS – 24 one-hour long-ish webcasts from industry experts on SQL related management and development.
geekSpeak: Generics and Collections (Level 200) – Webcast providing details and best practices with collections and lists in .Net Framework 3.5
Windows 7 and Windows Server 2008 R2: Testing your applications for compatibility – the title says it all.
The New Efficiency: IT Professional and Developer Technical Briefings – Phoenix, AZ – This is an in-person event involving Windows 7, Windows server 2008 R2, and Exchange Server 2010
SharePoint 2010 Live Conference and Expo – A live virtual conference regarding all things SharePoint.

Enjoy!

Eric

Eric Oszakiewski is a professional software developer based in Scottsdale, AZ with over 35 years of IT experience, and 19 years Native American Gaming experience. He is currently working as a Sr .Net/SharePoint Developer for General Motors, and also as a consultant.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

New code library site

I created a basic code library site to host all of the code snippets I’ve gathered and/or created over time. Go here to visit the site.

It’s pretty basic and empty right now, I’ll dress it up a little more as time goes on. I’m also trying to determine a better way of sub-categorizing the code in each of the language tabs. I welcome suggestions. Enjoy!

Eric

Eric Oszakiewski is a professional software developer based in Scottsdale, AZ with over 35 years of IT experience, and 19 years Native American Gaming experience. He is currently working as a Sr .Net/SharePoint Developer for General Motors, and also as a consultant.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube

App Pool Crashing – Part 2

When we left off on this issue, the app pool was crashing at semi-random intervals. After much discussion with Microsoft’s IIS forum, Microsoft’s ASP.Net forum (not much help there), and peer development groups (AZGroups.com primarily), I downloaded and installed IISState.exe, which is a command line monitor (basically) that is placed on the IIS server and listens for app pool issues. It’s far more detailed than Event Log or App Pool logging, providing codes at every POST/GET. Additionally, I tried turning app pool recycling completely off with no success, and also tried scheduling a single recycle in the middle of the night, also without success.

I decided to run IISState and see what happened. Upon running I noticed the expected logging, with typical “Ok, that worked” codes being thrown. After almost 2 days of running fine, the app pool finally crashed and IISState captured it. The interesting thing is, the error code it threw was a “C++ EH exception – code e06d7363 (first chance)”. Initial research shows this to be a problem internal to IIS. So far, additional research proves this is either not well known or not well documented. I also researched a forum about the error, and their recommendation was to uninstall and reinstall IIS. Yeah…..not happening. In fact, that solution is insulting. Similar to when your computer is slow, blow away the hard drive and reinstall the OS. Please, I thought we were professionals here!

So, the issue remains unresolved at this time. I had to put it aside temporarily to work on other projects, but it’s very much a priority to discover root cause and resolution. As soon as I get to that point, or if I discover anything else I’ll post it here.

Eric

Eric Oszakiewski is a professional software developer based in Scottsdale, AZ with over 35 years of IT experience, and 19 years Native American Gaming experience. He is currently working as a Sr .Net/SharePoint Developer for General Motors, and also as a consultant.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle PlusYouTube