473,395 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Query database in background thread

Hi All:

I'm new in Threading and Web Services, can someone please verify my
code, i'm not sure whether this is the correct way, althought it is
partially work, but some time it will raise exception at FillDatatable
dr.AcceptChanges, and also the XTraGridControl will not response when
click on the column header to do sorting and filtering.

What i'm trying to do is very simple, i just want to retrieve a very
large table from web service and show it on the GridControl, the query
will be execute in the background thread, so that user is able to
scroll, filter, sort the result on the gridcontrol while the query is
execute in the background
Here is the code:

edtSQL - a multiline text box to enter SQL statement
edtKeyFields - a text box to enter the key fields of the SQL statement
GridControl - I use developer XTraGrid control
btnFillData - Button control
btnStopFill - Button Control
DataAccessWS.DataAccessServiceWse - A web service

Private QueryThread As Thread
Private Delegate Sub FillDataTableDelegate(ByVal table As DataTable)
Private bFilling As Boolean
' when use click this button the background thread will start
' filling data to grid's datasource
Private Sub btnFillData_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnFillData.Click
Dim proxy As DataAccessWS.DataAccessServiceWse = New
DataAccessWS.DataAccessServiceWse

Me.ProgressBar1.Step = 1
Me.ProgressBar1.Minimum = 0
' call the web method to calculate number of row will be return
' by the SQL statement
Me.ProgressBar1.Maximum = CInt(proxy.ExecuteScalar("SELECT
Count(*) FROM (" & edtSQL.Text & ") as tem"))

GridControl3.DataSource = Nothing
GridView3.Columns.Clear()
QueryThread = Nothing

QueryThread = New Thread(AddressOf QueryDataBase)
QueryThread.IsBackground = True
QueryThread.Name = "QueryThread"
QueryThread.Start()
End Sub

' Sub routine used by the Background thread to query database
Public Sub QueryDataBase()
Dim proxy As DataAccessWS.DataAccessServiceWse = New
DataAccessWS.DataAccessServiceWse
Dim doc As XmlDocument
Dim xn As XmlNode
Dim dt As DataTable = New DataTable

If GridControl3.DataSource Is Nothing Then
' Call the web method to retrieve first page of record
' param1=SQL statement, param2=filter criteria,
' param3=key field, param4=Page size
xn = proxy.RetrieveFirstPageRecord(edtSQL.Text, "",
edtKeyFields.Text, 100)
doc = XMLNodeToXMLDocument(xn)
LoadDataTableFromXMLDocument(dt, doc)

' Is is correct to create New FillDataTableDelegate each time
' the BeginInvoke is call ?
Me.BeginInvoke(New FillDataTableDelegate(AddressOf
FillDatatable), New Object() {dt})
End If

While True
' wait until FillDatatable completed
While bFilling
' Is it ok to put this line in the thread function ?
Application.DoEvents()
End While

xn = Nothing
With DirectCast(GridControl3.DataSource, DataTable)
If .Rows.Count > 0 Then
' Call the web method to retrieve next page of record
' param1=Last row in current page, param2=SQL
' statement, param3=filter criteria, param4=key field
' param5=Page size
xn =
proxy.RetrieveNextPageRecord(SaveDataRowAsXMLDocum ent(.Rows(.Rows.Count
- 1)), edtSQL.Text, "", edtKeyFields.Text, 100)
End If
End With

If Not xn Is Nothing Then
dt.Clear()
doc = XMLNodeToXMLDocument(xn)
LoadDataTableFromXMLDocument(dt, doc)

' if RetrieveNextPageRecord return 0 row means Eof
If dt.Rows.Count > 0 Then
Me.BeginInvoke(New FillDataTableDelegate(AddressOf
FillDatatable), New Object() {dt})
Else
Exit While
End If

If QueryThread.ThreadState = ThreadState.Aborted Then
Exit While
End If
End If
End While
End Sub

' Sub routine that is to be executed on Form's thread
Public Sub FillDatatable(ByVal table As DataTable)
bFilling = True
If GridControl3.DataSource Is Nothing Then
GridControl3.DataSource = table.Copy
Me.ProgressBar1.Value = table.Rows.Count
Else
Dim dr As DataRow

With DirectCast(GridControl3.DataSource, DataTable)
.BeginLoadData()
For i As Integer = 0 To table.Rows.Count - 1
dr = .NewRow
CopyDataRow(table.Rows(i), dr, "*")
dr.AcceptChanges()
.Rows.Add(dr)

Me.ProgressBar1.PerformStep()

' With this, the grid is able to response to scrolling
' record using arrow key and mouse,
' but unable to resonse the column header click.
Application.DoEvents()
Next
.EndLoadData()
End With
End If
bFilling = False
End Sub

Private Sub btnStopFill_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnStopFill.Click
QueryThread.Abort()
End Sub

Thanks
JCVoon
Nov 21 '05 #1
0 1717

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Norm | last post by:
I have run into problems from time to time (and this is one of those times) using visual basic to access an external database and perform a basic select from statement. When the table is extremely...
96
by: Karen Hill | last post by:
SELECT surgeries.*, animals.* FROM surgeries INNER JOIN animals ON .=. AND WHERE ((.=Date()) Or .=Date()); I'm trying to write a query that joins two table together, animals and surgeries...
0
by: Gomaw Beoyr | last post by:
Hello The book "C# Black Book" chapter about Threads says that a "background thread" cannot communicate directly with a visual element, e.g. a label, and thus has to use the BeginInvoke method,...
0
by: Johanna | last post by:
Hello, Thread was being aborted exception is thrown by my asp.net application. I hope someone could help me with this error that I get in windows 2003 server. This error has not occured with...
9
by: Eugeny Balakhonov | last post by:
Hi! How to change query priority dynamically? I have a web site which uses PostgreSQL as a host database and a background program which uses this database too.
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
2
by: WuBin | last post by:
Hi, I have a GridVew and a sqlDtaSurce control in ASP.NET real-time page. The sqlDtaSurce control use a complicated query to load data from a SQL database. This real-time page is reloaded by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.