• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 889
  • Last Modified:

Progress Bar query

Hi,

I would like have a progress bar that steps every time a stored procedure is completed and changes the text of a label when certain groups of stored procedures are executed in other words, when the group of stored procedures that convert data  are running the label is to display "Converting data...", when the group of stored procedures that validate data are running the label is to display "Validating data...", and when the group of stored procedures that inport data into a specific database are running the label is to display "Importing Data..."

Many Thanks
0
JakeyCakes
Asked:
JakeyCakes
  • 10
  • 8
1 Solution
 
devsolnsCommented:
Your class that contains the code that executes the sprocs could publish events.  That way anyone including that label/progress bar can subscribe to them and recieve notifcation when something takes place.  This is a good design approach because the sproc code doesnt need to know that the GUI even exists.
0
 
JakeyCakesAuthor Commented:
devsolns,

could you give me an example of how to publish events and how the the label and progress bar as I am new(ish) to C#.

Thanks
0
 
t_itaniumCommented:
one way to do this is to use threads and play with the progress bar and increment it will thread is processed..

cheers
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
devsolnsCommented:
why would you use threads, i dont agree with that approach.  will drum up some code now...
0
 
devsolnsCommented:
namespace test
{
    public class SprocNotifyEventArgs : EventArgs
    {
        public readonly string Message;
        public SprocNotifyEventArgs(string message)
        {
            Message = message;
        }
    }

    public class SprocGateWay
    {
        public delegate void SprocNotification(SprocNotifyEventArgs eventArgs);

        public event SprocNotification OnDataConvert = null;

        public event SprocNotification OnDataValidate = null;

        protected void OnDataConvertInvoke(string message)
        {
            SprocNotification ev = OnDataConvert;
            if (ev != null) ev(new SprocNotifyEventArgs(message));
        }

        protected void OnDataValidateInvoke(string message)
        {
            SprocNotification ev = OnDataValidate;
            if (ev != null) ev(new SprocNotifyEventArgs(message));
        }

        public void ExcecuteConvertingProcs()
        {
            //before doing so kick off event
            OnDataConvertInvoke("Converting data...");

            //do work
            System.Threading.Thread.Sleep(1000);

            OnDataConvertInvoke("Done converting...");
        }

        public void ExecuteValidationProcs()
        {
            //before doing work kick off event
            OnDataValidateInvoke("Validating data...");

            //do work
            System.Threading.Thread.Sleep(1000);

            OnDataValidateInvoke("Done Validating...");
        }
    }



    public class MyGUI
    {
        private SprocGateWay _sprocGateWay = new SprocGateWay();

        public MyGUI()
        {
            _sprocGateWay.OnDataConvert += new SprocGateWay.SprocNotification(_sprocGateWay_OnDataConvert);
            _sprocGateWay.OnDataValidate += new SprocGateWay.SprocNotification(_sprocGateWay_OnDataValidate);
        }

        void _sprocGateWay_OnDataValidate(SprocNotifyEventArgs eventArgs)
        {
            //update label, or whatever here.
            Console.WriteLine(eventArgs.Message);
        }

        void _sprocGateWay_OnDataConvert(SprocNotifyEventArgs eventArgs)
        {
            //update label, or whatever here.
            Console.WriteLine(eventArgs.Message);
        }

        public void UpdateData()
        {
            _sprocGateWay.ExcecuteConvertingProcs();
            _sprocGateWay.ExecuteValidationProcs();
        }
    }
}
0
 
JakeyCakesAuthor Commented:
devosln,

Should the above code reside in the code behind a form or in a class module and is called by the form?
0
 
JakeyCakesAuthor Commented:
Also, I can see how a label can use the above code but am unsure how a progress bar could step with the above code
0
 
devsolnsCommented:
The only part of the sample code above that would go into the gui is things like in the MyGUI class.  The SprocGateway should NOT.  It sorta acts as the Model in a Model View Controller design pattern.

