May 11, 2011

Read and write Excel File in Asp.net



In this article, we will see how to display data from an Excel spreadsheet using ASP.NET. We will connect to a Microsoft Excel workbook using the OLEDB.NET data provider, extract data and then display the data in a GridView. Let us get started.
Step 1: Open Visual Studio > File > New >Website > Under Templates, click ASP.NET WebSite and choose either Visual C# or Visual Basic as the language. Select a location and click Ok.
Step 2: We will create two excel sheets and add them to the project. One excel sheet will be created in Office 2003(.xls) and the other one using Office 2007(.xlsx). Add 4 columns called EID, EName, Age and City to the ‘Sheet1’. Also add some data into the columns. Once these excel files are created, add them to your project. To add them to the project, right click project > Add Existing Item > Add the two excel files.
Step 3: We will now create a web.config file to store the connection string information. Right click project > Add New Item > Web.config. Add the following entries to the file
      <connectionStrings>
            <add name="xls" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Sample1.xls;Extended Properties=Excel 8.0"/>
            <add name="xlsx" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Sample.xlsx;Extended Properties=Excel 12.0"/>
      connectionStrings>
As you can observe, the connection string for xlsx (Excel 2007) contains Microsoft.ACE.OLEDB.12.0 as the provider. This is the new Access database engine OLE DB driver and is also capable of reading Excel 2003.
Step 4: Add a GridView to the Default.aspx page. We will extract  data from the excel file and bind it to the GridView.
Step 5: Let us now create a connection to the excel file and extract data from it. Before that add a reference to System.Data.OleDb;
C#
    protected void Page_Load(object sender, EventArgs e)
    {
        string connString = ConfigurationManager.ConnectionStrings["xls"].ConnectionString;
        // Create the connection object
        OleDbConnection oledbConn = new OleDbConnection(connString);
        try
        {
            // Open connection
            oledbConn.Open();
            // Create OleDbCommand object and select data from worksheet Sheet1
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
            // Create new OleDbDataAdapter
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            oleda.SelectCommand = cmd;
            // Create a DataSet which will hold the data extracted from the worksheet.
            DataSet ds = new DataSet();
            // Fill the DataSet from the data extracted from the worksheet.
            oleda.Fill(ds, "Employees");
            // Bind the data to the GridView
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();
        }
        catch
        {
        }
        finally
        {
            // Close connection
            oledbConn.Close();
        }     
    }
VB.NET
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim connString As String = ConfigurationManager.ConnectionStrings("xls").ConnectionString
            ' Create the connection object
            Dim oledbConn As OleDbConnection = New OleDbConnection(connString)
            Try
                  ' Open connection
                  oledbConn.Open()
                  ' Create OleDbCommand object and select data from worksheet Sheet1
                  Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn)
                  ' Create new OleDbDataAdapter
                  Dim oleda As OleDbDataAdapter = New OleDbDataAdapter()
                  oleda.SelectCommand = cmd
                  ' Create a DataSet which will hold the data extracted from the worksheet.
                  Dim ds As DataSet = New DataSet()
                  ' Fill the DataSet from the data extracted from the worksheet.
                  oleda.Fill(ds, "Employees")
                  ' Bind the data to the GridView
                  GridView1.DataSource = ds.Tables(0).DefaultView
                  GridView1.DataBind()
            Catch
            Finally
                  ' Close connection
                  oledbConn.Close()
            End Try
      End Sub
All set!! Run the application and see the data getting displayed in the GridView. If you want to target the Excel 2007 sheet, just change xls to xlsx in the ConfigurationManager.ConnectionString.
I hope this article was useful and I thank you for viewing it.

Google Finally Delivers On Promise Of Admin-Free Chrome Frame Installs For IE


Today at Google I/O in San Francisco, Google developers Amit Joshi and Alex Russell took the stage at a breakout session to announce an important milestone: a version of Chrome Frame that doesn’t require admin rights to install.
While this might not jump out at you as a huge thing, it’s big news for millions of users stuck working at offices or schools with older machines where admins won’t allow them to upgrade to newer, modern browsers. And some 30 percent of these users were not even able to install plug-ins for IE. Now there’s a way around that with the new dev build of Chrome Frame. You can install this within IE without needing admin access.
Huge.
Google has actually been working on this problem for months. While Chrome Frame was always a great idea for helping the world ween itself off IE6, 7, and 8, the admin issue remained a large one as users couldn’t do much without this access to install the plug-in. Google has finally figured out a way around that.
Russell was quick to note however that they also offer a range of tools for admins to use if they choose to, in order to set boundaries for Chrome Frame. But the admin issue was “a major roadblock” to the quick iterations that the entire Chrome team loves to do.
So how did the team get around this problem? “Let’s talk backstage,” Joshi said with a big smile when asked this question. He said it’s a bit of a technical discussion. Russell said at a high level it was just about getting the technology to work in the browser itself rather than being seen as a traditional plug-in.
When asked what Microsoft thought of this, neither Joshi nor Russell would comment. That probably says all you need to know right there.
Again, this new feature is a part of the new dev build of Chrome Frame right now. It will be tested here and then roll out to the beta channel and then the stable channel eventually.

