Results 1 to 4 of 4

Thread: [RESOLVED] Detect if Filling DataSet data is finish

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Resolved [RESOLVED] Detect if Filling DataSet data is finish

    Is there a way to know.detect if filling of Dataset data is finish?
    All I need is to do is to alert a user that the dataset is ready and have data on it.
    Here is my codes for filling my dataset:

    C# Code:
    1. SqlDataAdapter daOwner = new SqlDataAdapter();
    2.         DataSet dsOwner = new DataSet();
    3.  
    4.        private void fillDataset()
    5.        {
    6.            SqlConnection conn;
    7.            conn = new SqlConnection();
    8.            {
    9.                if (conn.State == ConnectionState.Open)
    10.                    conn.Close();
    11.                conn.ConnectionString = clsmethods.connString();
    12.                conn.Open();
    13.            }
    14.            string strSQL = "SELECT * from tblOwner";
    15.            daOwner.SelectCommand = new SqlCommand(strSQL, conn);
    16.            SqlCommandBuilder myCB = new SqlCommandBuilder(daOwner);
    17.            daOwner.Fill(dsOwner, "Owner");
    18.            conn.Close();
    19.        }

    I need to know if finish before executing my next function which is populating my ListView items from my Dataset.

    C# Code:
    1. private void loadOwnerDS()
    2.         {
    3.             DataTable dtable = dsOwner.Tables["Owner"];
    4.             lvOwner.Items.Clear();
    5.             for (int i = 0; i < dtable.Rows.Count; i++)
    6.             {
    7.                 DataRow drow = dtable.Rows[i];
    8.                 if (drow.RowState != DataRowState.Deleted)
    9.                 {
    10.                     ListViewItem lvi = new ListViewItem(drow[0].ToString());
    11.                     lvi.SubItems.Add(drow[1].ToString());
    12.                     lvi.SubItems.Add(drow[2].ToString());
    13.                     lvi.SubItems.Add(drow[3].ToString());
    14.                     lvOwner.Items.Add(lvi);
    15.                 }
    16.             }
    17.         }

    I am calling fillDataset using Thread to avoid freezing of my Main Form.

    C# Code:
    1. private void frmMain_Load(object sender, EventArgs e)
    2.         {
    3.             this._fillDataset = new Thread(this.fillDataset);
    4.             this._fillDataset.Priority = ThreadPriority.BelowNormal;
    5.             this._fillDataset.Start();          
    6.            
    7.         }

    Thanks..
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Detect if Filling DataSet data is finish

    First up, what's the point of checking the ConnectionState of a connection that you just created and closing it if it's open? How can it possibly be open? How can it possibly be open? You just created it.

    As for the question, Fill is a synchronous method. When it returns, the DataSet/DataTable has been filled.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: Detect if Filling DataSet data is finish

    I change my code into this
    and call in using thread
    but throw me a cross thread exemption
    this.lvOwner.Items.Add(ls);

    How can I apply Invoke to the listview.items.add?
    I have an idea that I will make a separate function that use invoke required and pass that 4 parameters...but I don't think if that is a good idea for that..or there is another good idea aside from my idea?

    Code:
            private void loadOwner()
            {
                string strsql = null;
                SqlDataReader reader = default(SqlDataReader);
                SqlConnection conn = default(SqlConnection);
                conn = new SqlConnection();
                try
                {
                    conn.ConnectionString = clsmethods.connString();
                    strsql = "Select ID,name, address, contact from tblOwner ORDER BY ID ASC";
                    conn.Open();
                    SqlCommand myCommand = new SqlCommand(strsql);
                    myCommand.Connection = conn;
                    reader = myCommand.ExecuteReader();
                    lvOwner.Items.Clear();
                    while (reader.Read())
                    {
                        ListViewItem ls = new ListViewItem(reader.GetValue(0).ToString());
                        ls.SubItems.Add(reader.GetValue(1).ToString());
                        ls.SubItems.Add(reader.GetValue(2).ToString());
                        ls.SubItems.Add(reader.GetValue(3).ToString());
                        this.lvOwner.Items.Add(ls);
                    }
                    conn.Close();
                    countReg();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error connecting to Database");
                }
                finally
                {
                    conn.Close();
                }
            }
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Detect if Filling DataSet data is finish

    Follow the CodeBank link in my signature and check out my thread on Accessing Controls From Worker Threads to learn how to use Invoke and InvokeRequired.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width