0

Ok, I start off by importing an excel file and then save the data into Data table and then putting it into Gridview. I wanted to copy the column header name into a row cell and it was giving error that Input string was not is a correct format. I tried changing the column data type and it works. But the data copied is different to the original. Here is the code I am using:

DataTable dtf1 = new DataTable();
 OleDbDataAdapter adapter = new OleDbDataAdapter(sql, con);
adapter.FillSchema(dtf1, SchemaType.Source);
            dtf1.Columns[6].DataType = typeof(string);
            adapter.Fill(dtf1);

 dtf1.Rows[0][6] = dtf1.Columns[6].ColumnName.ToString();

The original data is a postcode number "3210" but it is copying it as something "F7".

A.J
  • 141
  • 5
  • 17

1 Answers1

1

I found the solution here OleDB & mixed Excel datatypes : missing data. The problem was when excel was importing data it was ignoring numbers. Changing the connection string solved the problem.

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ServerPath + ";Extended Properties='Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text'");

Previously it was

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ServerPath + ";Extended Properties='Excel 8.0;IMEX=1'");
Community
  • 1
  • 1
A.J
  • 141
  • 5
  • 17