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

  • SEARCH
  • Home
  • 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 6232419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:06:50+00:00 2026-05-24T10:06:50+00:00

i am currently working on importing data from excel sheet to sql server.i have

  • 0

i am currently working on importing data from excel sheet to sql server.i have written a code for removing empty rows from the excel sheet..now i am trying to see the data in datagridview by assigning the method name to the datagridview datasource..but it is not displaying any data.Can anyone say me if i am doing correct or wrong. This is my code..

   public partial class Form9 : Form
{
    DataSet dsExcel = new DataSet();

    public Form9()
    {
        InitializeComponent();
    }

    private void btnUpload_Click(object sender, EventArgs e)
    {
        //String input = string.Empty;
        OpenFileDialog filepath = new OpenFileDialog();
        filepath.Filter = "Excel Files(*.xls)|*.xls|Excel Files(*.xlsx)|*.xlsx|Text Files(*.txt)|*.txt|All Files(*.*)|*.*";
        filepath.InitialDirectory = "C:";
        filepath.Title = "Select a file";
        if (filepath.ShowDialog() == DialogResult.OK)
            txtExcelFile.Text = filepath.FileName;
        if (txtExcelFile.Text == string.Empty)
            return;
    }

    private void btnImport_Click(object sender, EventArgs e)
    {


        OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + txtExcelFile.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'");
        cnn.Open();
        try
        {

            OleDbDataAdapter data = new OleDbDataAdapter("select * from [Customers$]", cnn);

            data.Fill(dsExcel);

            dgvCustomers.ColumnHeadersVisible = false;
            dgvCustomers.DataSource = GetExcelData();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            //cnn.Close();
        }

    }

       public DataSet GetExcelData()
        {
        DataTable ExcelTable = new DataTable();
        List<int> rowToRemove = new List<int>();
        //DataRow ExcelRow = new DataRow();

        foreach (DataRow excelrow in dsExcel.Tables[0].Rows)
        {
            bool IsEmpty = false;
            foreach (object item in excelrow.ItemArray)
            {
                if (String.IsNullOrEmpty(item.ToString())) 
                {
                     IsEmpty = true;
                     break;

                }                  
                else
                {
                    IsEmpty = false;

                }
            }
            if (IsEmpty)
            { 
                rowToRemove.Add((dsExcel.Tables[0].Rows.IndexOf(excelrow)));
            }

        }

        for (int i = rowToRemove.Count - 1; i >= 0; i--)
        {
            dsExcel.Tables[0].Rows.RemoveAt(rowToRemove[i]);

        }
        return dsExcel;

    }
}

}

  • 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-24T10:06:50+00:00Added an answer on May 24, 2026 at 10:06 am

    Your datasource should be a datatable in the dataset, not the dataset itself.

    Change

    public DataSet GetExcelData()
    

    to

    public DataTable GetExcelData()
    

    and at the end, return this:

    return dsExcel.Tables[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working through Accelerated C++ and have come across an issue in
I'm currently working with an NSPersistentDocument subclass that uses NSOperation to import data in
I am currently working on restructuring my site's database. As the schema I have
Here is my code. You have to kindly look does it suffer from 'same
I'm currently working on a data migration project and for performance-related issues, I want
I have recenty came across a problem while importing older data than my current
Im currently working on a large project and I have built all the different
I currently working on a application that use Entity Framework 4.1 (code first) and
For the site I'm currently working on, I have a list of products which
I am currently working on automating/improving the release process for packaging my shop's entire

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.