Solved

Progress Bar query

Posted on 2006-06-30
19
865 Views
Last Modified: 2008-01-09
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
Comment
Question by:JakeyCakes
  • 10
  • 8
19 Comments
 
LVL 13

Expert Comment

by:devsolns
ID: 17017225
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
 

Author Comment

by:JakeyCakes
ID: 17017468
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
 
LVL 5

Expert Comment

by:t_itanium
ID: 17017677
one way to do this is to use threads and play with the progress bar and increment it will thread is processed..

cheers
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:devsolns
ID: 17017947
why would you use threads, i dont agree with that approach.  will drum up some code now...
0
 
LVL 13

Expert Comment

by:devsolns
ID: 17018078
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
 

Author Comment

by:JakeyCakes
ID: 17018149
devosln,

Should the above code reside in the code behind a form or in a class module and is called by the form?
0
 

Author Comment

by:JakeyCakes
ID: 17018182
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
 
LVL 13

Expert Comment

by:devsolns
ID: 17018585
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
 

Author Comment

by:JakeyCakes
ID: 17041691
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
 
LVL 13

Expert Comment

by:devsolns
ID: 17044856
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
 

Author Comment

by:JakeyCakes
ID: 17051240
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
 

Author Comment

by:JakeyCakes
ID: 17051973
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
 

Author Comment

by:JakeyCakes
ID: 17056764
sorry for calling you 'devonsolns' in my last two posts devsolns - typho errors!
0
 
LVL 13

Expert Comment

by:devsolns
ID: 17060474
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
 

Author Comment

by:JakeyCakes
ID: 17079600
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
 
LVL 13

Expert Comment

by:devsolns
ID: 17080746
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
 

Author Comment

by:JakeyCakes
ID: 17081123
what I mean should the execution of the stored procedures take place in the class?
0
 
LVL 13

Accepted Solution

by:
devsolns earned 500 total points
ID: 17124734
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
 

Author Comment

by:JakeyCakes
ID: 17128738
thanks the points are yours
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question