שתף באמצעות


how to fix problem freeze form when execute query in textbox1 and also problem don't show PictureBox1 Because of freeze form in vb.net ?

Question

Saturday, January 11, 2014 12:42 PM

note : the (PictureBox1) contains picture (gif - progressbar)

I'm working this code :

Imports System.Data.SqlClient
Public Class Form1
    Dim sCommand As SqlCommand
    Dim sAdapter As SqlDataAdapter
    Dim sBuilder As SqlCommandBuilder
    Dim sDs As DataSet
    Dim sTable As DataTable
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PictureBox1.Visible = False
    End Sub


    Private Sub Execute_Query_Click(sender As Object, e As EventArgs) Handles Execute_Query.Click
        PictureBox1.Visible = True
        Try
            Dim connectionString As String = "Data Source=xxxxx;Persist Security Info=True;User ID=xxxxx;Initial Catalog=xxxxx;Data Source=xxxxx"
            Dim sql As String = "" & TextBox1.Text & ""
            Dim connection As New SqlConnection(connectionString)
            Dim sCommand As New SqlCommand(sql, connection)
            sCommand.Connection.Open()
            sAdapter = New SqlDataAdapter(sCommand)
            sBuilder = New SqlCommandBuilder(sAdapter)
            sDs = New DataSet()
            sAdapter.Fill(sDs, "%")
            sTable = sDs.Tables("%")
            sCommand.Connection.Close()
            DataGridView1.DataSource = sDs.Tables("%")
            PictureBox1.Visible = False
            MsgBox("success", MsgBoxStyle.Information)
        Catch ex As Exception
            PictureBox1.Visible = False
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

All replies (11)

Saturday, January 11, 2014 6:59 PM ✅Answered

Hi,

 I believe the problem you are having is because the UI thread is busy running your sql commands and not allowing the picturebox to be updated and repainted. You could try using a BackgroundWorker to do the sql commands. That should keep your form from freezing up. The only thing with doing it this way is you can not access any controls on the Form from the BackgroundWorker thread or you will get CrossThread errors. However, you can work around that by using some class scoped variables. I am not familiar with using sql but, i set this up so that it looked about rite to me, so it may work for you and if not then you can experiment with it.

Imports System.Data.SqlClient

