-3

i am working on windows form application...i have a crystal report...for showing crystal report in button click event i given code like this:

SqlCommand cmdrslt = new SqlCommand("rptdeptwisevisitor", con.connect);
cmdrslt.CommandType = CommandType.StoredProcedure;
cmdrslt.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = frmdateval;
cmdrslt.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = Todateval;
SqlParameter tvp1 = cmdrslt.Parameters.AddWithValue("@Dept", DepTable);
tvp1.SqlDbType = SqlDbType.Structured;
tvp1.TypeName = "dbo.Dept";
da.SelectCommand = cmdrslt;
da.Fill(ds);
DeptWiseRpt rpt = new DeptWiseRpt();
if ((ds.Tables(0).Rows.Count > 0)) {
    rpt.SetDataSource(ds.Tables(0));
    rpt.SetParameterValue("frmd", setparmstartd);
    rpt.SetParameterValue("tod", setparmendd);
    CrystalReportViewer1.ReportSource = rpt;

}

this is taking time to load the data..so i want to put a progress bar on this form..how could I put it?
any help is very appreciable...

Olrac
  • 1,527
  • 2
  • 10
  • 22
user3262364
  • 369
  • 3
  • 9
  • 23
  • You can use a backgroundworker for this. See [this](http://stackoverflow.com/questions/1470927/c-sharp-winform-progressbar-and-backgroundworker) question for further information. –  May 12 '14 at 08:10
  • but i want to show progress bar..while loading the records.. – user3262364 May 12 '14 at 08:14
  • Put all your code that takes a long time into the BackgroundWorker.DoWork method (you can also use Tasks). –  May 12 '14 at 08:19
  • sir could you pls show one example? – user3262364 May 12 '14 at 08:25
  • Using the search "c# backgroundworker progressbar site:stackoverflow.com" provides you with these results: [1](http://stackoverflow.com/questions/13874122/running-a-method-in-backgroundworker-and-showing-progressbar) [2](http://stackoverflow.com/questions/18465318/how-to-update-the-progress-bar-through-backgroundworker-in-c) [3](http://stackoverflow.com/questions/7974213/c-updating-a-progress-bar-using-background-worker-from-a-different-class) –  May 12 '14 at 08:29

1 Answers1

0

Loading report is a single operation (two at most: query and displaying the viewer), so that you can't split it do display progress accurately. You could display progressless bar or use animated image like this one:

enter image description here

That operation has to run in parallel to UI thread (use Thread, Task or BackgroundWorker), otherwise your progress (progressbar or image) will not get updated even once. For the time of loading you should not display report viewer itself (make it invisible or size 1x1). Once loading is completed: hide progress and display viewer.

Some code:

// hide viewer, show progress
CrystalReportViewer1.Visible = false;
pictureBoxProgress.Visible = true;
// start thread
(new Thread(() => {

    // your code
    SqlCommand cmdrslt = new SqlCommand("rptdeptwisevisitor", con.connect);
    cmdrslt.CommandType = CommandType.StoredProcedure;
    cmdrslt.Parameters.Add("@startDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = frmdateval;
    cmdrslt.Parameters.Add("@endDate", SqlDbType.NVarChar, 50, ParameterDirection.Input).Value = Todateval;
    SqlParameter tvp1 = cmdrslt.Parameters.AddWithValue("@Dept", DepTable);
    tvp1.SqlDbType = SqlDbType.Structured;
    tvp1.TypeName = "dbo.Dept";
    da.SelectCommand = cmdrslt;
    da.Fill(ds);
    DeptWiseRpt rpt = new DeptWiseRpt();
    if ((ds.Tables(0).Rows.Count > 0)) {
        rpt.SetDataSource(ds.Tables(0));
        rpt.SetParameterValue("frmd", setparmstartd);
        rpt.SetParameterValue("tod", setparmendd);
        // controls operations require invoke
        BeginInvoke(() => {
            CrystalReportViewer1.ReportSource = rpt;
            pictureBoxProgress.Visible = false;
            CrystalReportViewer1.Visible = true;
        });
    }
})).Start();
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • how i can give your animated image and what all are the changes i have to make in code? – user3262364 May 12 '14 at 09:21
  • Is not mine image. Make own, buy or find a free one. Use `PictureBox` to display it in winforms. See edit for code. – Sinatr May 12 '14 at 09:34
  • I have explained a necessary of using `Thread` already: you do not block UI thread while preparing data (query, setting up `DeptWiseRpt`). I am not sure how do deal if setting `ReportSource` itself is a bottleneck, perhaps crystal report has some options to *preload* (pre-render data). Here I can't help you. And about animated picture, search for "gif", there are plenty of gif-editors or converters (from avi to example). Or just find one in [internet](https://www.google.de/search?q=progress&tbas=0&tbm=isch&tbs=isz:i,itp:animated). – Sinatr May 12 '14 at 10:01
  • if i download jpg image..to which format i have to convert..to get as like your image(animated) – user3262364 May 12 '14 at 10:17
  • [Gif](http://en.wikipedia.org/wiki/Graphics_Interchange_Format). And you need multiple jpg's to make single gif. Perhaps you should try to download gif as a gif? – Sinatr May 12 '14 at 10:27
  • but i havent see any gif image like you given above – user3262364 May 12 '14 at 10:48