Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
importing excel data into sql server table

Code as follows

C#
protected void btnexport_Click(object sender, EventArgs e)
        {
            if (!FileUpload1.HasFile)
            {
      string CurrentFilePath = string.Concat(Server.MapPath("~/masterdata/Test1.xlsx" + FileUpload1.FileName));
                 FileUpload1.SaveAs(CurrentFilePath);

     string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", CurrentFilePath);

                    OleDbConnection connection = new OleDbConnection();
                    connection.ConnectionString = constr;
     OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
                    connection.Open();
       System.Data.Common.DbDataReader dr = command.ExecuteReader();
     string sqlConnectionString = @"Data Source = DESKTOP; Initial Catalog = Sample; User ID = sa; Password = 12345";
                     
                    SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString);
                    bulkInsert.DestinationTableName = "tbl_farmerregistration_ivorycoast";
                    bulkInsert.WriteToServer(dr);


In run mode i click th export button and debug and shows error as follows

The given value of type String from the data source cannot be converted to type int of the specified target column.

how to solve this error.

What I have tried:

importing excel data into sql server table

Code as follows

C#
protected void btnexport_Click(object sender, EventArgs e)
        {
            if (!FileUpload1.HasFile)
            {
      string CurrentFilePath = string.Concat(Server.MapPath("~/masterdata/Test1.xlsx" + FileUpload1.FileName));
                 FileUpload1.SaveAs(CurrentFilePath);

     string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", CurrentFilePath);

                    OleDbConnection connection = new OleDbConnection();
                    connection.ConnectionString = constr;
     OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
                    connection.Open();
       System.Data.Common.DbDataReader dr = command.ExecuteReader();
     string sqlConnectionString = @"Data Source = DESKTOP; Initial Catalog = Sample; User ID = sa; Password = 12345";
                     
                    SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString);
                    bulkInsert.DestinationTableName = "tbl_farmerregistration_ivorycoast";
                    bulkInsert.WriteToServer(dr);


In run mode i click th export button and debug and shows error as follows

The given value of type String from the data source cannot be converted to type int of the specified target column.

how to solve this error.
Posted
Updated 15-Aug-18 6:14am
v2

Look at the error message:
Quote:
The given value of type String from the data source cannot be converted to type int of the specified target column.
It's pretty explicit: you have a column which contains non-numeric data that you are trying to display in an integer column.

We can't do anything about that - we have no access to your excel data, or to your database ... so look at your data, and find out what it should be, what it is, and why. Then you can think about fixing it.

But we can't do any of that for you!
 
Share this answer
 
Hi,

It seems while inserting your Excel data into database, the type convertion is missing, please recheck your Excel data type with SQL datatype.
 
Share this answer
 

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