October 9, 2010

jQuery intellisense in Visual Studio 2008


To enable intellisense completion for jQuery within VS you'll want to follow three steps:
Step 1: Install VS 2008 SP1
VS 2008 SP1 adds richer JavaScript intellisense support to Visual Studio, and adds code completion support for a broad range of JavaScript libraries.
You can download VS 2008 SP1 and Visual Web Developer 2008 Express SP1 here.
Step 2: Install VS 2008 Patch KB958502 to Support "-vsdoc.js" Intellisense Files
Two weeks ago we shipped a patch that you can apply to VS 2008 SP1 and VWD 2008 Express SP1 that causes Visual Studio to check for the presence of an optional "-vsdoc.js" file when a JavaScript library is referenced, and if present to use this to drive the JavaScript intellisense engine.
These annotated "-vsdoc.js" files can include XML comments that provide help documentation for JavaScript methods, as well as additional code intellisense hints for dynamic JavaScript signatures that cannot automatically be inferred.  You can learn more about this patch here.  You can download it for free here.
Step 3: Download the jQuery-vsdoc.js file
We've worked with the jQuery team to put together a jQuery-vsdoc.js file that provides help comments and support for JavaScript intellisense on chained jQuery selector methods.  You can download both jQuery and the jQuery-vsdoc file from the official download page on the jQuery.com site:
step1 Save the jquery-vsdoc.js file next to your jquery.js file in your project (and make sure its naming prefix matches the jquery file name):step2 You can then reference the standard jquery file with an html <script/> element like so:step3
Or alternatively reference it using the <asp:scriptmanager/> control, or by adding a /// <reference/> comment at the top of a standalone .js file.
When you do this VS will now look for a -vsdoc.js file in the same directory as the script file you are referencing, and if found will use it for help and intellisense.  The annotated
For example, we could use jQuery to make a JSON based get request, and get intellisense for the method (hanging off of $.):
step4 step5 step6 step7

October 6, 2010

Insufficient available memory to meet the expected demands of an operation at this time, possibly due to virtual address space fragmentation


                        This message appears when Visual Studio crashes and virtual memory is low. However, this does not mean that that the virtual memory on your system is low, but rather that Visual Studio is running out of address space. Most commonly, this error occurs on machines with 32-bit operating systems, which limit Visual Studio to a 2GB address space. On 64-bit systems, the error is rare.

There is possibly a solution for this problem from Microsoft.

Download this Microsoft Hotfix:: Click here

September 22, 2010

Joomla Connection String



Below you will find some examples of where to find and how to edit the connection strings in some of the more common Content Management Systems such as Joomla and Wordpress Blogs.


Joomla


The file that you will need to look for in the root of your Joomla installation directory is:

            configuration.php

The section of the document that you will most likely need to edit should look similar to the following. This is where you will need to put in your new information if you are switching to or coming from a different hosting company.
      your example Configuration.php
      $hostname='mysqlX.gearhost.com'; var $host='mysqlX.gearhost.com';
      username='your database user'; var $user ='uid';
      $password='your password'; var $password ='your password';
      $dbname='your database name'; var $dbname='your database name';



Wordpress


wordpress
The file that you will need to look for in the root of your Wordpress installation directory is:
      wp-config.php

The section of the document that you will most likely need to edit should look similar to the following. This is where you will need to put in your new information if you are switching to or coming from a different hosting company.
      // ** MySQL settings ** //
      define('DB_NAME', 'databasename');    // The name of the database
      define('DB_USER', 'databaseuser');     // Your MySQL username
      define('DB_PASSWORD', 'databasepassword'); // ...and password
      define('DB_HOST', 'mysqlX.gearhost.com');    // 99% chance you won't need to change this value



DNN (Dot Net Nuke)


The file that you will need to look for in the root of your DNN installation directory is:

      web.config

The section of the document that you will most likely need to edit should look similar to the following. This is where you will need to put in your new information if you are switching to or coming from a different hosting company.