0

I am moving rows from a table from one server to another another using a C# script. Please don't tell me to use SSIS components for that. I am looping the incoming dataset using a for loop and I want to see the current iteration, ie for loop index in a GUI box. I can use MessageBox.Show("Text"), but I need to press ok/cancel to allow the code to continue. So, I thought of using a status bar instead. I tried an example I got online

The line this.Controls.Add(mainStatusBar); in the example (below) causes the error -

csproj.ScriptMain' does not contain a definition for 'Controls' and no extension method 'Controls' accepting a first argument of type '.csproj.ScriptMain' could be found (are you missing a using directive or an assembly reference?)

This happens despite adding the reference - System.Windows.Forms.dll and doing a save all (ie Ctrl+Shift+S). The script has a import using System.Windows.Forms; already.

Why am I getting this error and how do I fix it ?

Code -

protected StatusBar mainStatusBar = new StatusBar();
protected StatusBarPanel statusPanel = new StatusBarPanel();
protected StatusBarPanel datetimePanel = new StatusBarPanel();

private void CreateStatusBar()
{
    // Set first panel properties and add to StatusBar
    statusPanel.BorderStyle = StatusBarPanelBorderStyle.Sunken;
    statusPanel.Text = "Application started. No action yet.";
    statusPanel.ToolTipText = "Last Activity";
    statusPanel.AutoSize = StatusBarPanelAutoSize.Spring;
    mainStatusBar.Panels.Add(statusPanel);

    // Set second panel properties and add to StatusBar
    datetimePanel.BorderStyle = StatusBarPanelBorderStyle.Raised;
    datetimePanel.ToolTipText = "DateTime: " + 
    System.DateTime.Today.ToString();
    datetimePanel.Text = System.DateTime.Today.ToLongDateString();
    datetimePanel.AutoSize = StatusBarPanelAutoSize.Contents;
    mainStatusBar.Panels.Add(datetimePanel);

    mainStatusBar.ShowPanels = true;
    // Add StatusBar to Form controls
    this.Controls.Add(mainStatusBar);

}

private void button1_Click(object sender, EventArgs e)
{
    statusPanel.Text = "Button is clicked.";
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    statusPanel.Text = "CheckBox is checked.";
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    statusPanel.Text = "TextBox edited.";
}
Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
Steam
  • 9,368
  • 27
  • 83
  • 122
  • 1
    You do know that you can get more than OK with [MessageBox](http://msdn.microsoft.com/en-us/library/system.windows.forms.messagebox(v=vs.110).aspx), right? – billinkc Mar 10 '14 at 20:10
  • @billinkc - I don't. I don't want to click ok/cancel for 10K iterations. – Steam Mar 10 '14 at 20:19
  • 1
    Not sure if it's possible or not, but the `this` in `this.Controls.Add` is not a Windows Form, but rather the partial class `ScriptMain` (as the error mentions). Its API does not contain a `Controls` property. Ref: http://technet.microsoft.com/en-us/library/microsoft.sqlserver.dts.tasks.scripttask.vstartscriptobjectmodelbase.aspx – mr.Reband Mar 10 '14 at 21:00
  • Is the table in the same database? Will it stay that way? If so you can make things a lot simpler and faster by running `INSERT INTO Table1 (COl1, COl2) SELECT Col3,Col4 FROM Table2` Don't worry it's not SSIS. – Nick.Mc Mar 11 '14 at 02:23
  • @ElectricLlama - The tables are in different servers/instances. – Steam Mar 11 '14 at 03:30
  • Why not just use the Designer to create the statusbar? – John Saunders Mar 29 '14 at 01:53
  • 1
    What do you mean by "C# script"? Is the data copy code part of a larger application (Winforms, WPF, ASP.Net, a console app)? Or is this data copy a "one-off" operation, and your code will be thrown away afterwards? – Andrew Stephens Apr 04 '14 at 10:48
  • wow...long time since my 1 week ban. I don't really remember anything. I guess I'll award a bounty anyway. – Steam Apr 05 '14 at 07:37

3 Answers3

2

The program doesn't know that this is supposed to refer to a form in this statement this.Controls.Add(mainStatusBar);

You would have to do it the way Percentage has suggested.

The proper way to use this is like so :

For Instance

public partial class someForm : Form
    {
        public someForm()
        {
            InitializeComponent();
        }
    }

partial class someForm
    {
        private void InitializeComponent()
            {
                this.mainStatusBar = new StatusBar();
            }
    }

Also, take a look at this article :

When do you use the "this" keyword?

Community
  • 1
  • 1
Adjit
  • 10,134
  • 12
  • 53
  • 98
1

If you are runing script on SQL Server you can't add control to your script because you don't have a window there.

What you can do is to create a standalone GUI aplication that will comunicate with your script (by TCP for example).

Or you can create a file and add new text to it when you perform one iteration.

Use mTail to see what is happening inside your file in realtime.

mTail - http://ophilipp.free.fr/op_tail.htm

Logman
  • 4,031
  • 1
  • 23
  • 35
1

The class that you are using does not have Controls because it is not a Form.

You may create a Form the following way and add a status bar to it:

Replace

this.Controls.Add(mainStatusBar);

With

Form window = new Form();
window.Controls.Add(mainStatusBar);
window.ShowDialog();

The last line will display the window with your status bar in it.

Amandil
  • 473
  • 4
  • 8