Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:22:41+00:00 2026-05-16T11:22:41+00:00

Excel 2003 = literature.xls Sheets: LineCards, Data, Brochures, and Tips using System; using System.Collections;

  • 0

Excel 2003 = literature.xls
Sheets: LineCards, Data, Brochures, and Tips

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;

public partial class literature : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    LoadGrid(0);
}
protected void grd_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    LoadGrid(e.NewPageIndex);
}

void LoadGrid(int LineCards)
{
    String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("literature\\literature.xls") + ";" +
"Extended Properties=Excel 8.0;";

    // Create connection object by using the preceding connection string.
    OleDbConnection objConn = new OleDbConnection(sConnectionString);

    // Open connection with the database.
    objConn.Open();

    // The code to follow uses a SQL SELECT command to display the data from the worksheet.

    // Create new OleDbCommand to return data from worksheet.
    OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [LineCards$]", objConn);

    // Create new OleDbDataAdapter that is used to build a DataSet
    // based on the preceding SQL SELECT statement.
    OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

    // Pass the Select command to the adapter.
    objAdapter1.SelectCommand = objCmdSelect;

    // Create new DataSet to hold information from the worksheet.
    DataSet objDataset1 = new DataSet();

    // Fill the DataSet with the information from the worksheet.
    objAdapter1.Fill(objDataset1, "XLData");

    // Bind data to DataGrid control.
    grd.DataSource = objDataset1.Tables[0].DefaultView;
    grd.PageIndex = LineCards;
    grd.DataBind();

    // Clean up objects.
    objConn.Close();
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:22:41+00:00Added an answer on May 16, 2026 at 11:22 am

    You’re going to need to perform this code once for each file. Essentially, just create a function to take care of it, and just pass the function your connectionString (or whatever element of it changes). This assumes that all of the files you are querying have the correct data you are looking for.

    Here is an example of the function, and how to call it.

    //I don't know what values LineCards is supposed to be, so I am just passing 5, 6, and 7.
    //Put these calls where your LoadGrid() call is currently.
    
    assignExcelSheetToGrid (Server.MapPath("literature\\literature.xls"), grd, 5);
    assignExcelSheetToGrid (Server.MapPath("literature\\literature2.xls"), grd2, 6);
    assignExcelSheetToGrid (Server.MapPath("literature\\literature3.xls"), grd3, 7);
    

    And the function…. I assume your first function worked, so I am just re-using your code. If your original function worked for one grid, this code should work for any number.

    function assignExcelSheetToGrid(string thePath, YOURGRIDTYPE theGrid, int LineCards){
        ///This replaces LoadGrid function
        //Make sure you change YOURGRIDTYPE (Above) to the type of grid you are passing
    
        String theConnString= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + thePath + ";Extended Properties=Excel 8.0;";
    
        OleDbConnection objConn = new OleDbConnection(theConnString);
        objConn.Open();
    
        OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [LineCards$]", objConn);
        OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
    
        objAdapter1.SelectCommand = objCmdSelect;
        DataSet objDataset1 = new DataSet();
        objAdapter1.Fill(objDataset1, "XLData");
    
        theGrid.DataSource = objDataset1.Tables[0].DefaultView;
        theGrid.PageIndex = LineCards;
        theGrid.DataBind();
    
        objConn.Close();
    }
    

    UPDATE

    function now takes the file path instead of the entire connection string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using Excel (2003) to provide data for my unit tests it seems to
I use Excel 2003 on a Windows 7 Professional setup. In my Personal.xls file,
I have a set od Excel (2003) sheets saved as .xlt format. Now ,how
I have written a few Excel 2003/2007 add-ins using VSTO, and I usually end
I want to import data from an Excel file - assume Excel 2003 /
I am using ASP.NET to open an Excel 2003 document hosted on the server.
In Excel 2003 Files are saved with extension .xls and in Excel 2007 .xlsm
In Excel 2003 I can access the collection Application.CommandBars to create custom toolbars using
I'm using VBA excel 2003,SQL 2005 to make a sql query call and inside
I have an Excel 2003 worksheet with protection enabled being used for data entry

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.