Click here to Skip to main content
15,915,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My code as follows

i have one button called uploadFarmerData and select the file and upload.

for that code as follows

if (e.CommandName == "UploadFarmerData")
{
try
{

string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
Econ = new OleDbConnection(constr);
Econ.Open();
DataTable dtExcelSchema;
dtExcelSchema = Econ.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
DataSet ds = new DataSet();
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
string sql = "SELECT * From [" + SheetName + "]";
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbCommand cmdExcel = new OleDbCommand(sql, Econ);
 da.SelectCommand = cmdExcel;
                        da.Fill(ds);
                        Econ.Close();
                        DataTable dt = new DataTable();
                        dt = ds.Tables[0];
                        bool s = false;
                        if (dt.Rows.Count > 0)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                               string firstname = dt.Rows[i]["farmername"].ToString();
                                string lastname = "";
                                string surname = "";
 string[] nameSplit = firstname.Split(new string[] { " " }, StringSplitOptions.None);
                                if (nameSplit.Length == 2)
                                {
                                    firstname = nameSplit[0].ToString().Trim();
                                    lastname = nameSplit[1].ToString().Trim();
                                }
                            }
                        }

                          s = true;
                          if (s)
                            {
                 showStatusTrue.InnerHtml = "Farmer Details Uploaded Successfully";
                            }

                           else
                            {
                 showStatusFalse.InnerHtml = "Farmer Details Uploaded Failed";
                            }
               catch (Exception ex)
                    {
                         showStatusFalse.InnerHtml = "Farmer Details Uploaded Failed";
                    }
              }
          }


 i have one button called uploadFarmerData and select the file and upload, when i upload shows error as follows

            External table is not in the expected format

  please let me know what is the mistake in my above code.

What I have tried:

My code as follows

  if (e.CommandName == "UploadFarmerData")
                {
             try
                 {

        string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName); 
       Econ = new OleDbConnection(constr);
       Econ.Open();
      DataTable dtExcelSchema;
      dtExcelSchema = Econ.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
      string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
      DataSet ds = new DataSet();
      string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
      string sql = "SELECT * From [" + SheetName + "]";
      OleDbDataAdapter da = new OleDbDataAdapter();
      OleDbCommand cmdExcel = new OleDbCommand(sql, Econ);
      <pre> da.SelectCommand = cmdExcel;
                        da.Fill(ds);
                        Econ.Close();
                        DataTable dt = new DataTable();
                        dt = ds.Tables[0];
                        bool s = false;
                        if (dt.Rows.Count > 0)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                               string firstname = dt.Rows[i]["farmername"].ToString();
                                string lastname = "";
                                string surname = "";
 string[] nameSplit = firstname.Split(new string[] { " " }, StringSplitOptions.None);
                                if (nameSplit.Length == 2)
                                {
                                    firstname = nameSplit[0].ToString().Trim();
                                    lastname = nameSplit[1].ToString().Trim();
                                }
                            }
                        }

                          s = true;
                          if (s)
                            {
                 showStatusTrue.InnerHtml = "Farmer Details Uploaded Successfully";
                            }

                           else
                            {
                 showStatusFalse.InnerHtml = "Farmer Details Uploaded Failed";
                            }
               catch (Exception ex)
                    {
                         showStatusFalse.InnerHtml = "Farmer Details Uploaded Failed";
                    }
              }
          }


 i have one button called uploadFarmerData and select the file and upload, when i upload shows error as follows

            External table is not in the expected format

  please let me know what is the mistake in my above code.
Posted
Comments
Richard MacCutchan 27-Jun-18 4:02am    
The problem appears to be in your source data not your code. You need to inspect your file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900