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 6360265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:37:37+00:00 2026-05-24T23:37:37+00:00

I am working on importing data from an Excel sheet to database. The Excel

  • 0

I am working on importing data from an Excel sheet to database. The Excel sheet contains few empty rows and I want to remove those empty rows, then insert cleared data into database.
I have written a code by referring other code, this is the code for inserting values:

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

try
{
    cnn.Open();
    OleDbDataAdapter data = new OleDbDataAdapter("select * from [Customers$]", cnn);
    data.Fill(dsExcel);
    dgvCustomers.ColumnHeadersVisible = false;

    SqlConnection connection = new SqlConnection("Data Source=COMPUTER-8EB749;Initial Catalog=KITS;Integrated Security=true");
    connection.Open();
    for (int i = 0; i < dsExcel.Tables[0].Rows.Count; i++)
    {
        string ID = ds.Tables[0].Rows[i][0].ToString();
        Int16 CustID = Convert.ToInt16(ID);
        string CustName = dsExcel.Tables[0].Rows[i][1].ToString();
        string CardScheme = dsExcel.Tables[0].Rows[i][2].ToString();
        string Outlet = dsExcel.Tables[0].Rows[i][3].ToString();
        string TerminalNum = dsExcel.Tables[0].Rows[i][4].ToString();
        Int32 Terminal = Convert.ToInt32(TerminalNum);
        string Date1 = dsExcel.Tables[0].Rows[i][5].ToString();
        DateTime Date = Convert.ToDateTime(Date1);
        string Time = dsExcel.Tables[0].Rows[i][6].ToString();
        DateTime DateTime = Convert.ToDateTime(Time);
        string Amount1 = ds.Tables[0].Rows[i][7].ToString();
        double Amount = Convert.ToDouble(Amount1);

        SqlCommand com = new SqlCommand("insert into Customer(CustID,CustName,CardScheme,Outlet,TerminalNum,TranDate,TranDateTime,Amount) values ('" + CustID + "','" + CustName + "','" + CardScheme + "','" + Outlet + "','" + Terminal + "','" + Date + "','" + DateTime + "','" + Amount + "')", connection);
        com.ExecuteNonQuery();
    }
    connection.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    MessageBox.Show("Data Inserted Successfully.");
}

Can anyone say me how can I remove empty rows so that i can insert only data?!

Excel Sheet

  • 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-24T23:37:38+00:00Added an answer on May 24, 2026 at 11:37 pm

    Try this.

    public bool InsertRowsToDataBase()
    {
        try
        {
            DataTable excelTable = new DataTable();
    
            string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + txtExcelFile.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
            using (OleDbConnection cnn = new OleDbConnection(connString))
            {
                string query = "select * from [Customers$]";
                using (OleDbDataAdapter data = new OleDbDataAdapter(query, cnn))
                {
                    data.Fill(excelTable);
                }
            }
            dgvCustomers.ColumnHeadersVisible = false;
    
            connString = "Data Source=COMPUTER-8EB749;Initial Catalog=KITS;Integrated Security=true";
            using (SqlConnection connection = new SqlConnection(connString))
            {
                connection.Open();
                for (int i = 0; i < excelTable.Rows.Length; i++)
                {
                    //takes from the 3rd row
                    if (i > 1)
                    {
                        DataRow row = excelTable.Rows[i];
                        object ID = row[0];
                        if (ID != null && !String.IsNullOrEmpty(ID.ToString().Trim()))
                        {
                            Int16 CustID = Convert.ToInt16(ID);
                            string CustName = row[1].ToString();
                            string CardScheme = row[2].ToString();
                            string Outlet = row[3].ToString();
                            string TerminalNum = row[4].ToString();
                            Int32 Terminal = Convert.ToInt32(TerminalNum);
                            string Date1 = row[5].ToString();
                            DateTime Date = Convert.ToDateTime(Date1);
                            string Time = row[6].ToString();
                            DateTime DateTime = Convert.ToDateTime(Time);
                            string Amount1 = row[7].ToString();
                            double Amount = Convert.ToDouble(Amount1);
    
                            string columnNames = "CustID,CustName,CardScheme,Outlet,TerminalNum,TranDate,TranDateTime,Amount";
                            string query = String.Format("insert into Customer(0}) values ('{1}', '{2}','{3}','{4}','{5}','{6}','{7}','{8}')",
                                columnNames, CustID, CustName, CardScheme, Outlet, Terminal, Date, DateTime, Amount);
                            using (SqlCommand com = new SqlCommand(query, connection))
                            {
                                com.ExecuteNonQuery();
                            }
                        }
                    }
                    //this is your last row. do whatever you want with this
                    DataRow lastRow = excelTable.Rows[excelTable.Rows.Count - 1];
                }
            }
            return true;
        }
        catch (Exception exception)
        {
            Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
            return false;
        }
    }
    

    Please note that I am just checking if ID is null and not inserting any such rows as ID will be the PK in your table.

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

Sidebar

Related Questions

i am currently working on importing data from excel sheet to sql server.i have
i am preparing myself for importing data from excel sheet to sql server 2005.I
I'm working on a project where we are dealing with importing/exporting data from database
I'm importing more than 600.000.000 rows from an old database/table that has no primary
I'm working on an SSIS package where I'm importing data from a CSV file
I'm working on a web service that fetches data from an oracle data source
I'm working with a remote db for importing data to my Django proyect's db.
I have an excel spread sheet (well, hundreds of them) which I need importing
Thanks for the help. Core Data project. I'm importing text from a text file
I am having this big database on one MSSQL server that contains data indexed

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.