Yes can you certainly get it to work with a progress bar.

        void _sprocGateWay_OnDataValidate(SprocNotifyEventArgs eventArgs)
        {
                       //incrementer value = 100 / total expected calls.
                       progressBar.Value += incrementerValue;
        }

        void _sprocGateWay_OnDataConvert(SprocNotifyEventArgs eventArgs)
        {
                      //incrementer value = 100 / total expected calls.
                      progressBar.Value += incrementerValue;
        }

 
       ORRRR you can use a Marquee style progress bar because its likely you cant predict how long your procs will
       take.  You can add an enum param to the SprocNotifyEventArgs in the code above that tells the subsriber
       whether the proc is started or ending.


        void _sprocGateWay_OnDataValidate(SprocNotifyEventArgs eventArgs)
        {
                      if(eventArgs.Status == SprocNotifyStatus.Start)
                              //start marquee on prog bar.
                      else if(eventArgs.Status == SprocNotifyStatus.End)
                             //stop marquee on prog bar.
        }

        void _sprocGateWay_OnDataConvert(SprocNotifyEventArgs eventArgs)
        {
                      if(eventArgs.Status == SprocNotifyStatus.Start)
                              //start marquee on prog bar.
                      else if(eventArgs.Status == SprocNotifyStatus.End)
                             //stop marquee on prog bar.
        }
0
 
JakeyCakesAuthor Commented:
devsolns,

I keep getting build errors, and I cannot see where they are occuring - would you be so kind as to look at my code below and inform me what I have done and what I need to do to correct it?

The code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace testedexe
{
      /// <summary>
      /// Summary description for frmMigration.
      /// </summary>
      public class frmMigration : System.Windows.Forms.Form
      {
            private System.Windows.Forms.ProgressBar progMigration;
            private System.Windows.Forms.Button btnStart;
            private System.Windows.Forms.Button btnCancel;
            private System.Windows.Forms.Button btnExit;
            public System.Data.SqlClient.SqlConnection IRIS;
            private System.Data.SqlClient.SqlConnection Validation;
            private System.Data.SqlClient.SqlConnection TaxyWin;
            private System.Data.SqlClient.SqlCommand FirstConversionStoredProcedure;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public frmMigration()

            {
                  _sprocGateWay.OnDataConvert += new SprocGateWay.SprocNotification(_sprocGateWay_OnDataConvert);
                  _sprocGateWay.OnDataValidate += new SprocGateWay.SprocNotification(_sprocGateWay_OnDataValidate);
            

                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }
            
      

                  void _sprocGateWay_OnDataValidate(SprocNotifyEventArgs eventArgs)
                  {
                        if(eventArgs.Status == SprocNotifyStatus.Start)
                        {
                              //start marquee on prog bar.
                              
                        }
                        else if(eventArgs.Status == SprocNotifyStatus.End)
                        {
                              //stop marquee on prog bar.
                        }
                  }

            void _sprocGateWay_OnDataConvert(SprocNotifyEventArgs eventArgs)
            {
                  if(eventArgs.Status == SprocNotifyStatus.Start)
                  {
                        //start marquee on prog bar.
                        this.IRIS.Open();
                        this.FirstConversionStoredProcedure.CommandText("ClientDetails");
        this.FirstConversionStoredProcedure.ExecuteNonQuery();
                        this.IRIS.Close();
                        
                  }
                  else if(eventArgs.Status == SprocNotifyStatus.End)
                  {
                        //stop marquee on prog bar.
                  }
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if(components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.progMigration = new System.Windows.Forms.ProgressBar();
                  this.btnStart = new System.Windows.Forms.Button();
                  this.btnCancel = new System.Windows.Forms.Button();
                  this.btnExit = new System.Windows.Forms.Button();
                  this.IRIS = new System.Data.SqlClient.SqlConnection();
                  this.Validation = new System.Data.SqlClient.SqlConnection();
                  this.TaxyWin = new System.Data.SqlClient.SqlConnection();
                  this.FirstConversionStoredProcedure = new System.Data.SqlClient.SqlCommand();
                  this.SuspendLayout();
                  //
                  // progMigration
                  //
                  this.progMigration.Location = new System.Drawing.Point(16, 56);
                  this.progMigration.Name = "progMigration";
                  this.progMigration.Size = new System.Drawing.Size(256, 16);
                  this.progMigration.TabIndex = 0;
                  //
                  // btnStart
                  //
                  this.btnStart.Location = new System.Drawing.Point(16, 72);
                  this.btnStart.Name = "btnStart";
                  this.btnStart.Size = new System.Drawing.Size(88, 24);
                  this.btnStart.TabIndex = 1;
                  this.btnStart.Text = "Start";
                  this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
                  //
                  // btnCancel
                  //
                  this.btnCancel.Location = new System.Drawing.Point(104, 72);
                  this.btnCancel.Name = "btnCancel";
                  this.btnCancel.Size = new System.Drawing.Size(88, 24);
                  this.btnCancel.TabIndex = 2;
                  this.btnCancel.Text = "Cancel";
                  //
                  // btnExit
                  //
                  this.btnExit.Location = new System.Drawing.Point(192, 72);
                  this.btnExit.Name = "btnExit";
                  this.btnExit.Size = new System.Drawing.Size(80, 24);
                  this.btnExit.TabIndex = 3;
                  this.btnExit.Text = "Exit";
                  //
                  // IRIS
                  //
                  this.IRIS.ConnectionString = "workstation id=\"JAKEC-TEST\";packet size=4096;integrated security=SSPI;data source" +
                        "=\"JAKEC-TEST\";persist security info=False;initial catalog=IRIS_Conversion";
                  
                  //
                  // Validation
                  //
                  this.Validation.ConnectionString = "workstation id=\"JAKEC-TEST\";packet size=4096;integrated security=SSPI;data source" +
                        "=\"JAKEC-TEST\";persist security info=False;initial catalog=TaxabilityProValidatio" +
                        "n";
                  //
                  // TaxyWin
                  //
                  this.TaxyWin.ConnectionString = "workstation id=\"JAKEC-TEST\";packet size=4096;integrated security=SSPI;data source" +
                        "=\"JAKEC-TEST\";persist security info=False;initial catalog=TaxyWin";
                  //
                  // frmMigration
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 126);
                  this.Controls.Add(this.btnExit);
                  this.Controls.Add(this.btnCancel);
                  this.Controls.Add(this.btnStart);
                  this.Controls.Add(this.progMigration);
                  this.Name = "frmMigration";
                  this.Text = "Data Migration";
                  this.Load += new System.EventHandler(this.frmMigration_Load);
                  this.ResumeLayout(false);

            }
            #endregion

            private void btnStart_Click(object sender, System.EventArgs e)
            {
                  this._sprocGateWay_OnDataConvert();

            }

            

      



      
      }
}
 