Public Class Form1
    Dim WithEvents bgw As New System.ComponentModel.BackgroundWorker
    Dim sCommand As SqlCommand
    Dim sAdapter As SqlDataAdapter
    Dim sBuilder As SqlCommandBuilder
    Dim sDs As DataSet
    Dim sTable As DataTable
    Dim txt As String = "" 'This will be used for accessing the textbox text
    Dim err As Boolean = False 'This will be used for telling if an error happened

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        PictureBox1.Visible = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PictureBox1.Visible = True
        txt = TextBox1.Text
        bgw.RunWorkerAsync()
    End Sub

    Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        Try
            Dim connectionString As String = "Data Source=xxxxx;Persist Security Info=True;User ID=xxxxx;Initial Catalog=xxxxx;Data Source=xxxxx"
            Dim sql As String = "" & txt & ""
            Dim connection As New SqlConnection(connectionString)
            Dim sCommand As New SqlCommand(sql, connection)
            sCommand.Connection.Open()
            sAdapter = New SqlDataAdapter(sCommand)
            sBuilder = New SqlCommandBuilder(sAdapter)
            sDs = New DataSet()
            sAdapter.Fill(sDs, "%")
            sTable = sDs.Tables("%")
            sCommand.Connection.Close()
        Catch ex As Exception
            err = True
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
        If Not err Then
            DataGridView1.DataSource = sDs.Tables("%")
            MessageBox.Show("Success", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        PictureBox1.Visible = False
        err = False
    End Sub
End Class

Saturday, January 11, 2014 1:13 PM

hi srajmuneer,

i test your code as follow:

Imports System

Imports System.Data.SqlClient

Public Class Form1

    Dim sCommand As SqlCommand

    Dim sAdapter As SqlDataAdapter

    Dim sBuilder As SqlCommandBuilder

    Dim sDs As DataSet

    Dim sTable As DataTable

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        PictureBox1.Visible = True

       

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        PictureBox1.Visible = True

        Try

            Dim connectionString As String = "Data Source='Accounting-pc';Initial Catalog='rasol';Integrated Security='sspi';"

            Dim sql As String = "" & TextBox1.Text & ""

            Dim connection As New SqlConnection(connectionString)

            Dim sCommand As New SqlCommand(sql, connection)

            sCommand.Connection.Open()

            sAdapter = New SqlDataAdapter(sCommand)

            sBuilder = New SqlCommandBuilder(sAdapter)

            sDs = New DataSet()

            sAdapter.Fill(sDs, "%")

            sTable = sDs.Tables("%")

            sCommand.Connection.Close()

            DataGridView1.DataSource = sDs.Tables("%")

            PictureBox1.Visible = True

            MsgBox("success", MsgBoxStyle.Information)

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

End Class

and i dont get any error, of course i use TextBox1_TextChanged event.

regards.


Saturday, January 11, 2014 1:23 PM

I don't know what you are attempting. 

Seems to be a kind of program to show datatables in a DataGridView by using an SQL string. 

But because of the fact that the connectionstring is rubish it should only show the error message.

Assume that the connectionstring is a dummy to avoid information. 

Than still it can take very long time as long as there is no where clause in the SQL transact code in the textbox.

The picturebox should never show anything otherwise there is something wrong.

Success
Cor


Saturday, January 11, 2014 4:11 PM

thank you rasol , but my problem freeze form when click button execute query and don't show PictureBox1 Because of  freeze form , the (PictureBox1) contains picture (gif - progressbar)


Saturday, January 11, 2014 4:19 PM

thank you Cor Ligthert , I'm want show PictureBox1 which contains picture (gif - progressbar) when click button execute query

or

display progress bar while executing big SQLCommand


Saturday, January 11, 2014 5:01 PM

hi srajmuneer,

what is your version of vs?

pls aware me.

in my test, picturebox was showed.

regards.

pls try this code:

Imports System

Imports System.Data.SqlClient

Public Class Form1

    Dim sCommand As SqlCommand

    Dim sAdapter As SqlDataAdapter

    Dim sBuilder As SqlCommandBuilder

    Dim sDs As DataSet

    Dim sTable As DataTable

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        PictureBox1.Visible = True

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        PictureBox1.Visible = True

        Try

            Dim connectionString As String = "Data Source='Accounting-pc';Initial Catalog='rasol';Integrated Security='sspi';"

            Dim sql As String = "" & TextBox1.Text & ""

            Dim connection As New SqlConnection(connectionString)

            Dim sCommand As New SqlCommand(sql, connection)

            sCommand.Connection.Open()

            sAdapter = New SqlDataAdapter(sCommand)

            sBuilder = New SqlCommandBuilder(sAdapter)

            sDs = New DataSet()

            sAdapter.Fill(sDs, "%")

            sTable = sDs.Tables("%")

            sCommand.Connection.Close()

            DataGridView1.DataSource = sDs.Tables("%")

            PictureBox1.Visible = True

            MsgBox("success", MsgBoxStyle.Information)

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

End Class

best regards.


Saturday, January 11, 2014 6:05 PM

thank you rasol , I'm use VB 2013 .

I'm mean when click button execute query show PictureBox1 while execute query And then hide PictureBox1 and show data in DGV

sample : picture Gif-progressbar


Saturday, January 11, 2014 7:38 PM

hi ,

the result of my last code and IronRazerz's code is same as.i test it.

also i have not any error.

best regards.

http://social.msdn.microsoft.com/Forums/windows/en-US/c7d6aa37-9069-457e-b8db-fa2ec02fa692/progress-bar-when-running-a-function-with-a-big-sql-query?forum=winforms
http://stackoverflow.com/questions/14578123/how-to-use-a-progressbar-properly-in-vb-net

http://social.msdn.microsoft.com/Forums/vstudio/en-US/c68e948f-166f-4c62-9376-e4ad35bd6f38/background-worker-and-progress-bar-in-vbnet?forum=vbgeneral


Saturday, January 11, 2014 9:31 PM

use

this.refresh()

after PictureBox1.Visible = True/False

Its not freezing but not showing so refresh the form will show the picturebox

hope it will help you 


Monday, January 13, 2014 7:13 AM

Thank you very much IronRazerz


Monday, January 13, 2014 12:12 PM

Thank you very much IronRazerz

 Your Welcome.   :)