1

I am uploading an Excel having 5 sheets. While uploading i am getting the error " TOO MANY FIELDS DEFINED "

public DataSet ExportQCCheckData(string filepath)
{
    string path = filepath;
    OleDbConnection con;
    System.Data.DataTable dt = null;
    //Connection string for oledb
    string conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + path + "; Extended Properties='Excel 8.0;IMEX=1;'";
    con = new OleDbConnection(conn);
    try
    {
        con.Open();
        //get the sheet name in to a table

        dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        String[] excelsheets = new String[dt.Rows.Count];
        int i = 0;
        //using foreach get the sheet name in a string array called excelsheets[]
        foreach (DataRow dr in dt.Rows)
        {
            excelsheets[i] = dr["TABLE_NAME"].ToString();
            i++;
        }
        DataSet ds = new DataSet();
        int cnt = 0;
        foreach (string temp in excelsheets)
        {
            if (temp == "BASIC$" || temp == "SHORT$" || temp == "CLASS$" || temp == "MEMO$" || temp == "SOURCE$")
            {
                string query = "select * from [" + temp + "]";
                OleDbDataAdapter adp = new OleDbDataAdapter(query, con);
                adp.Fill(ds, temp);
                cnt++;
            }
        }
        if (cnt != 5)
        {
            ds = null;
        }
        return ds;
    }
    catch (Exception ex)
    {
        Console.Write(ex.Message);
        DataSet ds = null;
        return ds;
    }
    finally
    {
        con.Close();
    }
}
3
  • 1
    Does the first line in your excel sheet contain header names. If so include HDR=Yes; in you connection string to indicate that the first line is a header row
    – astro boy
    Jul 2, 2012 at 6:45
  • I saw similar type of Question.. but that does not solve my problem Jul 4, 2012 at 12:23
  • yeah, first line indicates header Jul 4, 2012 at 12:26

2 Answers 2

1

The Error belongs to number of fields on excel sheet kindly add excel sheet. you need to verify that excel sheet doesn't contain a lot of columns

0

This problem is due to the excel contains many columns . so what i do it means, just select all the empty cells

Control -

Then i upload the excel sheet, it uploaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.