0
 
devsolnsCommented:
One error I see right away
    public class frmMigration : System.Windows.Forms.Form
     {
          public frmMigration()

          {
               _sprocGateWay.OnDataConvert += new SprocGateWay.SprocNotification(_sprocGateWay_OnDataConvert);
               _sprocGateWay.OnDataValidate += new SprocGateWay.SprocNotification(_sprocGateWay_OnDataValidate);
          }
          ...
     }

_sprocGateWay isnt declared.

So fix like this


    public class frmMigration : System.Windows.Forms.Form
     {
          private _sprocGateWay = new SprocGateWay();
          public frmMigration()

          {
               _sprocGateWay.OnDataConvert += new SprocGateWay.SprocNotification(_sprocGateWay_OnDataConvert);
               _sprocGateWay.OnDataValidate += new SprocGateWay.SprocNotification(_sprocGateWay_OnDataValidate);
          }
          ...
     }
0
 
JakeyCakesAuthor Commented:
Devonsolns,

I implemented your above suggestion and got the following errors:
 Invalid token '=' in class, struct, or interface member declaration
 Failed to parse method 'InitializeComponent'. The parser reported the following error 'Object reference not set to an instance of an object.'. Please look in the Task List for  potential errors.
 Class, struct, or interface method must have a return type
 Class, struct, or interface method must have a return type
 A namespace does not directly contain members such as fields or methods

what do they mean and what do I need to do to ensure that the above errors do not occur again (note: I use visual studio.net 2003)

0
 
JakeyCakesAuthor Commented:
devonsolns,

Ignore above I have sorted those, however Now get the error:

'SprocNotifyEventArgs' does not contain a definition for 'Status'

how can I resolve this error?
0
 
JakeyCakesAuthor Commented:
sorry for calling you 'devonsolns' in my last two posts devsolns - typho errors!
0
 
