Friday, 20 January 2012

Binding data to gridview from database Asynchrously in windows application


using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; 
 private bool isLoading = false;
private void btnview_Click(object sender, EventArgs e)
        {

            try
            {
                if (textBox1.Text != string.Empty)
                {

                    Microsoft.Practices.EnterpriseLibrary.Data.Database db = new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(@"Data Source=" + Environment.MachineName + "\\SQLEXPRESS;AttachDbFilename=" + name + ";Integrated Security=True;Asynchronous Processing=true");
                    if (isLoading)
                    {

                    }
                    else
                    {
                        UpdateProgress(true);
                        db.BeginExecuteReader(CommandType.Text,
                            "WAITFOR DELAY '0:0:1';select * from ndc",
                            EndExecuteReaderCallback, db);
                    }

                }

                else
                {

                    MessageBox.Show("Please Select a File");
                }

            }
            catch (Exception ex)
            {
                if (textBox1.Text != string.Empty)
                {
                    isLoading = false;
                    Microsoft.Practices.EnterpriseLibrary.Data.Database db = new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(@"Data Source=" + Environment.MachineName + ";AttachDbFilename=" + name + ";Integrated Security=True;Asynchronous Processing=true");
                    if (isLoading)
                    {

                    }
                    else
                    {
                        UpdateProgress(true);
                        db.BeginExecuteReader(CommandType.Text,
                            "WAITFOR DELAY '0:0:0';select * from ndc",
                            EndExecuteReaderCallback, db);
                    }
                }

                else
                {
                    MessageBox.Show("Please Select a File");

                }

            }
        }


        private void UpdateProgress(bool state)
        {
            isLoading = state;
            panel1.Visible = state;
        }

        private void FillGrid(DataSet ds)
        {

            dataGridViewNDI.DataSource = ds.Tables[0];
            UpdateProgress(false);         

        }

        private void EndExecuteReaderCallback(IAsyncResult result)
        {
            try
            {
                var database = (Database)result.AsyncState;
           
                using (var reader = database.EndExecuteReader(result))
                {
                    DtSet.Load(reader, LoadOption.PreserveChanges, "data");
                }

                Invoke(new Action<DataSet>(FillGrid), DtSet);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

No comments:

Post a Comment