hey guys pls I need your help, the code below works at about 95%, the other 5% is the problem. when I run the program,the form allows me to select the file I want to read and then when I click the populate button(button2_Click_1), it goes down hill from there. The cancel button does not respond, the label is not visible and doesn't update and the same goes for the progress bar. I have used Application.DoEvent, but I still can't seem to get it to work.
namespace DellOrderUP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String Filename = "";
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\ ";
openFileDialog1.Filter = "txt files (*.xls)|*.xls|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
Filename = openFileDialog1.FileName;
String Extension = openFileDialog1.FileName;
String Check = Path.GetExtension(Extension);
if (Check == ".xls" || Check == ".xlsx" || Check == ".xlsm")
{
button2.Enabled = true;
}
else
{
String Error = "Sorry Only Excel Files!!!!";
String caption = "Excel Only";
MessageBox.Show(Error, caption);
button2.Enabled = false;
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{}
private void button2_Click_1(object sender, EventArgs e)
{
OpenExcelFile(Filename);
label1.Visible = true;
progressBar1.Visible = true;
}
private void button3_Click_1(object sender, EventArgs e)
{
string message = "Do you want to close this program ?";
string caption = "InventoryUploader";
MessageBoxButtons button = MessageBoxButtons.YesNoCancel;
DialogResult result;
result = MessageBox.Show(this, message, caption, button);
if (result == DialogResult.Yes)
{
this.Close();
}
}
private void label1_Click(object sender, EventArgs e)
{}
private void progressBar1_Click(object sender, EventArgs e)
{}
private void OpenExcelFile(String filepath)
{
button3.Enabled = true;
button2.Enabled = false;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filepath);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange.Columns[37];
UpdateLabel("Please Wait, reading data");
System.Array myvalues = (System.Array)xlRange.Cells.Value;
string[] strArray = myvalues.OfType<object>().Select(o => o.ToString()).ToArray();
int Size_Of_strArray = strArray.Length;
String[] FirstClean = new String[Size_Of_strArray];
String[] SecondClean = new String[Size_Of_strArray];
UpdateLabel("Sanitizing data");
for (int i = 1; i < Size_Of_strArray - 1; i++)
{
FirstClean[i] = strArray[i].Remove(0, 24);
SecondClean[i] = FirstClean[i].Replace("'", "");
}
UpdateProgress(10);
BeforeInsertingIntoDB(ref SecondClean);
}
private void UpdateProgress( int i)
{
progressBar1.Value = i;
this.progressBar1.Update();
Application.DoEvents();
}
private void UpdateLabel( String status)
{
label1.Text = status
this.label1.Update();
Application.DoEvents();
}
private void BeforeInsertingIntoDB(ref string[] data)
{
UpdateLabel("Preparing data for insertion");
String PoNum = ""; String OrderNum = "";
int Size_Of_Array = data.Length;
for (int i = 1; i < Size_Of_Array - 1; i++)
{
String Distribution = data[i];
counter = i;
String[] DistributionArray = Distribution.Split(',');
PoNum = DistributionArray[0]; OrderNum = DistributionArray[1];
InsertDataIntoDB(PoNum, OrderNum)
}
UpdateProgress(40);
}
private void InsertDataIntoDB(String PoNum, String OrderNum)
{
UpdateLabel("Opening Db connection");
UpdateProgress(20);
String InventoryUploader = ConfigurationManager.ConnectionStrings ["DellOrderUP.Properties.Settings.InventoryUploader"].ConnectionString;
SqlConnection con = new SqlConnection(InventoryUploader);
try
{
con.Open();
label1.Text = "Populating Database";
this.label1.Update();
UpdateProgress(30);
SqlCommand cmd = new SqlCommand("UploadDellOrder", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@ProdNum", SqlDbType.Int));
cmd.Parameters["@ProdNum"].Value = PoNum;
cmd.Parameters.Add(new SqlParameter("@OrderNum", SqlDbType.Int));
cmd.Parameters["@OrderNum"].Value = OrderNum;
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
String caption_error_num = Convert.ToString(counter);
String error = e.ToString();
MessageBox.Show(error,caption_error_num);
}
finally
{
con.Close();
}
}
Thanks for your help.
[edit] Space added in the line openFileDialog1.InitialDirectory = "c:\\";
to avoid scaped quotes and the mess in the formatting colous[/edit]