Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Sir or Madam,

I am Uday Satardekar.

I am new to C sharp programming.

I am inserting bulk data around 10000 to 15000 rows from excel using xml. I am using c sharp winform for from end and sql server 2005 for back end. It takes times more than 30 sec to 5 min depends upon rows.

Now I have to show progressbar during this time to show application is busy.

Please give solution.

On my save Button click I have write following code.

C#
try
          {

              MyConnect myCnn = new MyConnect();
              String connString = myCnn.getConnect().ToString();    //call from class

              SqlDataAdapter adapter;
              SqlTransaction transaction;
              DataSet ds1 = new DataSet();
              SqlConnection conn;
              conn = new SqlConnection(connString);
              conn.Open();

              transaction = conn.BeginTransaction();
              SqlCommand command = new SqlCommand();
              command.Transaction = transaction;

              string path = txtUploadFlNm.Text;

              string[] readText = File.ReadAllLines(path);

              if (readText[0] == "email")
              {

                  List<string> badEmail = new List<string>();
                  List<string> goodEmail = new List<string>();

                  for (int i = 1; i < readText.Length; i++)
                  {
                      if (validEmail(readText[i]))  // check valid
                      {
                          goodEmail.Add(readText[i]);
                      }
                      else
                      {
                          badEmail.Add(readText[i]);
                      }
                  }


                  StringBuilder XML_TRANSACTIONDETAILS = new StringBuilder();

                  string guid = Guid.NewGuid().ToString().Replace("-", "");
                  string dataID;
                  string today = DateTime.Now.ToLongTimeString();
                  int m = 1;</string></string></string></string>

// AFTER THIS LINE I WANTED TO START MY PROGRESS BAR

C#
                   XML_TRANSACTIONDETAILS.Append("<master>");
                    for (int i = 0; i < goodEmail.Count; i++)
                    {
                        m = m + 1;
                        dataID = guid + "-" + today + m.ToString();

                        XML_TRANSACTIONDETAILS.Append("<transaction>");
                        XML_TRANSACTIONDETAILS.Append("<maxid>" + SecurityElement.Escape(dataID) + "</maxid>");

                        XML_TRANSACTIONDETAILS.Append("<email>" + SecurityElement.Escape(goodEmail[i]) + "</email>");

                        XML_TRANSACTIONDETAILS.Append("</transaction>");
                    }
                    XML_TRANSACTIONDETAILS.Append("</master>");


                    SqlParameter prm = new SqlParameter("@XML_TRANSACTIONDETAILS", SqlDbType.NText);
                    SqlParameter prm1 = new SqlParameter("@status", cmbAllStatus.SelectedValue);
                    SqlParameter prm2 = new SqlParameter("@username", currentLoggedUserId);
                    SqlParameter prm3 = new SqlParameter("@urlorcatalog", tboxUrlCat.Text );
                    SqlParameter prm4 = new SqlParameter("@dataentrytype", cmbDataEntryType.Text);
                    SqlParameter prm5 = new SqlParameter("@category", cmbCategories.SelectedValue);

                    prm.Value = XML_TRANSACTIONDETAILS.ToString();

                    try
                    {
                        command.Connection = conn;
                        command.CommandText = "InsertTextFileUsingXml";
                        command.CommandType = CommandType.StoredProcedure;

                        command.Parameters.Add(prm);
                        command.Parameters.Add(prm1);
                        command.Parameters.Add(prm2);
                        command.Parameters.Add(prm3);
                        command.Parameters.Add(prm4);
                        command.Parameters.Add(prm5);

                        adapter = new SqlDataAdapter(command);
                        adapter.Fill(ds1, "rowsinserted");

                        int rows1 = command.ExecuteNonQuery();
                        transaction.Commit();

                        string duplicateemails = Convert.ToString(ds1.Tables["rowsinserted"].Rows[0][0]);

                        
                       
                            int id = CustomMessageBox.ShowBox("Total Companies:  " + readText.Length.ToString() + "\nDuplicates :" + duplicateemails + "\nBad Emails:  " + badEmail.Count.ToString() + "\nclick view to save/view bad Email", "Expogroup");
                           
                            if (id==2)                      
                            {
                                badEmailInTextFile(badEmail);
                            }
                       

//          <big>  <big></big>AFTER THIS LINE I WANTED TO FINISH OR DISABLE THE PROGRESSBAR</big>


                    }
                    catch (Exception ex1)
                    {
                        transaction.Rollback();
                        MessageBox.Show("Unable to add");
                    }

                    finally
                    {
                        conn.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Please enter email at the top of text file", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception)
            { }


        }


How can I achieve this?
Thanks.
Posted
Updated 7-Sep-11 3:04am
v2
Comments
LittleYellowBird 7-Sep-11 9:06am    
Not everyone on here is a man so it is polite to say 'Sir or Madam' if you wish to begin your question that way. I've added this text for you and tidied your question a little, adding blocks around thwe code.

0) Put the database code into a thread (a BackgroundWorker object would be suitable)

1) Put a ProgressBar on your form.

