[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:
SqlDataAdapter daOwner = new SqlDataAdapter();
DataSet dsOwner = new DataSet();
private void fillDataset()
{
SqlConnection conn;
conn = new SqlConnection();
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.ConnectionString = clsmethods.connString();
conn.Open();
}
string strSQL = "SELECT * from tblOwner";
daOwner.SelectCommand = new SqlCommand(strSQL, conn);
SqlCommandBuilder myCB = new SqlCommandBuilder(daOwner);
daOwner.Fill(dsOwner, "Owner");
conn.Close();
}
I need to know if finish before executing my next function which is populating my ListView items from my Dataset.
C# Code:
private void loadOwnerDS()
{
DataTable dtable = dsOwner.Tables["Owner"];
lvOwner.Items.Clear();
for (int i = 0; i < dtable.Rows.Count; i++)
{
DataRow drow = dtable.Rows[i];
if (drow.RowState != DataRowState.Deleted)
{
ListViewItem lvi = new ListViewItem(drow[0].ToString());
lvi.SubItems.Add(drow[1].ToString());
lvi.SubItems.Add(drow[2].ToString());
lvi.SubItems.Add(drow[3].ToString());
lvOwner.Items.Add(lvi);
}
}
}
I am calling fillDataset using Thread to avoid freezing of my Main Form.
C# Code:
private void frmMain_Load(object sender, EventArgs e)
{
this._fillDataset = new Thread(this.fillDataset);
this._fillDataset.Priority = ThreadPriority.BelowNormal;
this._fillDataset.Start();
}
Thanks..
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.
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();
}
}
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.