devsolnsCommented:
Right I just put "status" there to give you some idea.  Whatever information you want to send in the event just add that to the

    public class SprocNotifyEventArgs : EventArgs
    {
        public readonly string Message;
        public SprocNotifyEventArgs(string message)
        {
            Message = message;
        }
    }


So if you wanted status it would be like so,

    public class SprocNotifyEventArgs : EventArgs
    {
        public enum Status{Start = 0, End};
        public readonly string Message;
        public readonly Status EventStatus;
        public SprocNotifyEventArgs(string message, Status status)
        {
            Message = message;
            EventStatus = status;
        }
    }


And you'll now need to alter the code in the event that "triggers" the event.


      protected void OnDataConvertInvoke(string message, SprocNotifyEventArgs.Status status)
        {
            SprocNotification ev = OnDataConvert;
            if (ev != null) ev(new SprocNotifyEventArgs(message, status));
        }

        protected void OnDataValidateInvoke(string message, SprocNotifyEventArgs.Status status)
        {
            SprocNotification ev = OnDataValidate;
            if (ev != null) ev(new SprocNotifyEventArgs(message, status));
        }


Good luck,
gp


0
 
JakeyCakesAuthor Commented:
devsolns,

Thank you for your patience (told you I'm new at this! :-)). Two things: with

                  _sprocGateWay.OnDataConvert += new SprocGateWay.SprocNotification(_sprocGateWay_OnDataConvert);
                  _sprocGateWay.OnDataValidate += new SprocGateWay.SprocNotification(_sprocGateWay_OnDataValidate);
      _sprocGateWay.OnDataExport += new SprocGateWay.SprocNotification (_sprocGateWay_OnDataExport);

I now get the error 'Method 'testedexe.frmMigration._sprocGateWay_OnDataConvert(System.EventArgs)' does not match delegate 'void testedexe.SprocGateWay.SprocNotification(testedexe.SprocNotifyEventArgs)' - what does this mean?

secondly should the attachment to the stored procedure be in the voids in the form or the classes

e.g in the

void _sprocGateWay_OnDataConvert(EventArgs eventArgs)
            {
                  if(SprocNotifyEventArgs.Status.Start == SprocNotifyEventArgs.Status.Start)
                  {
                        //start marquee on prog bar.
                        this.lblInformation.Text = "Converting...";
                        this.IRIS.Open();
                        this.FirstConversionStoredProcedure.CommandText = "dbo.[DataConversion]";
                        this.FirstConversionStoredProcedure.CommandType = System.Data.CommandType.StoredProcedure;
                        this.FirstConversionStoredProcedure.Connection = this.IRIS;
                        //this.FirstConversionStoredProcedure.Parameters.Add(new System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, false, ((System.Byte)(0)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null));
                        this.btnExit.Enabled = false;
                        this.btnStart.Enabled = false;
                        this.IRIS.Close();
                        
                  }
                  if(SprocNotifyEventArgs.Status.End == SprocNotifyEventArgs.Status.End)
                  {
                        //stop marquee on prog bar.
                        this.lblInformation.Text = "Done Conversion...";
                        
                  }
            }

or in the executeconvertprocs() in the class?
0
 
devsolnsCommented:
They do not match because a delegate is a type safe pointer to a method.  Type safe meaning the parameters must match.  So as you'll see above I changed the delegate to now take a parameter of SprocNotifyEventArgs.Status.  Your method doesn't match anymore.


So in the above, change void _sprocGateWay_OnDataConvert(EventArgs eventArgs) to

void _sprocGateWay_OnDataConvert(EventArgs eventArgs, SprocNotifyEventArgs.Status)
{
   ...
}

Also do the same for the other.  Does that make sense?
0
 
JakeyCakesAuthor Commented:
what I mean should the execution of the stored procedures take place in the class?
0
 
devsolnsCommented:
yes the execution can take place in SprocGateWay class.


        public void ExecuteValidationProcs()
        {
            //before doing work kick off event
            OnDataValidateInvoke("Validating data...");

            //do work and call stored proc right here.

            OnDataValidateInvoke("Done Validating...");
        }
0
 
JakeyCakesAuthor Commented:
thanks the points are yours
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now