2) Handle the BackgroundWorker Progress event in your form, and update the progress bar.

3) Google is your friend.
 
Share this answer
 
v2
Comments
udusat13 7-Sep-11 9:09am    
Thanks
But how to put database code in background worker.
Please guied me sir.
I am new to c sharp.
#realJSOP 7-Sep-11 9:24am    
Did you see item #3?
Wendelius 7-Sep-11 9:11am    
Wrote basically same kind of answer simultaneously, my 5.
udusat13 8-Sep-11 4:36am    
Thanks Sir/Madam,

I have edited code like this

{
try
{

string path = txtUploadFlNm.Text;

string[] readText = File.ReadAllLines(path);

if (readText[0] == "email")
{

List<string> badEmail = new List<string>();
List<string> goodEmail = new List<string>();

for (int i = 1; i < readText.Length; i++)
{
if (validEmail(readText[i])) // check valid
{
goodEmail.Add(readText[i]);
}
else
{
badEmail.Add(readText[i]);
}
}

string guid = Guid.NewGuid().ToString().Replace("-", "");
string dataID;
string today = DateTime.Now.ToLongTimeString();
int m = 1;

T_XML_TRANSACTIONDETAILS.Append("<master>");
for (int i = 0; i < goodEmail.Count; i++)
{
m = m + 1;
dataID = guid + "-" + today + m.ToString();

T_XML_TRANSACTIONDETAILS.Append("<TRANSACTION>");
T_XML_TRANSACTIONDETAILS.Append("<maxid>" + SecurityElement.Escape(dataID) + "");

T_XML_TRANSACTIONDETAILS.Append("<email>" + SecurityElement.Escape(goodEmail[i]) + "");

T_XML_TRANSACTIONDETAILS.Append("</TRANSACTION>");
}
T_XML_TRANSACTIONDETAILS.Append("");

progressBar1.Show();
backgroundWorkerText.RunWorkerAsync();

//string duplicateemails = Convert.ToString(ds1.Tables["rowsinserted"].Rows[0][0]);

//DialogResult result;

//if (badEmail.Count == 0)
//{

// MessageBox.Show("Total Companies: " + readText.Length.ToString() + "\nDuplicates :" + duplicateemails + "", "", MessageBoxButtons.OKCancel);

//}
//else
//{

// int id = CustomMessageBox.ShowBox("Total Companies: " + readText.Length.ToString() + "\nDuplicates :" + duplicateemails + "\nBad Emails: " + badEmail.Count.ToString() + "\nclick view to save/view bad Email", "Expogroup");

// if (id == 2)
// {
// badEmailInTextFile(badEmail);
// }
//}

MessageBox.Show("Donnne");
}
else
{
MessageBox.Show("Please enter email at the top of text file", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception)
{ }


}


and

On backgroundWorkerText_DoWork

I have added database code

But First Its working properly .second time not.

Please any one give me solution
Perhaps the simpliest way could be to use a BackgroundWorker[^].

You could place the long running operation in the DoWork and use ProgressChanged event to inform that things are going forward.

However if you're sending the data to the database in one piece, you actually cannot say how long it's going to take so you won't be able to calculate percentage for the progress. In that kind of case you can use Marquee Style[^] for a progress bar.
 
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