We help IT Professionals succeed at work.
Get Started

sqlbulkinsert error

randycdouglas
on
354 Views
Last Modified: 2012-05-12
VS2010, SQL Server 2008
Short version: I'm uploading data from an excel spreadsheet, making some string manipulations, and then using sqlbulkinsert to insert it. Besides the data I'm pulling form the spreadsheet, I'm manually adding an ID I get from elsewhere. I'm adding this in the Excel query like this "string excelQuery = SELECT *, " + hdnRigID.Value + " FROM [Sheet1$" + txtFromCell.Text.Trim() + ":" + txtToCell.Text.Trim() + "]";

The field in the database is an int. Upon writing to the server I get the following error: "The given value of type String from the data source cannot be converted to type int of the specified target column."

Is there anyway I can make these datatypes match???  
protected void ButtonUploadFile_Click(object sender, System.EventArgs e)
{                      
//...  
string excelQuery = "SELECT *, " + hdnRigID.Value + " FROM [Sheet1$" + txtFromCell.Text.Trim() + ":" + txtToCell.Text.Trim() + "]";

//...
if (File.Exists(uploadFile.SavePath))
{
BulkSqlShipperImporter importer = new BulkSqlShipperImporter();
connectionMessage = uploadFile.GetOleDbConnectionString();
importer.SourceConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:(...)\\bf6a119e2fcc4a58aa8226cfa1ec0a08_DCL TOI POB.xls;Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\";

importer.Import(excelQuery, hdnCustomerID.Value);
}
}
catch 
{}
}
}

importer.Import class:

public override void Import(string query, string customer)
    {
        string destinationConnectionString = ...
        using (OleDbConnection myConnection = new OleDbConnection(base.SourceConnectionString))
        using (SqlConnection destinationConnection = new SqlConnection(destinationConnectionString))
        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
        {
            bulkCopy.ColumnMappings.Add(0, "Name");
            bulkCopy.ColumnMappings.Add(1, "RigID");
            
            bulkCopy.DestinationTableName = "dbo.LookUp";

            using (var myCommand = new OleDbCommand(query, myConnection))
            {
                myConnection.Open();
                destinationConnection.Open();
                
                DataTable dt = new DataTable();
                OleDbDataAdapter da = new OleDbDataAdapter(myCommand);

                da.Fill(dt);                
                try
                {
                    bulkCopy.WriteToServer(dt);
                }
                catch
                {}
                finally
                {
                    bulkCopy.Close();
                    myConnection.Close();
                    destinationConnection.Close();
                }
            }
        }    
    }
}

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE