---
title: "SqlBulkCopy operation hang and not responding"  
description: "SqlBulkCopy operation hang and not responding"  
author: "marcel ethan"  
published: 2013-12-23  
updated: 2013-12-23  
canonical: https://www.mindstick.com/forum/1828/sqlbulkcopy-operation-hang-and-not-responding  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# SqlBulkCopy operation hang and not responding

I am using [SqlBulkCopy](https://www.mindstick.com/interview/33996/sqlbulkcopy-class-in-c-sharp) to copy several [tables](https://www.mindstick.com/articles/336597/introduction-of-html-tables-for-web-development) to [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax). However, most of the tables are successfully copied to database [except one](https://www.mindstick.com/forum/159715/how-to-find-all-strings-except-one-string-using-regex) table with 6000++ [rows of data](https://www.mindstick.com/forum/157166/what-is-the-best-and-fast-way-to-insert-2-million-rows-of-data-into-sql-server). When I run the [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server), it just simply hang there and not responding.

## Below is my code:

```
using (SqlConnection destinationConnection = Login.GetConnection()){    destinationConnection.Open();    using (SqlTransaction transaction = destinationConnection.BeginTransaction(IsolationLevel.ReadCommited))    {        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection, SqlBulkCopyOptions.KeepIdentity, transaction))        {            bulkCopy.DestinationTableName = "dbo." + tableName;            try            {                bulkCopy.WriteToServer(dt);                transaction.Commit();                bulkCopySuccess = true;            }            catch (Exception ex)            {                transaction.Rollback();                MessageBox.Show(ex.Message, ex.GetType().ToString());                bulkCopySuccess = false;            }        }    }}
```

What is the possible [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic)? Is [something wrong](https://www.mindstick.com/forum/12790/something-wrong-with-gridview) with my code?

## Replies

### Reply by Pravesh Singh

Hi Marcel,

\
Problem : if you are running the BulkCopy code from the main thread, it will wait/not respond untill unleess the whole operation gets completed.

\
**so if the user wants to perform some operations on the UI it will be completly unresponsive and hangs.**

\
**Solution :** you could use the BackgroundWorker Component to perform the operations on background without hanging the UI.

\
**Try This:**

\

```
private void button1_Click(object sender, EventArgs e)    {        BackgroundWorker backgroundWorker = new BackgroundWorker();        backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);        backgroundWorker.RunWorkerAsync();    }  private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)    {        DoBulkCopy();    }  private void DoBulkCopy()  {     using (SqlConnection destinationConnection = Login.GetConnection())     {        destinationConnection.Open();        using (SqlTransaction transaction = destinationConnection.BeginTransaction(IsolationLevel.ReadCommited))        {          using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection, SqlBulkCopyOptions.KeepIdentity, transaction))           {             bulkCopy.DestinationTableName = "dbo." + tableName;                                        try             {               bulkCopy.WriteToServer(dt);               transaction.Commit();               bulkCopySuccess = true;             }             catch (Exception ex)             {              transaction.Rollback();              MessageBox.Show(ex.Message, ex.GetType().ToString());              bulkCopySuccess = false;             }            }         }      }}
```


---

Original Source: https://www.mindstick.com/forum/1828/sqlbulkcopy-operation-hang-and-not-responding

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
